-
Notifications
You must be signed in to change notification settings - Fork 20
/
gen_main.go
165 lines (142 loc) · 4.17 KB
/
gen_main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package main
import (
"github.com/DQNEO/minigo/stdlib/strings"
"github.com/DQNEO/minigo/stdlib/io/ioutil"
"github.com/DQNEO/minigo/util"
)
// builtin string
var builtinStringKey1 string = string("SfmtDumpInterface")
var builtinStringValue1 string = string("# interface = {ptr:%p,receiverTypeId:%d,dtype:'%s'}")
var builtinStringKey2 string = string("SfmtDumpSlice")
var builtinStringValue2 string = string("# slice = {underlying:%p,len:%d,cap:%d}")
func (program *Program) emitSpecialStrings() {
// https://sourceware.org/binutils/docs-2.30/as/Data.html#Data
emit(".data 0")
emit("# special strings")
// emit builtin string
emitWithoutIndent(".%s:", builtinStringKey1)
emit(".string \"%s\"", builtinStringValue1)
emitWithoutIndent(".%s:", builtinStringKey2)
emit(".string \"%s\"", builtinStringValue2)
}
func (program *Program) emitDynamicTypes() {
emitNewline()
emit("# Dynamic Types")
for dynamicTypeId, gs := range symbolTable.uniquedDTypes {
label := makeDynamicTypeLabel(dynamicTypeId)
emitWithoutIndent(".%s:", label)
emit(".string \"%s\"", gs)
}
}
func (program *Program) emitMethodTable() {
emitWithoutIndent("#--------------------------------------------------------")
emit("# Method table")
emit(".data 0")
emitWithoutIndent("%s:", "receiverTypes")
emit(".quad 0 # receiverTypeId:0")
var maxId int
var i int
var id int
for id, _ = range program.methodTable {
if maxId < id {
maxId = id
}
}
for i = 1; i <= maxId; i++ {
_, ok := program.methodTable[i]
if ok {
emit(".quad receiverType%d # receiverTypeId:%d", i, i)
} else {
emit(".quad 0")
}
}
var shortMethodNames []string
for i, v := range program.methodTable {
emitWithoutIndent("receiverType%d:", i)
methods := v
for _, methodNameFull := range methods {
if methodNameFull == "." {
panic("invalid method name")
}
splitted := strings.Split(methodNameFull, "$")
var shortMethodName string = splitted[1]
emit(".quad .S.S.%s # key", shortMethodName)
label := makeLabel()
gasIndentLevel++
emit(".data 1")
emit("%s:", label)
emit(".quad %s # func addr", methodNameFull)
gasIndentLevel--
emit(".data 0")
emit(".quad %s # func addr addr", label)
if !util.InArray(shortMethodName, shortMethodNames) {
shortMethodNames = append(shortMethodNames, shortMethodName)
}
}
}
emitWithoutIndent("#--------------------------------------------------------")
emitWithoutIndent("# Short method names")
for _, shortMethodName := range shortMethodNames {
emit(".data 0")
emit(".S.S.%s:", shortMethodName)
gasIndentLevel++
emit(".data 1")
emit(".S.%s:", shortMethodName)
emit(".quad 0") // Any value is ok. This is not referred to.
gasIndentLevel--
emit(".data 0")
emit(".quad .S.%s", shortMethodName)
}
}
// generate code
func (program *Program) emit() {
var buf []byte
// insert macro
buf, _ = ioutil.ReadFile("./macro.s")
emitBuf(buf)
// insert runtime asm codes
for _, txt := range pkgIRuntime.asm {
emitBuf([]byte(txt))
}
emit(".data 0")
program.emitSpecialStrings()
program.emitDynamicTypes()
program.emitMethodTable()
emitWithoutIndent(".text")
emitMainFunc(program.packages)
// emit packages
for _, pkg := range program.packages {
emitWithoutIndent("#--------------------------------------------------------")
emitWithoutIndent("# package %s:%s", string(pkg.normalizedPath), pkg.name)
emitWithoutIndent("# string literals")
emitWithoutIndent(".data 0")
for _, ast := range pkg.stringLiterals {
emitWithoutIndent(".%s:", ast.slabel)
// https://sourceware.org/binutils/docs-2.30/as/String.html#String
// the assembler marks the end of each string with a 0 byte.
emit(".string \"%s\"", ast.val)
}
for _, vardecl := range pkg.vars {
emitNewline()
vardecl.emit()
}
emitNewline()
emitWithoutIndent(".text")
for _, funcdecl := range pkg.funcs {
funcdecl.emit()
emitNewline()
}
}
}
func emitMainFunc(packages []*AstPackage) {
emitWithoutIndent("_init_packages:")
// init imported packages
for _, pkg := range packages {
if pkg.hasInit {
emit("# init %s", string(pkg.normalizedPath))
emit("FUNCALL %s", getFuncSymbol(pkg.normalizedPath, "init"))
}
}
emit("jmp iruntime.main")
emitNewline()
}