Skip to content

Commit f8a44ce

Browse files
authored
Merge pull request #112 from DQNEO/gotype
Redesign Types
2 parents 6607e4f + 4c261d7 commit f8a44ce

File tree

7 files changed

+902
-755
lines changed

7 files changed

+902
-755
lines changed

internal/builder/builder.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/DQNEO/babygo/internal/compiler"
88
"github.com/DQNEO/babygo/internal/sema"
9-
"github.com/DQNEO/babygo/internal/types"
109
"github.com/DQNEO/babygo/internal/universe"
1110
"github.com/DQNEO/babygo/internal/util"
1211
"github.com/DQNEO/babygo/lib/ast"
@@ -20,10 +19,10 @@ import (
2019

2120
func init() {
2221
// Check object addresses
23-
tIdent := types.Int.E.(*ast.Ident)
24-
if tIdent.Obj != universe.Int {
25-
panic("object mismatch")
26-
}
22+
//tIdent := types.Int.E.(*ast.Ident)
23+
//if tIdent.Obj != universe.Int {
24+
// panic("object mismatch")
25+
//}
2726

2827
}
2928

internal/codegen/codegen.go

Lines changed: 57 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,9 @@ func emitPopSlice() {
9292
printf(" popq %%rdx # slice.cap\n")
9393
}
9494

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 {
9798
case types.T_STRING:
9899
printf(" movq %d+8(%%rsp), %%rcx # copy str.len from stack top (%s)\n", offset, comment)
99100
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) {
103104
printf(" movq %d(%%rsp), %%rax # copy stack top value (%s) \n", offset, comment)
104105
printf(" pushq %%rax\n")
105106
default:
106-
unexpectedKind(sema.Kind(condType))
107+
unexpectedKind(knd)
107108
}
108109
}
109110

@@ -129,7 +130,7 @@ func emitAddConst(addValue int, comment string) {
129130
}
130131

