Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(#128) instruction: freeze #137

Merged
merged 4 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions asm/inst.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ func (fgen *funcGen) newValueInst(ident ir.LocalIdent, old ast.ValueInstruction)
return fgen.newPhiInst(ident, old)
case *ast.SelectInst:
return fgen.newSelectInst(ident, old)
case *ast.FreezeInst:
return fgen.newFreezeInst(ident, old)
case *ast.CallInst:
return fgen.newCallInst(ident, old)
case *ast.VAArgInst:
Expand Down Expand Up @@ -290,6 +292,8 @@ func (fgen *funcGen) irValueInst(new ir.Instruction, old ast.ValueInstruction) e
return fgen.irPhiInst(new, old)
case *ast.SelectInst:
return fgen.irSelectInst(new, old)
case *ast.FreezeInst:
return fgen.irFreezeInst(new, old)
case *ast.CallInst:
return fgen.irCallInst(new, old)
case *ast.VAArgInst:
Expand Down
27 changes: 27 additions & 0 deletions asm/inst_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ func (fgen *funcGen) newSelectInst(ident ir.LocalIdent, old *ast.SelectInst) (*i
return &ir.InstSelect{LocalIdent: ident, Typ: typ}, nil
}

// newSelectInst returns a new IR select instruction (without body but with
// type) based on the given AST select instruction.
func (fgen *funcGen) newFreezeInst(ident ir.LocalIdent, old *ast.FreezeInst) (*ir.InstFreeze, error) {
x, err := fgen.irTypeValue(old.X())
if err != nil {
return nil, errors.WithStack(err)
}
return &ir.InstFreeze{LocalIdent: ident, X: x}, nil
}

// newCallInst returns a new IR call instruction (without body but with type)
// based on the given AST call instruction.
func (fgen *funcGen) newCallInst(ident ir.LocalIdent, old *ast.CallInst) (*ir.InstCall, error) {
Expand Down Expand Up @@ -249,6 +259,23 @@ func (fgen *funcGen) irSelectInst(new ir.Instruction, old *ast.SelectInst) error
return nil
}

// --- [ freeze ] ----------------------------------------------------------

// irFreezeInst translates the given AST freeze instruction into an
// equivalent IR instruction.
func (fgen *funcGen) irFreezeInst(new ir.Instruction, old *ast.FreezeInst) error {
inst, ok := new.(*ir.InstFreeze)
if !ok {
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstFreeze, got %T", new))
}
x, err := fgen.irTypeValue(old.X())
if err != nil {
return errors.WithStack(err)
}
inst.X = x
return nil
}

// --- [ call ] ----------------------------------------------------------------

// irCallInst translates the given AST call instruction into an equivalent IR
Expand Down
45 changes: 45 additions & 0 deletions ir/inst_other.go
Original file line number Diff line number Diff line change
Expand Up @@ -674,3 +674,48 @@ func (inst *InstCleanupPad) LLString() string {
}
return buf.String()
}

// ~~~ [ freeze ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

// InstFreeze is an LLVM IR freeze instruction.
type InstFreeze struct {
// Name of local variable associated with the result.
LocalIdent

X value.Value

// extra.

// (optional) Metadata.
Metadata
}

// NewInstFreeze returns a new freeze instruction based on the given
// typed-value
func NewInstFreeze(x value.Value) *InstFreeze {
return &InstFreeze{X: x}
}

// String returns the LLVM syntax representation of the instruction as a
// type-value pair.
func (inst *InstFreeze) String() string {
return fmt.Sprintf("%s %s", inst.Type(), inst.Ident())
}

// Type returns the type of the instruction.
func (inst *InstFreeze) Type() types.Type {
return types.Token
}

// LLString returns the LLVM syntax representation of the instruction.
//
// 'freeze' Type Value
func (inst *InstFreeze) LLString() string {
buf := &strings.Builder{}
fmt.Fprintf(buf, "%s = ", inst.Ident())
fmt.Fprintf(buf, "freeze %s", inst.X)
for _, md := range inst.Metadata {
fmt.Fprintf(buf, ", %s", md)
}
return buf.String()
}
1 change: 1 addition & 0 deletions ir/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ package ir
// *ir.InstFCmp // https://godoc.org/github.com/llir/llvm/ir#InstFCmp
// *ir.InstPhi // https://godoc.org/github.com/llir/llvm/ir#InstPhi
// *ir.InstSelect // https://godoc.org/github.com/llir/llvm/ir#InstSelect
// *ir.InstFreeze // https://godoc.org/github.com/llir/llvm/ir#InstFreeze
// *ir.InstCall // https://godoc.org/github.com/llir/llvm/ir#InstCall
// *ir.InstVAArg // https://godoc.org/github.com/llir/llvm/ir#InstVAArg
// *ir.InstLandingPad // https://godoc.org/github.com/llir/llvm/ir#InstLandingPad
Expand Down
1 change: 1 addition & 0 deletions ir/sumtype.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (*InstICmp) isInstruction() {}
func (*InstFCmp) isInstruction() {}
func (*InstPhi) isInstruction() {}
func (*InstSelect) isInstruction() {}
func (*InstFreeze) isInstruction() {}
func (*InstCall) isInstruction() {}
func (*InstVAArg) isInstruction() {}
func (*InstLandingPad) isInstruction() {}
Expand Down