github.com/llir/llvm@v0.3.6/asm/value.go (about)

     1  package asm
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/llir/ll/ast"
     7  	"github.com/llir/llvm/ir/types"
     8  	"github.com/llir/llvm/ir/value"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  // irValue translates the AST value into an equivalent IR value.
    13  func (fgen *funcGen) irValue(typ types.Type, old ast.Value) (value.Value, error) {
    14  	switch old := old.(type) {
    15  	case *ast.GlobalIdent:
    16  		ident := globalIdent(*old)
    17  		v, ok := fgen.gen.new.globals[ident]
    18  		if !ok {
    19  			return nil, errors.Errorf("unable to locate global identifier %q", ident.Ident())
    20  		}
    21  		return v, nil
    22  	case *ast.LocalIdent:
    23  		ident := localIdent(*old)
    24  		v, ok := fgen.locals[ident]
    25  		if !ok {
    26  			return nil, errors.Errorf("unable to locate local identifier %q of %q", ident.Ident(), fgen.f.Ident())
    27  		}
    28  		return v, nil
    29  	case *ast.InlineAsm:
    30  		return irInlineAsm(typ, old), nil
    31  	case ast.Constant:
    32  		return fgen.gen.irConstant(typ, old)
    33  	default:
    34  		panic(fmt.Errorf("support for AST value %T not yet implemented", old))
    35  	}
    36  }
    37  
    38  // irTypeConst translates the AST type-value pair into an equivalent IR value.
    39  func (fgen *funcGen) irTypeValue(old ast.TypeValue) (value.Value, error) {
    40  	// Type.
    41  	typ, err := fgen.gen.irType(old.Typ())
    42  	if err != nil {
    43  		return nil, errors.WithStack(err)
    44  	}
    45  	// Value.
    46  	return fgen.irValue(typ, old.Val())
    47  }