Skip to content

Commit

Permalink
The builtin copy implemented as AST node.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Jul 25, 2024
1 parent 78c7323 commit c7b3ad1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 112 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ The MPCL runtime defines the following builtin functions:
- [ ] SSA aliasing is 1:1 but `amov` has 2:1 relation
- [ ] variable liveness analysis for templates
- [ ] BitShift
- [ ] `copy()` does not work on arrays which have been `make()`:ed
- [x] `copy()` does not work on arrays which have been `make()`:ed
- [ ] `&base[pos][i]` returns the address of the first element
- [ ] reading from `*[32]int32` returns invalid values
- [x] Pointer handling
Expand Down
111 changes: 0 additions & 111 deletions compiler/ast/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ type Eval func(args []AST, env *Env, ctx *Codegen, gen *ssa.Generator,

// Predeclared identifiers.
var builtins = map[string]Builtin{
"copy": {
SSA: copySSA,
},
"floorPow2": {
SSA: floorPow2SSA,
Eval: floorPow2Eval,
Expand All @@ -57,114 +54,6 @@ var builtins = map[string]Builtin{
},
}

func copySSA(block *ssa.Block, ctx *Codegen, gen *ssa.Generator,
args []ssa.Value, loc utils.Point) (*ssa.Block, []ssa.Value, error) {

if len(args) != 2 {
return nil, nil, ctx.Errorf(loc,
"invalid amount of arguments in call to copy")
}
dst := args[0]
src := args[1]

var baseName string
var baseType types.Info
var baseScope ssa.Scope
var baseBindings *ssa.Bindings
var base ssa.Value

var dstOffset types.Size
var elementType types.Info

switch dst.Type.Type {
case types.TArray, types.TSlice:
baseName = dst.Name
baseType = dst.Type
baseScope = dst.Scope
baseBindings = block.Bindings

dstOffset = 0
elementType = *dst.Type.ElementType
base = dst

case types.TPtr:
elementType = *dst.Type.ElementType
if !elementType.Type.Array() {
return nil, nil, ctx.Errorf(loc, "setting elements of non-array %s",
elementType)
}
baseName = dst.PtrInfo.Name
baseType = dst.PtrInfo.ContainerType
baseScope = dst.PtrInfo.Scope
baseBindings = dst.PtrInfo.Bindings

dstOffset = dst.PtrInfo.Offset
elementType = *elementType.ElementType

b, ok := baseBindings.Get(baseName)
if !ok {
return nil, nil, ctx.Errorf(loc, "undefined: %s", baseName)
}
base = b.Value(block, gen)

default:
return nil, nil, ctx.Errorf(loc,
"arguments to copy must be slices; have %s, %s",
dst.Type.Type, src.Type.Type)
}

var srcType types.Info
if src.Type.Type == types.TPtr {
srcType = *src.Type.ElementType
} else {
srcType = src.Type
}

if !srcType.Type.Array() {
return nil, nil, ctx.Errorf(loc,
"second argument to copy should be slice or array (%v)", src.Type)
}
if !elementType.Equal(*srcType.ElementType) {
return nil, nil, ctx.Errorf(loc,
"arguments to copy have different element types: %s and %s",
baseType.ElementType, src.Type.ElementType)
}

dstBits := dst.Type.Bits
srcBits := src.Type.Bits

var copied types.Size
if srcBits > dstBits {
fromConst := gen.Constant(int64(0), types.Undefined)
toConst := gen.Constant(int64(dstBits), types.Undefined)

tmp := gen.AnonVal(dst.Type)
block.AddInstr(ssa.NewSliceInstr(src, fromConst, toConst, tmp))
src = tmp
srcBits = dstBits

copied = dst.Type.ArraySize
} else {
copied = src.Type.ArraySize
}

lValue := gen.NewVal(baseName, baseType, baseScope)

fromConst := gen.Constant(int64(dstOffset), types.Undefined)
toConst := gen.Constant(int64(dstOffset+srcBits), types.Undefined)

block.AddInstr(ssa.NewAmovInstr(src, base, fromConst, toConst, lValue))
err := baseBindings.Set(lValue, nil)
if err != nil {
return nil, nil, ctx.Error(loc, err.Error())
}

v := gen.Constant(int64(copied), types.Undefined)
gen.AddConstant(v)

return block, []ssa.Value{v}, nil
}

func floorPow2SSA(block *ssa.Block, ctx *Codegen, gen *ssa.Generator,
args []ssa.Value, loc utils.Point) (*ssa.Block, []ssa.Value, error) {
return nil, nil, ctx.Errorf(loc, "floorPow2SSA not implemented")
Expand Down
17 changes: 17 additions & 0 deletions testsuite/lang/copy_make.mpcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// -*- go -*-

package main

// @Hex
// @LSB
// @Test 0x11223344 0xaabb = 0x11223344aa 0x11223344aa 5
func main(g, e []byte) ([]byte, []byte, int) {
buf := make([]byte, 5)
n := copy(buf, g)
m := copy(buf[n:], e)

buf2 := make([]byte, 5)
copy(buf2, buf)

return buf, buf2, n + m
}

0 comments on commit c7b3ad1

Please sign in to comment.