From 7b318bffb2570fa1c0a21e221001d6c081be7346 Mon Sep 17 00:00:00 2001 From: DQNEO Date: Sat, 19 Aug 2023 20:43:12 +0900 Subject: [PATCH] Refactor --- internal/codegen/codegen.go | 52 ++++++++++++++++++------------------- internal/sema/sema.go | 8 +++--- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/internal/codegen/codegen.go b/internal/codegen/codegen.go index 0711736c..c5c3d2d5 100644 --- a/internal/codegen/codegen.go +++ b/internal/codegen/codegen.go @@ -196,7 +196,7 @@ func emitVariableAddr(variable *ir.Variable) { } func emitListHeadAddr(list ir.MetaExpr) { - t := sema.GetGoTypeOfExpr(list) + t := sema.GetTypeOfExpr2(list) switch sema.Kind2(t) { case types.T_ARRAY: emitAddr(list) // array head @@ -229,10 +229,10 @@ func emitAddr(meta ir.MetaExpr) { panic("Unexpected kind") } case *ir.MetaIndexExpr: - if sema.Kind2(sema.GetGoTypeOfExpr(m.X)) == types.T_MAP { + if sema.Kind2(sema.GetTypeOfExpr2(m.X)) == types.T_MAP { emitAddrForMapSet(m) } else { - elmType := sema.GetTypeOfExpr(m) + elmType := sema.GetTypeOfExpr2(m) emitExpr(m.Index) // index number emitListElementAddr(m.X, elmType) } @@ -250,7 +250,7 @@ func emitAddr(meta ir.MetaExpr) { emitAddConst(m.Offset, "struct head address + struct.field offset") } case *ir.MetaCompositLit: - knd := sema.Kind2(sema.GetGoTypeOfExpr(m)) + knd := sema.Kind2(sema.GetTypeOfExpr2(m)) switch knd { case types.T_STRUCT: // result of evaluation of a struct literal is its address @@ -309,7 +309,7 @@ func emitConversion(toType *types.Type, arg0 ir.MetaExpr) { func emitIfcConversion(ic *ir.IfcConversion) { emitExpr(ic.Value) emitComment(2, "emitIfcConversion\n") - emitConvertToInterface(sema.GetGoTypeOfExpr(ic.Value), ic.Type.GoType) + emitConvertToInterface(sema.GetTypeOfExpr2(ic.Value), ic.Type.GoType) } func emitZeroValue(t types.GoType) { @@ -342,7 +342,7 @@ func emitZeroValue(t types.GoType) { } func emitLen(arg ir.MetaExpr) { - t := sema.GetGoTypeOfExpr(arg) + t := sema.GetTypeOfExpr2(arg) switch sema.Kind2(t) { case types.T_ARRAY: arrayLen := sema.GetArrayLen(t) @@ -365,7 +365,7 @@ func emitLen(arg ir.MetaExpr) { } func emitCap(arg ir.MetaExpr) { - t := sema.GetGoTypeOfExpr(arg) + t := sema.GetTypeOfExpr2(arg) knd := sema.Kind2(t) switch knd { case types.T_ARRAY: @@ -724,7 +724,7 @@ func emitIdent(meta *ir.MetaIdent) { } case "var": emitAddr(meta) - emitLoadAndPush(sema.GetGoTypeOfExpr(meta)) + emitLoadAndPush(sema.GetTypeOfExpr2(meta)) case "con": if meta.Const.IsGlobal && sema.Kind2(meta.Type.GoType) == types.T_STRING { // Treat like a global variable. @@ -751,14 +751,14 @@ func emitIndexExpr(meta *ir.MetaIndexExpr) { emitMapGet(meta, meta.NeedsOK) } else { emitAddr(meta) - emitLoadAndPush(sema.GetGoTypeOfExpr(meta)) + emitLoadAndPush(sema.GetTypeOfExpr2(meta)) } } // 1 value func emitStarExpr(meta *ir.MetaStarExpr) { emitAddr(meta) - emitLoadAndPush(sema.GetGoTypeOfExpr(meta)) + emitLoadAndPush(sema.GetTypeOfExpr2(meta)) } // 1 value X.Sel @@ -769,7 +769,7 @@ func emitSelectorExpr(meta *ir.MetaSelectorExpr) { } else { // strct.field emitAddr(meta) - emitLoadAndPush(sema.GetGoTypeOfExpr(meta)) + emitLoadAndPush(sema.GetTypeOfExpr2(meta)) } } @@ -1015,7 +1015,7 @@ func emitSliceExpr(meta *ir.MetaSliceExpr) { emitExpr(meta.Low) // index number elmType := sema.GetElementTypeOfCollectionType(listType) - emitListElementAddr(list, elmType) + emitListElementAddr(list, elmType.GoType) } // 1 or 2 values @@ -1059,7 +1059,7 @@ func emitMapGet(m *ir.MetaIndexExpr, okContext bool) { // 1 or 2 values func emitTypeAssertExpr(meta *ir.MetaTypeAssertExpr) { emitExpr(meta.X) - emitDtypeLabelAddr(meta.Type.GoType, sema.GetGoTypeOfExpr(meta.X)) + emitDtypeLabelAddr(meta.Type.GoType, sema.GetTypeOfExpr2(meta.X)) emitCompareDtypes() emitPopBool("type assertion ok value") @@ -1226,11 +1226,11 @@ func emitAddrForMapSet(indexExpr *ir.MetaIndexExpr) { emitCallDirect("runtime.getAddrForMapSet", args, ir.RuntimeGetAddrForMapSetSignature) } -func emitListElementAddr(list ir.MetaExpr, elmType *types.Type) { +func emitListElementAddr(list ir.MetaExpr, elmType types.GoType) { emitListHeadAddr(list) emitPopAddress("list head") printf(" popq %%rcx # index id\n") - printf(" movq $%d, %%rdx # elm size\n", sema.GetSizeOfType(elmType)) + printf(" movq $%d, %%rdx # elm size\n", sema.GetSizeOfType2(elmType)) printf(" imulq %%rdx, %%rcx\n") printf(" addq %%rcx, %%rax\n") printf(" pushq %%rax # addr of element\n") @@ -1397,7 +1397,7 @@ func _emitSingleAssign(lhs ir.MetaExpr, rhs ir.MetaExpr) { emitAddr(lhs) emitComment(2, "Assignment: emitExpr(rhs)\n") emitExpr(rhs) - emitStore(sema.GetGoTypeOfExpr(lhs), true, false) + emitStore(sema.GetTypeOfExpr2(lhs), true, false) } func emitBlockStmt(s *ir.MetaBlockStmt) { @@ -1438,7 +1438,7 @@ func emitOkAssignment(meta *ir.MetaTupleAssign) { } else { // @TODO interface conversion emitAddr(lhsMeta) - emitStore(sema.GetGoTypeOfExpr(lhsMeta), false, false) + emitStore(sema.GetTypeOfExpr2(lhsMeta), false, false) } } @@ -1466,7 +1466,7 @@ func emitFuncallAssignment(meta *ir.MetaTupleAssign) { } // @TODO interface conversion emitAddr(lhsMeta) - emitStore(sema.GetGoTypeOfExpr(lhsMeta), false, false) + emitStore(sema.GetTypeOfExpr2(lhsMeta), false, false) } } } @@ -1588,8 +1588,8 @@ func emitRangeMap(meta *ir.MetaForContainer) { printf(" popq %%rax\n") // &item{....} printf(" movq 16(%%rax), %%rcx\n") // item.key_data printf(" pushq %%rcx\n") - emitLoadAndPush(sema.GetGoTypeOfExpr(keyMeta)) // load dynamic data - emitStore(sema.GetGoTypeOfExpr(keyMeta), true, false) + emitLoadAndPush(sema.GetTypeOfExpr2(keyMeta)) // load dynamic data + emitStore(sema.GetTypeOfExpr2(keyMeta), true, false) } } @@ -1609,8 +1609,8 @@ func emitRangeMap(meta *ir.MetaForContainer) { printf(" popq %%rax\n") // &item{....} printf(" movq 24(%%rax), %%rcx\n") // item.key_data printf(" pushq %%rcx\n") - emitLoadAndPush(sema.GetGoTypeOfExpr(valueMeta)) // load dynamic data - emitStore(sema.GetGoTypeOfExpr(valueMeta), true, false) + emitLoadAndPush(sema.GetTypeOfExpr2(valueMeta)) // load dynamic data + emitStore(sema.GetTypeOfExpr2(valueMeta), true, false) } } @@ -1682,15 +1682,15 @@ func emitRangeStmt(meta *ir.MetaForContainer) { printf(" jne %s # jmp if false\n", labelExit) emitComment(2, "assign list[indexvar] value variables\n") - elemType := sema.GetTypeOfExpr(meta.ForRangeStmt.Value) + elemType := sema.GetTypeOfExpr2(meta.ForRangeStmt.Value) emitAddr(meta.ForRangeStmt.Value) // lhs emitVariableAddr(meta.ForRangeStmt.Indexvar) emitLoadAndPush(types.Int.GoType) // index value emitListElementAddr(meta.ForRangeStmt.X, elemType) - emitLoadAndPush(elemType.GoType) - emitStore(elemType.GoType, true, false) + emitLoadAndPush(elemType) + emitStore(elemType, true, false) // Body emitComment(2, "ForRangeStmt Body\n") @@ -1827,7 +1827,7 @@ func emitTypeSwitchStmt(meta *ir.MetaTypeSwitchStmt) { if t == nil { // case nil: printf(" pushq $0 # nil\n") } else { // case T:s - emitDtypeLabelAddr(t.GoType, sema.GetGoTypeOfExpr(meta.Subject)) + emitDtypeLabelAddr(t.GoType, sema.GetTypeOfExpr2(meta.Subject)) } emitCompareDtypes() emitPopBool(" of switch-case comparison") diff --git a/internal/sema/sema.go b/internal/sema/sema.go index d1f6c4cd..60c2fd57 100644 --- a/internal/sema/sema.go +++ b/internal/sema/sema.go @@ -238,7 +238,7 @@ func GetPackageSymbol(pkgName string, subsymbol string) string { return pkgName + "." + subsymbol } -func GetGoTypeOfExpr(meta ir.MetaExpr) types.GoType { +func GetTypeOfExpr2(meta ir.MetaExpr) types.GoType { t := GetTypeOfExpr(meta) return t.GoType } @@ -1324,7 +1324,7 @@ func walkTypeSwitchStmt(e *ast.TypeSwitchStmt) *ir.MetaTypeSwitchStmt { var typ *types.Type if !isNilIdent(e) { typ = E2T(e) - RegisterDtype(typ.GoType, GetGoTypeOfExpr(typeSwitch.Subject)) + RegisterDtype(typ.GoType, GetTypeOfExpr2(typeSwitch.Subject)) } typs = append(typs, typ) // universe nil can be appended } @@ -1624,7 +1624,7 @@ func walkConversion(pos token.Pos, toType *types.Type, arg0 ir.MetaExpr) ir.Meta Type: toType, Arg0: arg0, } - fromType := GetGoTypeOfExpr(arg0) + fromType := GetTypeOfExpr2(arg0) fromKind := Kind2(fromType) toKind := Kind(toType) if toKind == types.T_INTERFACE && fromKind != types.T_INTERFACE { @@ -2124,7 +2124,7 @@ func walkTypeAssertExpr(e *ast.TypeAssertExpr, ctx *ir.EvalContext) *ir.MetaType panic(fmt.Sprintf("[walkTypeAssertExpr] GoType is not set:%T\n", e.Type)) } - RegisterDtype(meta.Type.GoType, GetGoTypeOfExpr(meta.X)) + RegisterDtype(meta.Type.GoType, GetTypeOfExpr2(meta.X)) return meta }