131132
// "Load" means copy data from memory to registers
132-
func emitLoadAndPush(t *types.Type) {
133+
func emitLoadAndPush(t types.Type) {
133134
assert(t != nil, "type should not be nil", __func__)
134135
emitPopAddress(string(sema.Kind(t)))
135136
switch sema.Kind(t) {
@@ -261,7 +262,7 @@ func emitAddr(meta ir.MetaExpr) {
261262
}
262263

263264
// explicit conversion T(e)
264-
func emitConversion(toType *types.Type, arg0 ir.MetaExpr) {
265+
func emitConversion(toType types.Type, arg0 ir.MetaExpr) {
265266
emitComment(2, "[emitConversion]\n")
266267
fromType := sema.GetTypeOfExpr(arg0)
267268
fromKind := sema.Kind(fromType)
@@ -309,7 +310,7 @@ func emitIfcConversion(ic *ir.IfcConversion) {
309310
emitConvertToInterface(sema.GetTypeOfExpr(ic.Value), ic.Type)
310311
}
311312

312-
func emitZeroValue(t *types.Type) {
313+
func emitZeroValue(t types.Type) {
313314
switch sema.Kind(t) {
314315
case types.T_SLICE:
315316
printf(" pushq $0 # slice cap\n")
@@ -357,13 +358,14 @@ func emitLen(arg ir.MetaExpr) {
357358
emitCallDirect("runtime.lenMap", []ir.MetaExpr{arg}, sig)
358359

359360
default:
360-
unexpectedKind(sema.Kind(sema.GetTypeOfExpr(arg)))
361+
unexpectedKind(sema.Kind(t))
361362
}
362363
}
363364

364365
func emitCap(arg ir.MetaExpr) {
365366
t := sema.GetTypeOfExpr(arg)
366-
switch sema.Kind(t) {
367+
knd := sema.Kind(t)
368+
switch knd {
367369
case types.T_ARRAY:
368370
arrayLen := sema.GetArrayLen(t)
369371
printf(" pushq $%d # array len\n", arrayLen)
@@ -374,7 +376,7 @@ func emitCap(arg ir.MetaExpr) {
374376
case types.T_STRING:
375377
panic("cap() cannot accept string type")
376378
default:
377-
unexpectedKind(sema.Kind(sema.GetTypeOfExpr(arg)))
379+
unexpectedKind(knd)
378380
}
379381
}
380382

@@ -403,7 +405,7 @@ func emitStructLiteral(meta *ir.MetaCompositLit) {
403405
emitExpr(metaElm.Value)
404406

405407
// assign
406-
emitStore(metaElm.FieldType, true, false)
408+
emitStore(metaElm.Type, true, false)
407409
}
408410
}
409411

@@ -477,7 +479,7 @@ func emitCallDirect(symbol string, args []ir.MetaExpr, sig *ir.Signature) {
477479
// slc.cap
478480
// r
479481
// --
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) {
481483
emitComment(2, "emitCall len(args)=%d\n", len(args))
482484
var totalParamSize int
483485
var offsets []int
@@ -518,24 +520,27 @@ func emitCall(fv *ir.FuncValue, args []ir.MetaExpr, paramTypes []*types.Type, re
518520
}
519521

520522
func emitAllocReturnVarsAreaFF(ff *ir.Func) {
521-
emitAllocReturnVarsArea(getTotalSizeOfType(ff.Signature.ReturnTypes))
523+
rtypes := (ff.Signature.ReturnTypes)
524+
emitAllocReturnVarsArea(getTotalSizeOfType(rtypes))
522525
}
523526

524-
func getTotalSizeOfType(types []*types.Type) int {
527+
func getTotalSizeOfType(ts []types.Type) int {
525528
var r int
526-
for _, t := range types {
529+
for _, t := range ts {
527530
r += sema.GetSizeOfType(t)
528531
}
529532
return r
530533
}
531534

532535
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)
534539
symbol := ff.PkgName + "." + ff.Name
535-
emitCallQ(sema.NewFuncValueFromSymbol(symbol), totalParamSize, ff.Signature.ReturnTypes)
540+
emitCallQ(sema.NewFuncValueFromSymbol(symbol), totalParamSize, rtypes)
536541
}
537542

538-
func emitCallQ(fv *ir.FuncValue, totalParamSize int, returnTypes []*types.Type) {
543+
func emitCallQ(fv *ir.FuncValue, totalParamSize int, returnTypes []types.Type) {
539544
if fv.IsDirect {
540545
if fv.Symbol == "" {
541546
panic("callq target must not be empty")
@@ -550,7 +555,7 @@ func emitCallQ(fv *ir.FuncValue, totalParamSize int, returnTypes []*types.Type)
550555
var methodId int
551556
methods := sema.GetInterfaceMethods(fv.IfcType)
552557
for i, m := range methods {
553-
if m.Names[0].Name == fv.MethodName {
558+
if m.Name == fv.MethodName {
554559
methodId = i
555560
}
556561
}
@@ -588,7 +593,7 @@ func emitReturnStmt(meta *ir.MetaReturnStmt) {
588593
}
589594

590595
// caller
591-
func emitFreeAndPushReturnedValue(returnTypes []*types.Type) {
596+
func emitFreeAndPushReturnedValue(returnTypes []types.Type) {
592597
switch len(returnTypes) {
593598
case 0:
594599
// do nothing
@@ -644,8 +649,8 @@ func emitMetaCallMake(m *ir.MetaCallMake) {
644649
func emitMetaCallAppend(m *ir.MetaCallAppend) {
645650
sliceArg := m.Arg0
646651
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)
649654

650655
var symbol string
651656
switch elmSize {
@@ -660,6 +665,7 @@ func emitMetaCallAppend(m *ir.MetaCallAppend) {
660665
default:
661666
throw(elmSize)
662667
}
668+
elmType := sema.GetElementTypeOfCollectionType(sema.GetTypeOfExpr(sliceArg))
663669
arg1 := sema.CheckIfcConversion(m.Pos(), elemArg, elmType)
664670
args := []ir.MetaExpr{sliceArg, arg1}
665671
sig := sema.NewAppendSignature(elmType)
@@ -670,7 +676,7 @@ func emitMetaCallAppend(m *ir.MetaCallAppend) {
670676

671677
func emitMetaCallPanic(m *ir.MetaCallPanic) {
672678
funcVal := "runtime.panic"
673-
arg0 := sema.CheckIfcConversion(m.Pos(), m.Arg0, types.Eface)
679+
arg0 := sema.CheckIfcConversion(m.Pos(), m.Arg0, types.EmptyInterface)
674680
args := []ir.MetaExpr{arg0}
675681
emitCallDirect(funcVal, args, ir.BuiltinPanicSignature)
676682
return
@@ -1106,7 +1112,9 @@ func emitExpr(meta ir.MetaExpr) {
11061112
case *ir.MetaConversionExpr:
11071113
emitConversion(m.Type, m.Arg0)
11081114
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
11101118
case *ir.MetaIndexExpr:
11111119
emitIndexExpr(m) // can be Tuple
11121120
case *ir.MetaSliceExpr:
@@ -1127,7 +1135,7 @@ func emitExpr(meta ir.MetaExpr) {
11271135
}
11281136

11291137
// convert stack top value to interface
1130-
func emitConvertToInterface(fromType *types.Type, toType *types.Type) {
1138+
func emitConvertToInterface(fromType types.Type, toType types.Type) {
11311139
emitComment(2, "ConversionToInterface\n")
11321140
if sema.HasIfcMethod(toType) {
11331141
emitComment(2, "@@@ toType has methods\n")
@@ -1190,8 +1198,8 @@ func emitCompareDtypes() {
11901198
printf(" %s:\n", labelEnd)
11911199
}
11921200

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)
11951203
dtypeLabel := de.Label
11961204
sr := de.DSerialized
11971205
printf(" leaq %s(%%rip), %%rax # dtype label address \"%s\"\n", dtypeLabel, sr)
@@ -1207,7 +1215,7 @@ func emitAddrForMapSet(indexExpr *ir.MetaIndexExpr) {
12071215
emitCallDirect("runtime.getAddrForMapSet", args, ir.RuntimeGetAddrForMapSetSignature)
12081216
}
12091217

1210-
func emitListElementAddr(list ir.MetaExpr, elmType *types.Type) {
1218+
func emitListElementAddr(list ir.MetaExpr, elmType types.Type) {
12111219
emitListHeadAddr(list)
12121220
emitPopAddress("list head")
12131221
printf(" popq %%rcx # index id\n")
@@ -1239,7 +1247,7 @@ func emitBinaryExprComparison(left ir.MetaExpr, right ir.MetaExpr) {
12391247
emitExpr(right) // right
12401248
emitCallFF(ff)
12411249
} else {
1242-
// Assuming 64 bit types (int, pointer, map, etc)
1250+
// Assuming 64 bit types.Type (int, pointer, map, etc)
12431251
//var t = GetTypeOfExpr(left)
12441252
emitExpr(left) // left
12451253
emitExpr(right) // right
@@ -1250,7 +1258,7 @@ func emitBinaryExprComparison(left ir.MetaExpr, right ir.MetaExpr) {
12501258

12511259
}
12521260

1253-
// @TODO handle larger Types than int
1261+
// @TODO handle larger types.Type than int
12541262
func emitCompExpr(inst string) {
12551263
printf(" popq %%rcx # right\n")
12561264
printf(" popq %%rax # left\n")
@@ -1297,7 +1305,7 @@ func emitPop(knd types.TypeKind) {
12971305
}
12981306
}
12991307

1300-
func emitStore(t *types.Type, rhsTop bool, pushLhs bool) {
1308+
func emitStore(t types.Type, rhsTop bool, pushLhs bool) {
13011309
knd := sema.Kind(t)
13021310
emitComment(2, "emitStore(%s)\n", knd)
13031311
if rhsTop {
@@ -1315,7 +1323,7 @@ func emitStore(t *types.Type, rhsTop bool, pushLhs bool) {
13151323
emitRegiToMem(t)
13161324
}
13171325

1318-
func emitRegiToMem(t *types.Type) {
1326+
func emitRegiToMem(t types.Type) {
13191327
printf(" popq %%rsi # place to save\n")
13201328
k := sema.Kind(t)
13211329
switch k {
@@ -1348,7 +1356,7 @@ func emitRegiToMem(t *types.Type) {
13481356
}
13491357
}
13501358

1351-
func emitAssignZeroValue(lhs ir.MetaExpr, lhsType *types.Type) {
1359+
func emitAssignZeroValue(lhs ir.MetaExpr, lhsType types.Type) {
13521360
emitComment(2, "emitAssignZeroValue\n")
13531361
emitComment(2, "lhs addresss\n")
13541362
emitAddr(lhs)
@@ -1785,7 +1793,7 @@ func emitTypeSwitchStmt(meta *ir.MetaTypeSwitchStmt) {
17851793
// subjectVariable = subject
17861794
emitVariableAddr(meta.SubjectVariable)
17871795
emitExpr(meta.Subject)
1788-
emitStore(types.Eface, true, false)
1796+
emitStore(types.EmptyInterface, true, false)
17891797

17901798
cases := meta.Cases
17911799
var labels = make([]string, len(cases), len(cases))
@@ -1851,7 +1859,7 @@ func emitTypeSwitchStmt(meta *ir.MetaTypeSwitchStmt) {
18511859

18521860
// push rhs
18531861
emitVariableAddr(meta.SubjectVariable)
1854-
emitLoadAndPush(types.Eface)
1862+
emitLoadAndPush(types.EmptyInterface)
18551863
printf(" popq %%rax # ifc.dtype\n")
18561864
printf(" popq %%rcx # ifc.data\n")
18571865
printf(" pushq %%rcx # ifc.data\n")
@@ -1951,7 +1959,7 @@ func emitStmt(meta ir.MetaStmt) {
19511959
}
19521960
}
19531961

1954-
func emitRevertStackTop(t *types.Type) {
1962+
func emitRevertStackTop(t types.Type) {
19551963
printf(" addq $%d, %%rsp # revert stack top\n", sema.GetSizeOfType(t))
19561964
}
19571965

@@ -1992,7 +2000,7 @@ func emitFuncDecl(pkgName string, fnc *ir.Func) {
19922000
printf(" ret\n")
19932001
}
19942002

1995-
func emitZeroData(t *types.Type) {
2003+
func emitZeroData(t types.Type) {
19962004
for i := 0; i < sema.GetSizeOfType(t); i++ {
19972005
printf(" .byte 0 # zero value\n")
19982006
}
@@ -2084,24 +2092,25 @@ func GenerateDecls(pkg *ir.AnalyzedPackage, declFilePath string) {
20842092
}
20852093

20862094
fmt.Fprintf(fout, "package %s\n", pkg.Name)
2087-
20882095
// list import
20892096
for _, im := range pkg.Imports {
20902097
fmt.Fprintf(fout, "import \"%s\"\n", im)
20912098
}
20922099
// Type, Con, Var, Func
20932100
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)
20962105
}
20972106
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))
20992108
}
21002109
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))
21022111
}
21032112
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))
21052114
}
21062115

21072116
fout.Close()
@@ -2191,20 +2200,20 @@ func emitInterfaceTables(itab map[string]*sema.ITabEntry) {
21912200

21922201
methods := sema.GetInterfaceMethods(ent.Itype)
21932202
if len(methods) == 0 {
2194-
printf(" # no methods for %s\n", sema.SerializeType(ent.Itype, true))
2203+
printf(" # no methods\n")
21952204
}
21962205
for mi, m := range methods {
2197-
dmethod := sema.LookupMethod(ent.Dtype, m.Names[0])
2206+
dmethod := sema.LookupMethod(ent.Dtype, m.Name)
21982207
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)
22022211
}
22032212
printf(" .quad 0 # End of methods\n")
22042213

22052214
for mi, m := range methods {
22062215
printf(".method_name_%d_%d:\n", id, mi)
2207-
printf(" .string \"%s\"\n", m.Names[0].Name)
2216+
printf(" .string \"%s\"\n", m.Name)
22082217
}
22092218
}
22102219
printf("\n")

0 commit comments

Comments
 (0)