-
Notifications
You must be signed in to change notification settings - Fork 547
/
goinstr.go
343 lines (315 loc) · 9.03 KB
/
goinstr.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
Copyright 2020 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cl
import (
"log"
"reflect"
"github.com/goplus/gop/ast"
"github.com/goplus/gop/exec.spec"
)
// -----------------------------------------------------------------------------
type goInstr = func(ctx *blockCtx, v *ast.CallExpr, ct callType) func()
type goInstrInfo struct {
instr goInstr
}
var goinstrs map[string]goInstrInfo
func init() {
goinstrs = map[string]goInstrInfo{
"append": {igoAppend},
"copy": {igoCopy},
"delete": {igoDelete},
"len": {igoLen},
"cap": {igoCap},
"make": {igoMake},
"new": {igoNew},
"complex": {igoComplex},
"real": {igoReal},
"imag": {igoImag},
"close": {igoClose},
"recover": {igoRecover},
}
}
// -----------------------------------------------------------------------------
/*
func checkSliceType(args []interface{}, i int) (typ reflect.Type, ok bool) {
if i >= len(args) {
return
}
typ = args[i].(iValue).Type()
ok = typ.Kind() == reflect.Slice
return
}
*/
// func append(slice []Type, elems ...Type) []Type
func igoAppend(ctx *blockCtx, v *ast.CallExpr, ct callType) func() {
if ct != callExpr {
log.Panicf("%s discards result of %s\n", gCallTypes[ct], ctx.code(v))
}
if len(v.Args) < 2 {
log.Panicln("append: argument count not enough")
}
sliceExpr := compileExpr(ctx, v.Args[0])
sliceTy := ctx.infer.Get(-1).(iValue).Type()
if sliceTy.Kind() != reflect.Slice {
log.Panicln("append: first argument not a slice")
}
return func() {
sliceExpr()
n1 := len(v.Args) - 1
for i := 1; i <= n1; i++ {
compileExpr(ctx, v.Args[i])()
}
args := ctx.infer.GetArgs(n1)
elem := sliceTy.Elem()
if isEllipsis(v) {
if n1 != 1 {
log.Panicln("append: please use `append(slice, elems...)`")
}
checkType(sliceTy, args[0], ctx.out)
ctx.infer.PopN(1)
ctx.out.Append(elem, -1)
} else {
checkElementType(elem, args, 0, n1, 1, ctx.out)
ctx.infer.PopN(n1)
ctx.out.Append(elem, n1+1)
}
}
}
// func copy(dst, src []Type) int
func igoCopy(ctx *blockCtx, v *ast.CallExpr, ct callType) func() {
if len(v.Args) < 2 {
log.Panicln("not enough arguments in call to copy")
}
if len(v.Args) > 2 {
log.Panicln("too many arguments in call to copy")
}
dstExpr := compileExpr(ctx, v.Args[0])
dstTy := ctx.infer.Get(-1).(iValue).Type()
if dstTy.Kind() != reflect.Slice {
log.Panicln("arguments to copy must be slices; have ", dstTy.Kind())
}
if ct == callExpr {
ctx.infer.Ret(1, &goValue{exec.TyInt})
}
return func() {
dstExpr()
compileExpr(ctx, v.Args[1])()
srcTy := ctx.infer.Get(-1).(iValue).Type()
switch srcTy.Kind() {
case reflect.Slice:
if srcTy.Elem().Kind() != dstTy.Elem().Kind() {
log.Panicf("arguments to copy have different element types: %s(%s) %s(%s)", dstTy.Kind(), dstTy.Elem().Kind(), srcTy.Kind(), srcTy.Elem().Kind())
}
case reflect.String:
if dstTy.Elem().Kind() != reflect.Uint8 {
log.Panicln("arguments to copy have different element types:", dstTy.Kind(), srcTy.Kind())
}
default:
log.Panicln("second argument to copy should be slice or string; have", srcTy.Kind())
}
ctx.infer.Pop()
builder(ctx, ct).GoBuiltin(dstTy, exec.GobCopy)
}
}
// func delete(m map[Type]Type1, key Type)
func igoDelete(ctx *blockCtx, v *ast.CallExpr, ct callType) func() {
narg := len(v.Args)
if narg != 2 {
if narg < 2 {
log.Panicln("missing second (key) argument to delete")
}
log.Panicln("too many arguments to delete")
}
mapExpr := compileExpr(ctx, v.Args[0])
mapType := ctx.infer.Pop().(iValue).Type()
if mapType.Kind() != reflect.Map {
log.Panicln("first argument to delete must be map; have", mapType)
}
ctx.infer.Push(&nonValue{})
return func() {
mapExpr()
compileExpr(ctx, v.Args[1])()
key := ctx.infer.Pop().(iValue)
checkType(mapType.Key(), key, ctx.out)
builder(ctx, ct).GoBuiltin(mapType, exec.GobDelete)
}
}
// func len/cap(v Type) int
func igoLenOrCap(ctx *blockCtx, v *ast.CallExpr, op exec.GoBuiltin) func() {
narg := len(v.Args)
if narg != 1 {
if narg < 1 {
logPanic(ctx, v, `missing argument to %v: %v`, op, ctx.code(v))
}
logPanic(ctx, v, `too many arguments to %v: %v`, op, ctx.code(v))
}
expr := compileExpr(ctx, v.Args[0])
x := ctx.infer.Get(-1)
typ := x.(iValue).Type()
kind := typ.Kind()
switch kind {
case reflect.Array:
n := typ.Len()
ctx.infer.Ret(1, newConstVal(n, exec.Int))
return func() {
ctx.out.Push(n)
}
default:
if kind == reflect.String {
if op == exec.GobCap {
logPanic(ctx, v, `invalid argument a (type %v) for cap`, typ)
}
if cons, ok := x.(*constVal); ok {
n := len(cons.v.(string))
ctx.infer.Ret(1, newConstVal(n, exec.Int))
return func() {
ctx.out.Push(n)
}
}
}
ctx.infer.Ret(1, &goValue{t: exec.TyInt})
}
return func() {
switch kind {
case reflect.Slice, reflect.String, reflect.Map, reflect.Chan:
if op == exec.GobCap && kind == reflect.Chan {
logPanic(ctx, v, `invalid argument a (type %v) for cap`, typ)
}
expr()
ctx.out.GoBuiltin(typ, op)
default:
logPanic(ctx, v, `invalid argument a (type %v) for %v`, typ, op)
}
}
}
// func len(v Type) int
func igoLen(ctx *blockCtx, v *ast.CallExpr, ct callType) func() {
if ct != callExpr {
log.Panicf("%s discards result of %s\n", gCallTypes[ct], ctx.code(v))
}
return igoLenOrCap(ctx, v, exec.GobLen)
}
// func cap(v Type) int
func igoCap(ctx *blockCtx, v *ast.CallExpr, ct callType) func() {
if ct != callExpr {
log.Panicf("%s discards result of %s\n", gCallTypes[ct], ctx.code(v))
}
return igoLenOrCap(ctx, v, exec.GobCap)
}
// func make(t Type, size ...IntegerType) Type
func igoMake(ctx *blockCtx, v *ast.CallExpr, ct callType) func() {
if ct != callExpr {
log.Panicf("%s discards result of %s\n", gCallTypes[ct], ctx.code(v))
}
if len(v.Args) < 1 {
logPanic(ctx, v, `missing argument to make: %v`, ctx.code(v))
}
t := toType(ctx, v.Args[0])
kind := t.Kind()
switch kind {
case reflect.Slice, reflect.Map, reflect.Chan:
default:
log.Panicln("make: invalid type, must be a slice, map or chan -", t)
}
typ := t.(reflect.Type)
ctx.infer.Push(&goValue{t: typ})
return func() {
n1 := len(v.Args) - 1
for i := 1; i <= n1; i++ {
compileExpr(ctx, v.Args[i])()
checkIntType(ctx.infer.Pop(), ctx.out)
}
if kind == reflect.Slice && n1 == 0 {
log.Panicln("make slice: must specified length of slice")
}
ctx.out.Make(typ, n1)
}
}
// func new(Type) *Type
func igoNew(ctx *blockCtx, v *ast.CallExpr, ct callType) func() {
if ct != callExpr {
log.Panicf("%s discards result of %s\n", gCallTypes[ct], ctx.code(v))
}
if n := len(v.Args); n != 1 {
if n == 0 {
log.Panicln("missing argument to new")
}
log.Panicf("too many arguments to new(%v)\n", ctx.code(v.Args[0]))
}
t := toType(ctx, v.Args[0]).(reflect.Type)
ptrT := reflect.PtrTo(t)
ctx.infer.Push(&goValue{t: ptrT})
return func() {
ctx.out.New(t)
}
}
// func complex(r, i FloatType) ComplexType
func igoComplex(ctx *blockCtx, v *ast.CallExpr, ct callType) func() {
if ct != callExpr {
log.Panicf("%s discards result of %s\n", gCallTypes[ct], ctx.code(v))
}
panic("todo")
}
// func real(c ComplexType) FloatType
func igoReal(ctx *blockCtx, v *ast.CallExpr, ct callType) func() {
if ct != callExpr {
log.Panicf("%s discards result of %s\n", gCallTypes[ct], ctx.code(v))
}
panic("todo")
}
// func imag(c ComplexType) FloatType
func igoImag(ctx *blockCtx, v *ast.CallExpr, ct callType) func() {
if ct != callExpr {
log.Panicf("%s discards result of %s\n", gCallTypes[ct], ctx.code(v))
}
panic("todo")
}
// func close(c chan<- Type)
func igoClose(ctx *blockCtx, v *ast.CallExpr, ct callType) func() {
panic("todo")
}
// func recover() interface{}
func igoRecover(ctx *blockCtx, v *ast.CallExpr, ct callType) func() {
panic("todo")
}
func compileTypeCast(typ reflect.Type, ctx *blockCtx, v *ast.CallExpr) func() {
if len(v.Args) != 1 {
log.Panicln("compileTypeCast: invalid argument count, please use `type(expr)`")
}
xExpr := compileExpr(ctx, v.Args[0])
in := ctx.infer.Get(-1)
kind := typ.Kind()
if kind <= reflect.Complex128 || kind == reflect.String { // can be constant
if cons, ok := in.(*constVal); ok {
cons.kind = typ.Kind()
return func() {
pushConstVal(ctx.out, cons)
}
}
}
ctx.infer.Ret(1, &goValue{typ})
return func() {
xExpr()
iv := in.(iValue)
n := iv.NumValues()
if n != 1 {
panicExprNotValue(n)
}
tIn := iv.Type()
if !tIn.ConvertibleTo(typ) {
log.Panicf("compileTypeCast: can't convert type `%v` to `%v`\n", tIn, typ)
}
ctx.out.TypeCast(tIn, typ)
}
}
// -----------------------------------------------------------------------------