-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c139dc
commit 196a58e
Showing
6 changed files
with
180 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// | ||
// Copyright (c) 2024 Markku Rossi | ||
// | ||
// All rights reserved. | ||
// | ||
|
||
package ast | ||
|
||
import ( | ||
"slices" | ||
|
||
"github.com/markkurossi/mpc/compiler/ssa" | ||
"github.com/markkurossi/mpc/types" | ||
) | ||
|
||
func indexOfs(idx *Index, block *ssa.Block, ctx *Codegen, gen *ssa.Generator) ( | ||
*ssa.Block, *LRValue, types.Info, types.Info, types.Size, error) { | ||
undef := types.Undefined | ||
|
||
lv := idx | ||
|
||
var err error | ||
var values []ssa.Value | ||
var indices []arrayIndex | ||
var lrv *LRValue | ||
|
||
for lrv == nil { | ||
block, values, err = idx.Index.SSA(block, ctx, gen) | ||
if err != nil { | ||
return nil, nil, undef, undef, 0, err | ||
} | ||
if len(values) != 1 { | ||
return nil, nil, undef, undef, 0, | ||
ctx.Errorf(idx.Index, "invalid index") | ||
} | ||
index, err := values[0].ConstInt() | ||
if err != nil { | ||
return nil, nil, undef, undef, 0, ctx.Error(idx.Index, err.Error()) | ||
} | ||
indices = append(indices, arrayIndex{ | ||
i: index, | ||
ast: idx.Index, | ||
}) | ||
switch i := idx.Expr.(type) { | ||
case *Index: | ||
idx = i | ||
|
||
case *VariableRef: | ||
lrv, _, _, err = ctx.LookupVar(block, gen, block.Bindings, i) | ||
if err != nil { | ||
return nil, nil, undef, undef, 0, err | ||
} | ||
|
||
default: | ||
return nil, nil, undef, undef, 0, ctx.Errorf(idx.Expr, | ||
"invalid operation: cannot index %v (%T)", | ||
idx.Expr, idx.Expr) | ||
} | ||
} | ||
slices.Reverse(indices) | ||
|
||
lrv = lrv.Indirect() | ||
baseType := lrv.ValueType() | ||
elType := baseType | ||
var offset types.Size | ||
|
||
for _, index := range indices { | ||
if !elType.Type.Array() { | ||
return nil, nil, undef, undef, 0, ctx.Errorf(index.ast, | ||
"indexing non-array %s (%s)", lv.Expr, elType) | ||
} | ||
if index.i >= elType.ArraySize { | ||
return nil, nil, undef, undef, 0, ctx.Errorf(index.ast, | ||
"invalid array index %d (out of bounds for %d-element array)", | ||
index.i, elType.ArraySize) | ||
} | ||
offset += index.i * elType.ElementType.Bits | ||
elType = *elType.ElementType | ||
} | ||
|
||
return block, lrv, baseType, elType, offset, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// | ||
// Copyright (c) 2024 Markku Rossi | ||
// | ||
// All rights reserved. | ||
// | ||
|
||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
func Tracef(format string, a ...interface{}) { | ||
var filename string | ||
var linenum int | ||
|
||
for skip := 1; ; skip++ { | ||
pc, file, line, ok := runtime.Caller(skip) | ||
if !ok { | ||
break | ||
} | ||
f := runtime.FuncForPC(pc) | ||
if f != nil && strings.HasSuffix(f.Name(), ".errf") { | ||
continue | ||
} | ||
|
||
filename = filepath.Base(file) | ||
linenum = line | ||
break | ||
} | ||
|
||
if len(filename) > 0 { | ||
fmt.Printf("%s:%d: ", filename, linenum) | ||
} | ||
msg := fmt.Sprintf(format, a...) | ||
if msg[len(msg)-1] != '\n' { | ||
msg += "\n" | ||
} | ||
fmt.Print(msg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters