@@ -92,8 +92,9 @@ func emitPopSlice() {
92
92
printf (" popq %%rdx # slice.cap\n " )
93
93
}
94
94
95
- func emitPushStackTop (condType * types.Type , offset int , comment string ) {
96
- switch sema .Kind (condType ) {
95
+ func emitPushStackTop (t types.Type , offset int , comment string ) {
96
+ knd := sema .Kind (t )
97
+ switch knd {
97
98
case types .T_STRING :
98
99
printf (" movq %d+8(%%rsp), %%rcx # copy str.len from stack top (%s)\n " , offset , comment )
99
100
printf (" movq %d+0(%%rsp), %%rax # copy str.ptr from stack top (%s)\n " , offset , comment )
@@ -103,7 +104,7 @@ func emitPushStackTop(condType *types.Type, offset int, comment string) {
103
104
printf (" movq %d(%%rsp), %%rax # copy stack top value (%s) \n " , offset , comment )
104
105
printf (" pushq %%rax\n " )
105
106
default :
106
- unexpectedKind (sema . Kind ( condType ) )
107
+ unexpectedKind (knd )
107
108
}
108
109
}
109
110
@@ -129,7 +130,7 @@ func emitAddConst(addValue int, comment string) {
129
130
}
130
131
131
132
// "Load" means copy data from memory to registers
132
- func emitLoadAndPush (t * types.Type ) {
133
+ func emitLoadAndPush (t types.Type ) {
133
134
assert (t != nil , "type should not be nil" , __func__ )
134
135
emitPopAddress (string (sema .Kind (t )))
135
136
switch sema .Kind (t ) {
@@ -261,7 +262,7 @@ func emitAddr(meta ir.MetaExpr) {
261
262
}
262
263
263
264
// explicit conversion T(e)
264
- func emitConversion (toType * types.Type , arg0 ir.MetaExpr ) {
265
+ func emitConversion (toType types.Type , arg0 ir.MetaExpr ) {
265
266
emitComment (2 , "[emitConversion]\n " )
266
267
fromType := sema .GetTypeOfExpr (arg0 )
267
268
fromKind := sema .Kind (fromType )
@@ -309,7 +310,7 @@ func emitIfcConversion(ic *ir.IfcConversion) {
309
310
emitConvertToInterface (sema .GetTypeOfExpr (ic .Value ), ic .Type )
310
311
}
311
312
312
- func emitZeroValue (t * types.Type ) {
313
+ func emitZeroValue (t types.Type ) {
313
314
switch sema .Kind (t ) {
314
315
case types .T_SLICE :
315
316
printf (" pushq $0 # slice cap\n " )
@@ -357,13 +358,14 @@ func emitLen(arg ir.MetaExpr) {
357
358
emitCallDirect ("runtime.lenMap" , []ir.MetaExpr {arg }, sig )
358
359
359
360
default :
360
- unexpectedKind (sema .Kind (sema . GetTypeOfExpr ( arg ) ))
361
+ unexpectedKind (sema .Kind (t ))
361
362
}
362
363
}
363
364
364
365
func emitCap (arg ir.MetaExpr ) {
365
366
t := sema .GetTypeOfExpr (arg )
366
- switch sema .Kind (t ) {
367
+ knd := sema .Kind (t )
368
+ switch knd {
367
369
case types .T_ARRAY :
368
370
arrayLen := sema .GetArrayLen (t )
369
371
printf (" pushq $%d # array len\n " , arrayLen )
@@ -374,7 +376,7 @@ func emitCap(arg ir.MetaExpr) {
374
376
case types .T_STRING :
375
377
panic ("cap() cannot accept string type" )
376
378
default :
377
- unexpectedKind (sema . Kind ( sema . GetTypeOfExpr ( arg )) )
379
+ unexpectedKind (knd )
378
380
}
379
381
}
380
382
@@ -403,7 +405,7 @@ func emitStructLiteral(meta *ir.MetaCompositLit) {
403
405
emitExpr (metaElm .Value )
404
406
405
407
// assign
406
- emitStore (metaElm .FieldType , true , false )
408
+ emitStore (metaElm .Type , true , false )
407
409
}
408
410
}
409
411
@@ -477,7 +479,7 @@ func emitCallDirect(symbol string, args []ir.MetaExpr, sig *ir.Signature) {
477
479
// slc.cap
478
480
// r
479
481
// --
480
- func emitCall (fv * ir.FuncValue , args []ir.MetaExpr , paramTypes []* types.Type , returnTypes []* types.Type ) {
482
+ func emitCall (fv * ir.FuncValue , args []ir.MetaExpr , paramTypes []types.Type , returnTypes []types.Type ) {
481
483
emitComment (2 , "emitCall len(args)=%d\n " , len (args ))
482
484
var totalParamSize int
483
485
var offsets []int
@@ -518,24 +520,27 @@ func emitCall(fv *ir.FuncValue, args []ir.MetaExpr, paramTypes []*types.Type, re
518
520
}
519
521
520
522
func emitAllocReturnVarsAreaFF (ff * ir.Func ) {
521
- emitAllocReturnVarsArea (getTotalSizeOfType (ff .Signature .ReturnTypes ))
523
+ rtypes := (ff .Signature .ReturnTypes )
524
+ emitAllocReturnVarsArea (getTotalSizeOfType (rtypes ))
522
525
}
523
526
524
- func getTotalSizeOfType (types []* types.Type ) int {
527
+ func getTotalSizeOfType (ts []types.Type ) int {
525
528
var r int
526
- for _ , t := range types {
529
+ for _ , t := range ts {
527
530
r += sema .GetSizeOfType (t )
528
531
}
529
532
return r
530
533
}
531
534
532
535
func emitCallFF (ff * ir.Func ) {
533
- totalParamSize := getTotalSizeOfType (ff .Signature .ParamTypes )
536
+ ptypes := (ff .Signature .ParamTypes )
537
+ rtypes := (ff .Signature .ReturnTypes )
538
+ totalParamSize := getTotalSizeOfType (ptypes )
534
539
symbol := ff .PkgName + "." + ff .Name
535
- emitCallQ (sema .NewFuncValueFromSymbol (symbol ), totalParamSize , ff . Signature . ReturnTypes )
540
+ emitCallQ (sema .NewFuncValueFromSymbol (symbol ), totalParamSize , rtypes )
536
541
}
537
542
538
- func emitCallQ (fv * ir.FuncValue , totalParamSize int , returnTypes []* types.Type ) {
543
+ func emitCallQ (fv * ir.FuncValue , totalParamSize int , returnTypes []types.Type ) {
539
544
if fv .IsDirect {
540
545
if fv .Symbol == "" {
541
546
panic ("callq target must not be empty" )
@@ -550,7 +555,7 @@ func emitCallQ(fv *ir.FuncValue, totalParamSize int, returnTypes []*types.Type)
550
555
var methodId int
551
556
methods := sema .GetInterfaceMethods (fv .IfcType )
552
557
for i , m := range methods {
553
- if m .Names [ 0 ]. Name == fv .MethodName {
558
+ if m .Name == fv .MethodName {
554
559
methodId = i
555
560
}
556
561
}
@@ -588,7 +593,7 @@ func emitReturnStmt(meta *ir.MetaReturnStmt) {
588
593
}
589
594
590
595
// caller
591
- func emitFreeAndPushReturnedValue (returnTypes []* types.Type ) {
596
+ func emitFreeAndPushReturnedValue (returnTypes []types.Type ) {
592
597
switch len (returnTypes ) {
593
598
case 0 :
594
599
// do nothing
@@ -644,8 +649,8 @@ func emitMetaCallMake(m *ir.MetaCallMake) {
644
649
func emitMetaCallAppend (m * ir.MetaCallAppend ) {
645
650
sliceArg := m .Arg0
646
651
elemArg := m .Arg1
647
- elmType := sema .GetElementTypeOfCollectionType (sema .GetTypeOfExpr (sliceArg ))
648
- elmSize := sema .GetSizeOfType (elmType )
652
+ elmTypeG := sema .GetElementTypeOfCollectionType (sema .GetTypeOfExpr (sliceArg ))
653
+ elmSize := sema .GetSizeOfType (elmTypeG )
649
654
650
655
var symbol string
651
656
switch elmSize {
@@ -660,6 +665,7 @@ func emitMetaCallAppend(m *ir.MetaCallAppend) {
660
665
default :
661
666
throw (elmSize )
662
667
}
668
+ elmType := sema .GetElementTypeOfCollectionType (sema .GetTypeOfExpr (sliceArg ))
663
669
arg1 := sema .CheckIfcConversion (m .Pos (), elemArg , elmType )
664
670
args := []ir.MetaExpr {sliceArg , arg1 }
665
671
sig := sema .NewAppendSignature (elmType )
@@ -670,7 +676,7 @@ func emitMetaCallAppend(m *ir.MetaCallAppend) {
670
676
671
677
func emitMetaCallPanic (m * ir.MetaCallPanic ) {
672
678
funcVal := "runtime.panic"
673
- arg0 := sema .CheckIfcConversion (m .Pos (), m .Arg0 , types .Eface )
679
+ arg0 := sema .CheckIfcConversion (m .Pos (), m .Arg0 , types .EmptyInterface )
674
680
args := []ir.MetaExpr {arg0 }
675
681
emitCallDirect (funcVal , args , ir .BuiltinPanicSignature )
676
682
return
@@ -1106,7 +1112,9 @@ func emitExpr(meta ir.MetaExpr) {
1106
1112
case * ir.MetaConversionExpr :
1107
1113
emitConversion (m .Type , m .Arg0 )
1108
1114
case * ir.MetaCallExpr :
1109
- emitCall (m .FuncVal , m .Args , m .ParamTypes , m .Types ) // can be Tuple
1115
+ rtypes := m .Types
1116
+ mtypes := m .ParamTypes
1117
+ emitCall (m .FuncVal , m .Args , mtypes , rtypes ) // can be Tuple
1110
1118
case * ir.MetaIndexExpr :
1111
1119
emitIndexExpr (m ) // can be Tuple
1112
1120
case * ir.MetaSliceExpr :
@@ -1127,7 +1135,7 @@ func emitExpr(meta ir.MetaExpr) {
1127
1135
}
1128
1136
1129
1137
// convert stack top value to interface
1130
- func emitConvertToInterface (fromType * types.Type , toType * types.Type ) {
1138
+ func emitConvertToInterface (fromType types.Type , toType types.Type ) {
1131
1139
emitComment (2 , "ConversionToInterface\n " )
1132
1140
if sema .HasIfcMethod (toType ) {
1133
1141
emitComment (2 , "@@@ toType has methods\n " )
@@ -1190,8 +1198,8 @@ func emitCompareDtypes() {
1190
1198
printf (" %s:\n " , labelEnd )
1191
1199
}
1192
1200
1193
- func emitDtypeLabelAddr (t * types.Type , it * types.Type ) {
1194
- de := sema .GetITabEntry (t , it )
1201
+ func emitDtypeLabelAddr (d types.Type , i types.Type ) {
1202
+ de := sema .GetITabEntry (d , i )
1195
1203
dtypeLabel := de .Label
1196
1204
sr := de .DSerialized
1197
1205
printf (" leaq %s(%%rip), %%rax # dtype label address \" %s\" \n " , dtypeLabel , sr )
@@ -1207,7 +1215,7 @@ func emitAddrForMapSet(indexExpr *ir.MetaIndexExpr) {
1207
1215
emitCallDirect ("runtime.getAddrForMapSet" , args , ir .RuntimeGetAddrForMapSetSignature )
1208
1216
}
1209
1217
1210
- func emitListElementAddr (list ir.MetaExpr , elmType * types.Type ) {
1218
+ func emitListElementAddr (list ir.MetaExpr , elmType types.Type ) {
1211
1219
emitListHeadAddr (list )
1212
1220
emitPopAddress ("list head" )
1213
1221
printf (" popq %%rcx # index id\n " )
@@ -1239,7 +1247,7 @@ func emitBinaryExprComparison(left ir.MetaExpr, right ir.MetaExpr) {
1239
1247
emitExpr (right ) // right
1240
1248
emitCallFF (ff )
1241
1249
} else {
1242
- // Assuming 64 bit types (int, pointer, map, etc)
1250
+ // Assuming 64 bit types.Type (int, pointer, map, etc)
1243
1251
//var t = GetTypeOfExpr(left)
1244
1252
emitExpr (left ) // left
1245
1253
emitExpr (right ) // right
@@ -1250,7 +1258,7 @@ func emitBinaryExprComparison(left ir.MetaExpr, right ir.MetaExpr) {
1250
1258
1251
1259
}
1252
1260
1253
- // @TODO handle larger Types than int
1261
+ // @TODO handle larger types.Type than int
1254
1262
func emitCompExpr (inst string ) {
1255
1263
printf (" popq %%rcx # right\n " )
1256
1264
printf (" popq %%rax # left\n " )
@@ -1297,7 +1305,7 @@ func emitPop(knd types.TypeKind) {
1297
1305
}
1298
1306
}
1299
1307
1300
- func emitStore (t * types.Type , rhsTop bool , pushLhs bool ) {
1308
+ func emitStore (t types.Type , rhsTop bool , pushLhs bool ) {
1301
1309
knd := sema .Kind (t )
1302
1310
emitComment (2 , "emitStore(%s)\n " , knd )
1303
1311
if rhsTop {
@@ -1315,7 +1323,7 @@ func emitStore(t *types.Type, rhsTop bool, pushLhs bool) {
1315
1323
emitRegiToMem (t )
1316
1324
}
1317
1325
1318
- func emitRegiToMem (t * types.Type ) {
1326
+ func emitRegiToMem (t types.Type ) {
1319
1327
printf (" popq %%rsi # place to save\n " )
1320
1328
k := sema .Kind (t )
1321
1329
switch k {
@@ -1348,7 +1356,7 @@ func emitRegiToMem(t *types.Type) {
1348
1356
}
1349
1357
}
1350
1358
1351
- func emitAssignZeroValue (lhs ir.MetaExpr , lhsType * types.Type ) {
1359
+ func emitAssignZeroValue (lhs ir.MetaExpr , lhsType types.Type ) {
1352
1360
emitComment (2 , "emitAssignZeroValue\n " )
1353
1361
emitComment (2 , "lhs addresss\n " )
1354
1362
emitAddr (lhs )
@@ -1785,7 +1793,7 @@ func emitTypeSwitchStmt(meta *ir.MetaTypeSwitchStmt) {
1785
1793
// subjectVariable = subject
1786
1794
emitVariableAddr (meta .SubjectVariable )
1787
1795
emitExpr (meta .Subject )
1788
- emitStore (types .Eface , true , false )
1796
+ emitStore (types .EmptyInterface , true , false )
1789
1797
1790
1798
cases := meta .Cases
1791
1799
var labels = make ([]string , len (cases ), len (cases ))
@@ -1851,7 +1859,7 @@ func emitTypeSwitchStmt(meta *ir.MetaTypeSwitchStmt) {
1851
1859
1852
1860
// push rhs
1853
1861
emitVariableAddr (meta .SubjectVariable )
1854
- emitLoadAndPush (types .Eface )
1862
+ emitLoadAndPush (types .EmptyInterface )
1855
1863
printf (" popq %%rax # ifc.dtype\n " )
1856
1864
printf (" popq %%rcx # ifc.data\n " )
1857
1865
printf (" pushq %%rcx # ifc.data\n " )
@@ -1951,7 +1959,7 @@ func emitStmt(meta ir.MetaStmt) {
1951
1959
}
1952
1960
}
1953
1961
1954
- func emitRevertStackTop (t * types.Type ) {
1962
+ func emitRevertStackTop (t types.Type ) {
1955
1963
printf (" addq $%d, %%rsp # revert stack top\n " , sema .GetSizeOfType (t ))
1956
1964
}
1957
1965
@@ -1992,7 +2000,7 @@ func emitFuncDecl(pkgName string, fnc *ir.Func) {
1992
2000
printf (" ret\n " )
1993
2001
}
1994
2002
1995
- func emitZeroData (t * types.Type ) {
2003
+ func emitZeroData (t types.Type ) {
1996
2004
for i := 0 ; i < sema .GetSizeOfType (t ); i ++ {
1997
2005
printf (" .byte 0 # zero value\n " )
1998
2006
}
@@ -2084,24 +2092,25 @@ func GenerateDecls(pkg *ir.AnalyzedPackage, declFilePath string) {
2084
2092
}
2085
2093
2086
2094
fmt .Fprintf (fout , "package %s\n " , pkg .Name )
2087
-
2088
2095
// list import
2089
2096
for _ , im := range pkg .Imports {
2090
2097
fmt .Fprintf (fout , "import \" %s\" \n " , im )
2091
2098
}
2092
2099
// Type, Con, Var, Func
2093
2100
for _ , typ := range pkg .Types {
2094
- ut := sema .GetUnderlyingType (typ )
2095
- fmt .Fprintf (fout , "type %s %s\n " , typ .Name , sema .SerializeType (ut , false ))
2101
+ ut := typ .Underlying ()
2102
+ utAsString := sema .SerializeType (ut , true , pkg .Name )
2103
+ named := typ .(* types.Named )
2104
+ fmt .Fprintf (fout , "type %s %s\n " , named .String (), utAsString )
2096
2105
}
2097
2106
for _ , vr := range pkg .Vars {
2098
- fmt .Fprintf (fout , "var %s %s\n " , vr .Name .Name , sema .SerializeType (vr .Type , false ))
2107
+ fmt .Fprintf (fout , "var %s %s\n " , vr .Name .Name , sema .SerializeType (vr .Type , true , pkg . Name ))
2099
2108
}
2100
2109
for _ , cnst := range pkg .Consts {
2101
- fmt .Fprintf (fout , "const %s %s = %s\n " , cnst .Name .Name , sema .SerializeType (cnst .Type , false ), sema .GetConstRawValue (cnst .MetaVal ))
2110
+ fmt .Fprintf (fout , "const %s %s = %s\n " , cnst .Name .Name , sema .SerializeType (cnst .Type , true , pkg . Name ), sema .GetConstRawValue (cnst .MetaVal ))
2102
2111
}
2103
2112
for _ , fnc := range pkg .Funcs {
2104
- fmt .Fprintf (fout , "%s\n " , sema .RestoreFuncDecl (fnc ))
2113
+ fmt .Fprintf (fout , "%s\n " , sema .RestoreFuncDecl (fnc , true , pkg . Name ))
2105
2114
}
2106
2115
2107
2116
fout .Close ()
@@ -2191,20 +2200,20 @@ func emitInterfaceTables(itab map[string]*sema.ITabEntry) {
2191
2200
2192
2201
methods := sema .GetInterfaceMethods (ent .Itype )
2193
2202
if len (methods ) == 0 {
2194
- printf (" # no methods for %s \n " , sema . SerializeType ( ent . Itype , true ) )
2203
+ printf (" # no methods\n " )
2195
2204
}
2196
2205
for mi , m := range methods {
2197
- dmethod := sema .LookupMethod (ent .Dtype , m .Names [ 0 ] )
2206
+ dmethod := sema .LookupMethod (ent .Dtype , m .Name )
2198
2207
sym := sema .GetMethodSymbol (dmethod )
2199
- printf (" .quad .method_name_%d_%d # %s \n " , id , mi , m .Names [ 0 ]. Name )
2200
- printf (" .quad %d # method name len\n " , len (m .Names [ 0 ]. Name ))
2201
- printf (" .quad %s # method ref %s\n " , sym , m .Names [ 0 ]. Name )
2208
+ printf (" .quad .method_name_%d_%d # %s \n " , id , mi , m .Name )
2209
+ printf (" .quad %d # method name len\n " , len (m .Name ))
2210
+ printf (" .quad %s # method ref %s\n " , sym , m .Name )
2202
2211
}
2203
2212
printf (" .quad 0 # End of methods\n " )
2204
2213
2205
2214
for mi , m := range methods {
2206
2215
printf (".method_name_%d_%d:\n " , id , mi )
2207
- printf (" .string \" %s\" \n " , m .Names [ 0 ]. Name )
2216
+ printf (" .string \" %s\" \n " , m .Name )
2208
2217
}
2209
2218
}
2210
2219
printf ("\n " )
0 commit comments