mxbuiltins.go raw

   1  package compiler
   2  
   3  // Moxie builtin interception.
   4  //
   5  // The source rewrite phase converts Moxie-specific syntax into calls to
   6  // stub functions (__moxie_concat for | pipe concatenation). This file
   7  // intercepts those calls at the SSA level and emits the correct LLVM IR.
   8  //
   9  // Channel literals (chan T{}) are rewritten to make(chan T) at text level
  10  // and go through the standard MakeChan SSA path.
  11  
  12  import (
  13  	"golang.org/x/tools/go/ssa"
  14  	"tinygo.org/x/go-llvm"
  15  )
  16  
  17  // createMoxieConcat handles __moxie_concat(a, b []byte) []byte.
  18  // Emits a call to runtime.bytesConcat.
  19  func (b *builder) createMoxieConcat(instr *ssa.CallCommon) llvm.Value {
  20  	a := b.getValue(instr.Args[0], instr.Pos())
  21  	bv := b.getValue(instr.Args[1], instr.Pos())
  22  	return b.createRuntimeCall("bytesConcat", []llvm.Value{a, bv}, "")
  23  }
  24  
  25  // createMoxieEq handles __moxie_eq(a, b []byte) bool.
  26  // Emits a call to runtime.stringEqual (same layout as []byte).
  27  func (b *builder) createMoxieEq(instr *ssa.CallCommon) llvm.Value {
  28  	a := b.getValue(instr.Args[0], instr.Pos())
  29  	bv := b.getValue(instr.Args[1], instr.Pos())
  30  	return b.createRuntimeCall("stringEqual", []llvm.Value{a, bv}, "")
  31  }
  32  
  33  // createMoxieLt handles __moxie_lt(a, b []byte) bool.
  34  // Emits a call to runtime.stringLess (same layout as []byte).
  35  func (b *builder) createMoxieLt(instr *ssa.CallCommon) llvm.Value {
  36  	a := b.getValue(instr.Args[0], instr.Pos())
  37  	bv := b.getValue(instr.Args[1], instr.Pos())
  38  	return b.createRuntimeCall("stringLess", []llvm.Value{a, bv}, "")
  39  }
  40