Skip to content

Commit

Permalink
WIP: more work on pointers.
Browse files Browse the repository at this point in the history
  • Loading branch information
markkurossi committed Jul 31, 2021
1 parent 6fe4017 commit d196fa9
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 120 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,7 @@ form assembly:
- [ ] local variables in for-loop unrolling
- [ ] Pointer handling
- [ ] Pointer to struct field
- [ ] Compound init values must be zero-padded to full size
- [X] Method calls
- [ ] Signed / unsigned arithmetics
- [X] `byte` type
Expand Down
46 changes: 46 additions & 0 deletions compiler/ast/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,52 @@ func (ctx *Codegen) LookupFunc(block *ssa.Block, ref *VariableRef) (
return called, nil
}

// LookupVar resolved the named variable from the context.
func (ctx *Codegen) LookupVar(bindings *ssa.Bindings, ref *VariableRef) (
ssa.Binding, bool, error) {

var b ssa.Binding
var ok bool

if len(ref.Name.Package) > 0 {
// Check if package name is bound to a value.
b, ok = bindings.Get(ref.Name.Package)
if !ok {
// Check names in the current package.
b, ok = ctx.Package.Bindings.Get(ref.Name.Package)
}
if ok {
return b, true, nil
}
}

// Name from environment bindings.
b, ok = bindings.Get(ref.Name.Name)
if ok {
return b, ok, nil
}

// Global symbols from the package.
var pkgName string
if len(ref.Name.Package) > 0 {
pkgName = ref.Name.Package
} else {
pkgName = ref.Name.Defined
}
pkg, ok := ctx.Packages[pkgName]
if !ok {
return ssa.Binding{}, false,
ctx.Errorf(ref, "package '%s' not found", pkgName)
}
b, ok = pkg.Bindings.Get(ref.Name.Name)
if !ok {
return ssa.Binding{}, false,
ctx.Errorf(ref, "undefined variable '%s'", ref.Name)
}

return b, true, nil
}

// Func returns the current function in the current compilation.
func (ctx *Codegen) Func() *Func {
if len(ctx.Stack) == 0 {
Expand Down
40 changes: 6 additions & 34 deletions compiler/ast/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,40 +398,9 @@ func (ast *Index) Eval(env *Env, ctx *Codegen, gen *ssa.Generator) (
func (ast *VariableRef) Eval(env *Env, ctx *Codegen,
gen *ssa.Generator) (ssa.Value, bool, error) {

var b ssa.Binding
var ok bool

// Check if package name is bound to variable.
b, ok = env.Get(ast.Name.Package)
if ok {
// Bound. We are selecting value from its value.
val, ok := b.Bound.(*ssa.Value)
if !ok || !val.Const {
return ssa.Undefined, false, nil
}
return ssa.Undefined, false, ctx.Errorf(ast,
"VariableRef.Eval: select not implemented yet")
}

if len(ast.Name.Package) > 0 {
var pkg *Package
pkg, ok = ctx.Packages[ast.Name.Package]
if !ok {
return ssa.Undefined, false, ctx.Errorf(ast,
"package '%s' not found", ast.Name.Package)
}
b, ok = pkg.Bindings.Get(ast.Name.Name)
} else {
// First check env bindings.
b, ok = env.Get(ast.Name.Name)
if !ok {
// Check names in the current package.
b, ok = ctx.Package.Bindings.Get(ast.Name.Name)
}
}
if !ok {
return ssa.Undefined, false, ctx.Errorf(ast, "undefined variable '%s'",
ast.Name.String())
b, ok, err := ctx.LookupVar(env.Bindings, ast)
if !ok || err != nil {
return ssa.Undefined, ok, err
}

val, ok := b.Bound.(*ssa.Value)
Expand All @@ -452,6 +421,9 @@ func (ast *BasicLit) Eval(env *Env, ctx *Codegen, gen *ssa.Generator) (
func (ast *CompositeLit) Eval(env *Env, ctx *Codegen, gen *ssa.Generator) (
ssa.Value, bool, error) {

// XXX the init values might be short so we must pad them with
// zero values so that we create correctly sized values.

typeInfo, err := ast.Type.Resolve(env, ctx, gen)
if err != nil {
return ssa.Undefined, false, err
Expand Down
Loading

0 comments on commit d196fa9

Please sign in to comment.