Skip to content

Commit

Permalink
asm: split instruction translation into dedicated files
Browse files Browse the repository at this point in the history
  • Loading branch information
mewmew committed Nov 2, 2018
1 parent a2e3864 commit c0d8f96
Show file tree
Hide file tree
Showing 8 changed files with 986 additions and 928 deletions.
34 changes: 34 additions & 0 deletions asm/inst_aggregate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package asm

import (
"fmt"

"github.com/llir/l/ir"
"github.com/mewmew/l-tm/asm/ll/ast"
)

// --- [ Aggregate instructions ] ----------------------------------------------

// ~~~ [ extractvalue ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

func (fgen *funcGen) astToIRInstExtractValue(inst ir.Instruction, old *ast.ExtractValueInst) (*ir.InstExtractValue, error) {
i, ok := inst.(*ir.InstExtractValue)
if !ok {
// NOTE: panic since this would indicate a bug in the implementation.
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstExtractValue, got %T", inst))
}
// TODO: implement
return i, nil
}

// ~~~ [ insertvalue ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

func (fgen *funcGen) astToIRInstInsertValue(inst ir.Instruction, old *ast.InsertValueInst) (*ir.InstInsertValue, error) {
i, ok := inst.(*ir.InstInsertValue)
if !ok {
// NOTE: panic since this would indicate a bug in the implementation.
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstInsertValue, got %T", inst))
}
// TODO: implement
return i, nil
}
348 changes: 348 additions & 0 deletions asm/inst_binary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,348 @@
package asm

import (
"fmt"

"github.com/llir/l/ir"
"github.com/mewmew/l-tm/asm/ll/ast"
"github.com/pkg/errors"
)

// --- [ Binary instructions ] -------------------------------------------------

// ~~~ [ add ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

func (fgen *funcGen) astToIRInstAdd(inst ir.Instruction, old *ast.AddInst) (*ir.InstAdd, error) {
i, ok := inst.(*ir.InstAdd)
if !ok {
// NOTE: panic since this would indicate a bug in the implementation.
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstAdd, got %T", inst))
}
// Overflow flags.
i.OverflowFlags = irOverflowFlags(old.OverflowFlags())
// X operand.
x, err := fgen.astToIRTypeValue(old.X())
if err != nil {
return nil, errors.WithStack(err)
}
i.X = x
// Y operand.
y, err := fgen.astToIRValue(x.Type(), old.Y())
if err != nil {
return nil, errors.WithStack(err)
}
i.Y = y
return i, nil
}

// ~~~ [ fadd ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

func (fgen *funcGen) astToIRInstFAdd(inst ir.Instruction, old *ast.FAddInst) (*ir.InstFAdd, error) {
i, ok := inst.(*ir.InstFAdd)
if !ok {
// NOTE: panic since this would indicate a bug in the implementation.
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstFAdd, got %T", inst))
}
// Fast math flags.
i.FastMathFlags = irFastMathFlags(old.FastMathFlags())
// X operand.
// TODO: remove xType in favour of x.Type().
xType, err := fgen.gen.irType(old.X().Typ())
if err != nil {
return nil, errors.WithStack(err)
}
x, err := fgen.astToIRTypeValue(old.X())
if err != nil {
return nil, errors.WithStack(err)
}
i.X = x
// Y operand.
y, err := fgen.astToIRValue(xType, old.Y())
if err != nil {
return nil, errors.WithStack(err)
}
i.Y = y
return i, nil
}

// ~~~ [ sub ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

func (fgen *funcGen) astToIRInstSub(inst ir.Instruction, old *ast.SubInst) (*ir.InstSub, error) {
i, ok := inst.(*ir.InstSub)
if !ok {
// NOTE: panic since this would indicate a bug in the implementation.
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstSub, got %T", inst))
}
// Overflow flags.
i.OverflowFlags = irOverflowFlags(old.OverflowFlags())
// X operand.
xType, err := fgen.gen.irType(old.X().Typ())
if err != nil {
return nil, errors.WithStack(err)
}
x, err := fgen.astToIRTypeValue(old.X())
if err != nil {
return nil, errors.WithStack(err)
}
i.X = x
// Y operand.
y, err := fgen.astToIRValue(xType, old.Y())
if err != nil {
return nil, errors.WithStack(err)
}
i.Y = y
return i, nil
}

// ~~~ [ fsub ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

func (fgen *funcGen) astToIRInstFSub(inst ir.Instruction, old *ast.FSubInst) (*ir.InstFSub, error) {
i, ok := inst.(*ir.InstFSub)
if !ok {
// NOTE: panic since this would indicate a bug in the implementation.
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstFSub, got %T", inst))
}
// Fast math flags.
i.FastMathFlags = irFastMathFlags(old.FastMathFlags())
// X operand.
xType, err := fgen.gen.irType(old.X().Typ())
if err != nil {
return nil, errors.WithStack(err)
}
x, err := fgen.astToIRTypeValue(old.X())
if err != nil {
return nil, errors.WithStack(err)
}
i.X = x
// Y operand.
y, err := fgen.astToIRValue(xType, old.Y())
if err != nil {
return nil, errors.WithStack(err)
}
i.Y = y
return i, nil
}

// ~~~ [ mul ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

func (fgen *funcGen) astToIRInstMul(inst ir.Instruction, old *ast.MulInst) (*ir.InstMul, error) {
i, ok := inst.(*ir.InstMul)
if !ok {
// NOTE: panic since this would indicate a bug in the implementation.
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstMul, got %T", inst))
}
// Overflow flags.
i.OverflowFlags = irOverflowFlags(old.OverflowFlags())
// X operand.
xType, err := fgen.gen.irType(old.X().Typ())
if err != nil {
return nil, errors.WithStack(err)
}
x, err := fgen.astToIRTypeValue(old.X())
if err != nil {
return nil, errors.WithStack(err)
}
i.X = x
// Y operand.
y, err := fgen.astToIRValue(xType, old.Y())
if err != nil {
return nil, errors.WithStack(err)
}
i.Y = y
return i, nil
}

// ~~~ [ fmul ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

func (fgen *funcGen) astToIRInstFMul(inst ir.Instruction, old *ast.FMulInst) (*ir.InstFMul, error) {
i, ok := inst.(*ir.InstFMul)
if !ok {
// NOTE: panic since this would indicate a bug in the implementation.
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstFMul, got %T", inst))
}
// Fast math flags.
i.FastMathFlags = irFastMathFlags(old.FastMathFlags())
// X operand.
xType, err := fgen.gen.irType(old.X().Typ())
if err != nil {
return nil, errors.WithStack(err)
}
x, err := fgen.astToIRTypeValue(old.X())
if err != nil {
return nil, errors.WithStack(err)
}
i.X = x
// Y operand.
y, err := fgen.astToIRValue(xType, old.Y())
if err != nil {
return nil, errors.WithStack(err)
}
i.Y = y
return i, nil
}

// ~~~ [ udiv ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

func (fgen *funcGen) astToIRInstUDiv(inst ir.Instruction, old *ast.UDivInst) (*ir.InstUDiv, error) {
i, ok := inst.(*ir.InstUDiv)
if !ok {
// NOTE: panic since this would indicate a bug in the implementation.
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstUDiv, got %T", inst))
}
// X operand.
xType, err := fgen.gen.irType(old.X().Typ())
if err != nil {
return nil, errors.WithStack(err)
}
x, err := fgen.astToIRTypeValue(old.X())
if err != nil {
return nil, errors.WithStack(err)
}
i.X = x
// Y operand.
y, err := fgen.astToIRValue(xType, old.Y())
if err != nil {
return nil, errors.WithStack(err)
}
i.Y = y
return i, nil
}

// ~~~ [ sdiv ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

