github.com/llir/llvm@v0.3.6/asm/inst_unary.go (about) 1 package asm 2 3 import ( 4 "fmt" 5 6 "github.com/llir/ll/ast" 7 "github.com/llir/llvm/ir" 8 "github.com/pkg/errors" 9 ) 10 11 // === [ Create IR ] =========================================================== 12 13 // newFNegInst returns a new IR fneg instruction (without body but with type) 14 // based on the given AST fneg instruction. 15 func (fgen *funcGen) newFNegInst(ident ir.LocalIdent, old *ast.FNegInst) (*ir.InstFNeg, error) { 16 typ, err := fgen.gen.irType(old.X().Typ()) 17 if err != nil { 18 return nil, errors.WithStack(err) 19 } 20 return &ir.InstFNeg{LocalIdent: ident, Typ: typ}, nil 21 } 22 23 // === [ Translate AST to IR ] ================================================= 24 25 // --- [ fneg ] ---------------------------------------------------------------- 26 27 // irFNegInst translates the given AST fneg instruction into an equivalent IR 28 // instruction. 29 func (fgen *funcGen) irFNegInst(new ir.Instruction, old *ast.FNegInst) error { 30 inst, ok := new.(*ir.InstFNeg) 31 if !ok { 32 panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstFNeg, got %T", new)) 33 } 34 // X operand. 35 x, err := fgen.irTypeValue(old.X()) 36 if err != nil { 37 return errors.WithStack(err) 38 } 39 inst.X = x 40 // (optional) Fast math flags. 41 inst.FastMathFlags = irFastMathFlags(old.FastMathFlags()) 42 // (optional) Metadata. 43 md, err := fgen.gen.irMetadataAttachments(old.Metadata()) 44 if err != nil { 45 return errors.WithStack(err) 46 } 47 inst.Metadata = md 48 return nil 49 }