-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathgen_globalvar.go
222 lines (209 loc) · 6.58 KB
/
gen_globalvar.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import "github.com/DQNEO/minigo/stdlib/fmt"
func (vr *ExprVariable) globalSymbol() string {
assert(vr.isGlobal, vr.token(), " Not global var")
return fmt.Sprintf("%s.%s", string(vr.pkg), string(vr.varname))
}
// gloabal var which should be initialized with zeros
// https://en.wikipedia.org/wiki/.bss
func (decl *DeclVar) emitBss() {
emit(".data")
// https://sourceware.org/binutils/docs-2.30/as/Lcomm.html#Lcomm
emit(".lcomm %s, %d", decl.variable.globalSymbol(), decl.variable.getGtype().getSize())
}
func (decl *DeclVar) emitData() {
ptok := decl.token()
gtype := decl.variable.gtype
right := decl.initval
emitWithoutIndent("%s: # gtype=%s", decl.variable.globalSymbol(), gtype.String())
emitWithoutIndent("# right.gtype = %s", right.getGtype().String())
emitWithoutIndent(".data 0")
doEmitData(ptok, right.getGtype(), right, "", 0)
}
func (e *ExprStructLiteral) lookup(fieldname identifier) Expr {
for _, field := range e.fields {
if string(field.key) == string(fieldname) {
return field.value
}
}
return nil
}
func doEmitData(ptok *Token /* left type */, gtype *Gtype, value /* nullable */ Expr, containerName string, depth int) {
value = unwrapRel(value)
emit("# doEmitData: containerName=%s, depth=%d", containerName, depth)
primType := gtype.getKind()
if primType == G_ARRAY {
arrayliteral, ok := value.(*ExprArrayLiteral)
var values []Expr
if ok {
values = arrayliteral.values
}
assert(ok || arrayliteral == nil, ptok, "*ExprArrayLiteral expected, but got ")
elmType := gtype.elementType
assertNotNil(elmType != nil, nil)
for i := 0; i < gtype.length; i++ {
var selector string
if i >= len(values) {
// zero value
doEmitData(ptok, elmType, nil, "", depth)
} else {
value := arrayliteral.values[i]
assertNotNil(value != nil, nil)
size := elmType.getSize()
if size == 8 {
switch value.(type) {
case *ExprUop:
uop := value.(*ExprUop)
operand := unwrapRel(uop.operand)
vr, ok := operand.(*ExprVariable)
assert(ok, uop.token(), "only variable is allowed")
emit(".quad %s # %s %s", vr.globalSymbol(), value.getGtype().String(), selector)
case *ExprVariable:
assert(false, value.token(), "variable here is not allowed")
default:
emit(".quad %d # %s %s", evalIntExpr(value), value.getGtype().String(), selector)
}
} else if size == 1 {
emit(".byte %d", evalIntExpr(value))
} else {
doEmitData(ptok, gtype.elementType, value, selector, depth)
}
}
}
emit(".quad 0 # nil terminator")
} else if primType == G_SLICE || primType == G_STRING {
switch value.(type) {
case nil:
emit(".quad 0")
emit(".quad 0")
emit(".quad 0")
case *ExprNilLiteral:
emit(".quad 0")
emit(".quad 0")
emit(".quad 0")
case *ExprSliceLiteral:
// initialize a hidden array
lit := value.(*ExprSliceLiteral)
arrayLiteral := &ExprArrayLiteral{
gtype: lit.invisiblevar.gtype,
values: lit.values,
}
emitDataAddr(arrayLiteral, depth) // emit underlying array
emit(".quad %d", lit.invisiblevar.gtype.length) // len
emit(".quad %d", lit.invisiblevar.gtype.length) // cap
case *ExprStringLiteral:
stringLiteral := value.(*ExprStringLiteral)
emit(".quad .%s", stringLiteral.slabel)
var length int = len(stringLiteral.val)
emit(".quad %d", length)
emit(".quad %d", length)
case *ExprFuncallOrConversion:
call := value.(*ExprFuncallOrConversion)
assert(call.rel.gtype != nil, value.token(), "should be Conversion")
stringLiteral, ok := call.args[0].(*ExprStringLiteral)
assert(ok, call.token(), "arg0 should be stringliteral")
emit(".quad .%s", stringLiteral.slabel)
var length int = len(stringLiteral.val)
emit(".quad %d", length)
emit(".quad %d", length)
default:
TBI(ptok, "unable to handle gtype %s", gtype.String())
}
} else if primType == G_INTERFACE {
emit(".quad 0")
emit(".quad 0")
emit(".quad 0")
} else if primType == G_BOOL {
if value == nil {
// zero value
emit(".quad 0 # %s %s", gtype.String(), containerName)
return
}
var val int = evalIntExpr(value)
emit(".quad %d # %s %s", val, gtype.String(), containerName)
} else if primType == G_STRUCT {
s := string(containerName) + "." + string(gtype.relation.name)
containerName = s
for _, field := range gtype.relation.gtype.fields {
emit("# padding=%d", field.padding)
switch field.padding {
case 1:
emit(".byte 0 # padding")
case 4:
emit(".double 0 # padding")
case 8:
emit(".quad 0 # padding")
default:
}
emit("# field:offesr=%d, fieldname=%s", field.offset, field.fieldname)
if value == nil {
s2 := string(containerName) + "." + string(field.fieldname)
doEmitData(ptok, field, nil, s2, depth)
continue
}
structLiteral, ok := value.(*ExprStructLiteral)
assert(ok, nil, "ok")
value := structLiteral.lookup(field.fieldname)
if value == nil {
// zero value
//continue
}
gtype := field
s3 := string(containerName) + "." + string(field.fieldname)
doEmitData(ptok, gtype, value, s3, depth)
}
} else {
var val int
var gtypeString string = gtype.String()
switch value.(type) {
case nil:
emit(".quad %d # %s %s zero value", val, gtypeString, containerName)
case *ExprNumberLiteral:
val = value.(*ExprNumberLiteral).val
emit(".quad %d # %s %s", val, gtypeString, containerName)
case *ExprConstVariable:
cnst := value.(*ExprConstVariable)
val = evalIntExpr(cnst)
emit(".quad %d # %s ", val, gtypeString)
case *ExprVariable:
vr := value.(*ExprVariable)
val = evalIntExpr(vr)
emit(".quad %d # %s ", val, gtypeString)
case *ExprBinop:
val = evalIntExpr(value)
emit(".quad %d # %s ", val, gtypeString)
case *ExprUop:
uop := value.(*ExprUop)
assert(uop.op == "&", ptok, "only uop & is allowed")
operand := unwrapRel(uop.operand)
vr, ok := operand.(*ExprVariable)
if ok {
assert(vr.isGlobal, value.token(), "operand should be a global variable")
emit(".quad %s", vr.globalSymbol())
} else {
// var gv = &Struct{_}
emitDataAddr(operand, depth)
}
default:
TBI(ptok, "unable to handle %d", primType)
}
}
}
// this logic is stolen from 8cc.
func emitDataAddr(operand Expr, depth int) {
emit(".data %d", depth+1)
label := makeLabel()
emit("%s:", label)
doEmitData(nil, operand.getGtype(), operand, "", depth+1)
emit(".data %d", depth)
emit(".quad %s", label)
}
func (decl *DeclVar) emitGlobal() {
emitWithoutIndent("# emitGlobal for %s", decl.variable.globalSymbol())
assertNotNil(decl.variable.gtype != nil, nil)
if decl.initval == nil {
decl.emitBss()
} else {
decl.emitData()
}
}