func (fgen *funcGen) astToIRInstSDiv(inst ir.Instruction, old *ast.SDivInst) (*ir.InstSDiv, error) {
i, ok := inst.(*ir.InstSDiv)
if !ok {
// NOTE: panic since this would indicate a bug in the implementation.
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstSDiv, got %T", inst))
}
// X operand.
xType, err := fgen.gen.irType(old.X().Typ())
if err != nil {
return nil, errors.WithStack(err)
}
x, err := fgen.astToIRTypeValue(old.X())
if err != nil {
return nil, errors.WithStack(err)
}
i.X = x
// Y operand.
y, err := fgen.astToIRValue(xType, old.Y())
if err != nil {
return nil, errors.WithStack(err)
}
i.Y = y
return i, nil
}

// ~~~ [ fdiv ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

func (fgen *funcGen) astToIRInstFDiv(inst ir.Instruction, old *ast.FDivInst) (*ir.InstFDiv, error) {
i, ok := inst.(*ir.InstFDiv)
if !ok {
// NOTE: panic since this would indicate a bug in the implementation.
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstFDiv, got %T", inst))
}
// Fast math flags.
i.FastMathFlags = irFastMathFlags(old.FastMathFlags())
// X operand.
xType, err := fgen.gen.irType(old.X().Typ())
if err != nil {
return nil, errors.WithStack(err)
}
x, err := fgen.astToIRTypeValue(old.X())
if err != nil {
return nil, errors.WithStack(err)
}
i.X = x
// Y operand.
y, err := fgen.astToIRValue(xType, old.Y())
if err != nil {
return nil, errors.WithStack(err)
}
i.Y = y
return i, nil
}

// ~~~ [ urem ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

func (fgen *funcGen) astToIRInstURem(inst ir.Instruction, old *ast.URemInst) (*ir.InstURem, error) {
i, ok := inst.(*ir.InstURem)
if !ok {
// NOTE: panic since this would indicate a bug in the implementation.
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstURem, got %T", inst))
}
// X operand.
xType, err := fgen.gen.irType(old.X().Typ())
if err != nil {
return nil, errors.WithStack(err)
}
x, err := fgen.astToIRTypeValue(old.X())
if err != nil {
return nil, errors.WithStack(err)
}
i.X = x
// Y operand.
y, err := fgen.astToIRValue(xType, old.Y())
if err != nil {
return nil, errors.WithStack(err)
}
i.Y = y
return i, nil
}

// ~~~ [ srem ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

func (fgen *funcGen) astToIRInstSRem(inst ir.Instruction, old *ast.SRemInst) (*ir.InstSRem, error) {
i, ok := inst.(*ir.InstSRem)
if !ok {
// NOTE: panic since this would indicate a bug in the implementation.
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstSRem, got %T", inst))
}
// X operand.
xType, err := fgen.gen.irType(old.X().Typ())
if err != nil {
return nil, errors.WithStack(err)
}
x, err := fgen.astToIRTypeValue(old.X())
if err != nil {
return nil, errors.WithStack(err)
}
i.X = x
// Y operand.
y, err := fgen.astToIRValue(xType, old.Y())
if err != nil {
return nil, errors.WithStack(err)
}
i.Y = y
return i, nil
}

// ~~~ [ frem ] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

func (fgen *funcGen) astToIRInstFRem(inst ir.Instruction, old *ast.FRemInst) (*ir.InstFRem, error) {
i, ok := inst.(*ir.InstFRem)
if !ok {
// NOTE: panic since this would indicate a bug in the implementation.
panic(fmt.Errorf("invalid IR instruction for AST instruction; expected *ir.InstFRem, got %T", inst))
}
// Fast math flags.
i.FastMathFlags = irFastMathFlags(old.FastMathFlags())
// X operand.
xType, err := fgen.gen.irType(old.X().Typ())
if err != nil {
return nil, errors.WithStack(err)
}
x, err := fgen.astToIRTypeValue(old.X())
if err != nil {
return nil, errors.WithStack(err)
}
i.X = x
// Y operand.
y, err := fgen.astToIRValue(xType, old.Y())
if err != nil {
return nil, errors.WithStack(err)
}
i.Y = y
return i, nil
}
Loading

0 comments on commit c0d8f96

Please sign in to comment.