func main() { goopt.Parse(func() []string { return nil }) if len(goopt.Args) > 0 { x, err := parser.ParseFiles(myfiles, goopt.Args, 0) die(err) fmt.Fprintln(os.Stderr, "Parsed: ", *x["main"]) die(typechecker.CheckPackage(myfiles, x["main"], nil)) fmt.Fprintln(os.Stderr, "Checked: ", *x["main"]) //for _,a := range x["main"].Files { // die(printer.Fprint(os.Stdout, a)) //} aaa := x86.StartData var bbb *Stack var cv = CompileVisitor{&aaa, make(map[string]string), bbb.New("global")} ast.Walk(StringVisitor(cv), x["main"]) cv.Append(x86.StartText...) ast.Walk(&cv, x["main"]) // Here we just add a crude debug library cv.Append(x86.Debugging...) ass := x86.Assembly(*cv.assembly) //fmt.Println(ass) die(elf.AssembleAndLink(goopt.Args[0][:len(goopt.Args[0])-3], []byte(ass))) } }
// PopTo returns code to save data from the stack into the variable. // It also changes the stack size accordingly. func (s *Stack) PopTo(name string) x86.X86 { v := s.Lookup(name) off := SizeOnStack(v.Type()) if TypeToSize(v.Type()) != off { panic("I can't yet handle types with sizes that aren't a multiple of 4") } comment := "Popping to variable " + v.Name() if v.Name() == "_" { comment = fmt.Sprint("Popping to ", name, " of type ", PrettyType(v.Type()), " at ", v.InMemory()) } code := []x86.X86{x86.Comment(s.PrettyComments())} switch off { case 4: s.Size -= off vnew := s.Lookup(name) // Its location relative to stack may have changed! return x86.RawAssembly(x86.Assembly([]x86.X86{ x86.PopL(x86.EAX), x86.Commented(x86.MovL(x86.EAX, vnew.InMemory()), comment), })) case 8: s.Size -= 4 vnew := s.Lookup(name) // Its location relative to stack may have changed! code = append(code, x86.PopL(x86.EAX), x86.Commented(x86.MovL(x86.EAX, vnew.InMemory()), comment)) s.Size -= 4 vnew = s.Lookup(name) // Its location relative to stack may have changed again! code = append(code, x86.PopL(x86.EAX), x86.Commented(x86.MovL(x86.EAX, vnew.InMemory().Add(4)), comment)) return x86.RawAssembly(x86.Assembly(code)) default: panic(fmt.Sprintf("I don't pop variables with length %s", SizeOnStack(v.Type()))) } panic("This can't happen") }