From 8a6fac824ebff33e9a6485685da0b36a03ac2a6a Mon Sep 17 00:00:00 2001 From: Alfred Huang Date: Thu, 11 Feb 2021 14:39:40 -0800 Subject: [PATCH 1/2] 1. Change all label defs/access from name based to puidx based. 2, Support pstatic data for C/C++. 3. Enhance string constant functions. 4. Fix be/lower.cpp handling of C_va_start intrinsic. --- src/mapleall/maple_be/include/cg/cgfunc.h | 5 + src/mapleall/maple_be/include/cg/emit.h | 23 +- src/mapleall/maple_be/src/be/lower.cpp | 3 + .../src/cg/aarch64/aarch64_emitter.cpp | 81 +++--- .../src/cg/aarch64/aarch64_operand.cpp | 22 +- src/mapleall/maple_be/src/cg/cfi.cpp | 10 +- src/mapleall/maple_be/src/cg/cgfunc.cpp | 10 +- src/mapleall/maple_be/src/cg/emit.cpp | 239 +++++++++++++++--- .../maple_driver/src/driver_runner.cpp | 4 +- 9 files changed, 321 insertions(+), 76 deletions(-) diff --git a/src/mapleall/maple_be/include/cg/cgfunc.h b/src/mapleall/maple_be/include/cg/cgfunc.h index 8a2cafb8ee..00853febc8 100644 --- a/src/mapleall/maple_be/include/cg/cgfunc.h +++ b/src/mapleall/maple_be/include/cg/cgfunc.h @@ -797,6 +797,10 @@ class CGFunc { return shortFuncName; } + size_t GetLSymSize() const { + return lSymSize; + } + virtual InsnVisitor *NewInsnModifier() = 0; protected: @@ -804,6 +808,7 @@ class CGFunc { uint32 firstNonPregVRegNO; uint32 vRegCount; /* for assigning a number for each CG virtual register */ uint32 maxRegCount; /* for the current virtual register number limit */ + size_t lSymSize; /* size of local symbol table imported */ MapleVector vRegTable; /* table of CG's virtual registers indexed by v_reg no */ MapleUnorderedMap vRegOperandTable; MapleUnorderedMap pRegSpillMemOperands; diff --git a/src/mapleall/maple_be/include/cg/emit.h b/src/mapleall/maple_be/include/cg/emit.h index 3d920ec90d..8a2cafd74e 100644 --- a/src/mapleall/maple_be/include/cg/emit.h +++ b/src/mapleall/maple_be/include/cg/emit.h @@ -137,6 +137,10 @@ class Emitter { currentMop = mOp; } + std::vector &GetStringPtr() { + return stringPtr; + } + void EmitAsmLabel(AsmLabel label); void EmitAsmLabel(const MIRSymbol &mirSymbol, AsmLabel label); void EmitFileInfo(const std::string &fileName); @@ -147,8 +151,9 @@ class Emitter { void EmitCombineBfldValue(StructEmitInfo &structEmitInfo); void EmitBitFieldConstant(StructEmitInfo &structEmitInfo, MIRConst &mirConst, const MIRType *nextType, uint64 fieldOffset); - void EmitScalarConstant(MIRConst &mirConst, bool newLine = true, bool flag32 = false); - void EmitStrConstant(const MIRStrConst &mirStrConst); + void EmitScalarConstant(MIRConst &mirConst, bool newLine = true, bool flag32 = false, bool isIndirect = false); + void EmitStr(const std::string& mplStr, bool emitAscii = false, bool emitNewline = false); + void EmitStrConstant(const MIRStrConst &mirStrConst, bool isAscii = false, bool isIndirect = false); void EmitStr16Constant(const MIRStr16Const &mirStr16Const); void EmitIntConst(const MIRSymbol &mirSymbol, MIRAggConst &aggConst, uint32 itabConflictIndex, const std::map &strIdx2Type, size_t idx); @@ -166,6 +171,7 @@ class Emitter { void EmitGlobalVar(const MIRSymbol &globalVar); void EmitStaticFields(const std::vector &fields); void EmitLiteral(const MIRSymbol &literal, const std::map &strIdx2Type); + void EmitStringPointers(); void GetHotAndColdMetaSymbolInfo(const std::vector &mirSymbolVec, std::vector &hotFieldInfoSymbolVec, std::vector &coldFieldInfoSymbolVec, const std::string &prefixStr, @@ -178,6 +184,7 @@ class Emitter { void MarkVtabOrItabEndFlag(const std::vector &mirSymbolVec); void EmitArrayConstant(MIRConst &mirConst); void EmitStructConstant(MIRConst &mirConst); + void EmitLocalVariable(CGFunc &cgFunc); void EmitGlobalVariable(); void EmitGlobalRootList(const MIRSymbol &mirSymbol); void EmitMuidTable(const std::vector &vec, const std::map &strIdx2Type, @@ -202,9 +209,9 @@ class Emitter { return *this; } - void EmitLabelRef(const std::string &name, LabelIdx labIdx); - void EmitStmtLabel(const std::string &name, LabelIdx labIdx); - void EmitLabelPair(const std::string &name, const LabelPair &pairLabel); + void EmitLabelRef(LabelIdx labIdx); + void EmitStmtLabel(LabelIdx labIdx); + void EmitLabelPair(const LabelPair &pairLabel); /* Emit signed/unsigned integer literals in decimal or hexadecimal */ void EmitDecSigned(int64 num); @@ -226,6 +233,10 @@ class Emitter { return funcInsnCount; } + CG *GetCG() const { + return cg; + } + void ClearFuncInsnCount() { funcInsnCount = 0; } @@ -268,6 +279,7 @@ class Emitter { MIRModule &mirModule = *cg.GetMIRModule(); memPool = mirModule.GetMemPool(); asmInfo = memPool->New(*memPool); + stringPtr.resize(GlobalTables::GetUStrTable().StringTableSize()); } ~Emitter() = default; @@ -286,6 +298,7 @@ class Emitter { MemPool *memPool; uint32 arraySize; bool isFlexibleArray; + std::vector stringPtr; #if 1/* REQUIRE TO SEPERATE TARGAARCH64 TARGARM32 */ /* Following code is under TARGAARCH64 condition */ uint64 javaInsnCount = 0; diff --git a/src/mapleall/maple_be/src/be/lower.cpp b/src/mapleall/maple_be/src/be/lower.cpp index 36f85a18a5..520c49fca2 100644 --- a/src/mapleall/maple_be/src/be/lower.cpp +++ b/src/mapleall/maple_be/src/be/lower.cpp @@ -2510,6 +2510,9 @@ StmtNode *CGLowerer::LowerIntrinsiccall(IntrinsiccallNode &intrincall, BlockNode if (intrnID == INTRN_MPL_CLEAR_STACK) { return LowerIntrinsicMplClearStack(intrincall, newBlk); } + if (intrnID == INTRN_C_va_start) { + return &intrincall; + } IntrinDesc *intrinDesc = &IntrinDesc::intrinTable[intrnID]; if (intrinDesc->IsSpecial()) { /* For special intrinsics we leave them to CGFunc::SelectIntrinCall() */ diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_emitter.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_emitter.cpp index 594e84401c..96ea2a9a8c 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_emitter.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_emitter.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) [2020] Huawei Technologies Co.,Ltd.All rights reserved. + * Copyright (c) [2020-2021] Huawei Technologies Co.,Ltd.All rights reserved. * * OpenArkCompiler is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. @@ -85,20 +85,21 @@ void AArch64AsmEmitter::EmitFastLSDA(FuncEmitInfo &funcEmitInfo) { CG *currCG = cgFunc.GetCG(); Emitter *emitter = currCG->GetEmitter(); - const std::string &funcName = std::string(cgFunc.GetShortFuncName().c_str()); + PUIdx pIdx = currCG->GetMIRModule()->CurFunction()->GetPuidx(); + const std::string &idx = strdup(std::to_string(pIdx).c_str()); /* * .word 0xFFFFFFFF * .word .Label.LTest_3B_7C_3Cinit_3E_7C_28_29V3-func_start_label */ (void)emitter->Emit("\t.word 0xFFFFFFFF\n"); - (void)emitter->Emit("\t.word .L." + funcName + "."); + (void)emitter->Emit("\t.word .L." + idx + "__"); if (aarchCGFunc.NeedCleanup()) { emitter->Emit(cgFunc.GetCleanupLabel()->GetLabelIdx()); } else { ASSERT(!cgFunc.GetExitBBsVec().empty(), "exitbbsvec is empty in AArch64AsmEmitter::EmitFastLSDA"); emitter->Emit(cgFunc.GetExitBB(0)->GetLabIdx()); } - emitter->Emit("-.L." + funcName + ".") + emitter->Emit("-.L." + idx + "__") .Emit(cgFunc.GetStartLabel()->GetLabelIdx()) .Emit("\n"); emitter->IncreaseJavaInsnCount(); @@ -117,20 +118,19 @@ void AArch64AsmEmitter::EmitFullLSDA(FuncEmitInfo &funcEmitInfo) { emitter->Emit("\t.align 2\n"); /* emit LSDA header */ LSDAHeader *lsdaHeader = ehFunc->GetLSDAHeader(); - const std::string &funcName = std::string(cgFunc.GetShortFuncName().c_str()); - emitter->EmitStmtLabel(funcName, lsdaHeader->GetLSDALabel()->GetLabelIdx()); + emitter->EmitStmtLabel(lsdaHeader->GetLSDALabel()->GetLabelIdx()); emitter->Emit("\t.byte ").Emit(lsdaHeader->GetLPStartEncoding()).Emit("\n"); emitter->Emit("\t.byte ").Emit(lsdaHeader->GetTTypeEncoding()).Emit("\n"); emitter->Emit("\t.uleb128 "); - emitter->EmitLabelPair(funcName, lsdaHeader->GetTTypeOffset()); - emitter->EmitStmtLabel(funcName, lsdaHeader->GetTTypeOffset().GetStartOffset()->GetLabelIdx()); + emitter->EmitLabelPair(lsdaHeader->GetTTypeOffset()); + emitter->EmitStmtLabel(lsdaHeader->GetTTypeOffset().GetStartOffset()->GetLabelIdx()); /* emit call site table */ emitter->Emit("\t.byte ").Emit(lsdaHeader->GetCallSiteEncoding()).Emit("\n"); /* callsite table size */ emitter->Emit("\t.uleb128 "); - emitter->EmitLabelPair(funcName, ehFunc->GetLSDACallSiteTable()->GetCSTable()); + emitter->EmitLabelPair(ehFunc->GetLSDACallSiteTable()->GetCSTable()); /* callsite start */ - emitter->EmitStmtLabel(funcName, ehFunc->GetLSDACallSiteTable()->GetCSTable().GetStartOffset()->GetLabelIdx()); + emitter->EmitStmtLabel(ehFunc->GetLSDACallSiteTable()->GetCSTable().GetStartOffset()->GetLabelIdx()); ehFunc->GetLSDACallSiteTable()->SortCallSiteTable([&aarchCGFunc](LSDACallSite *a, LSDACallSite *b) { CHECK_FATAL(a != nullptr, "nullptr check"); CHECK_FATAL(b != nullptr, "nullptr check"); @@ -145,14 +145,14 @@ void AArch64AsmEmitter::EmitFullLSDA(FuncEmitInfo &funcEmitInfo) { for (size_t i = 0; i < callSiteTable.size(); ++i) { LSDACallSite *lsdaCallSite = callSiteTable[i]; emitter->Emit("\t.uleb128 "); - emitter->EmitLabelPair(funcName, lsdaCallSite->csStart); + emitter->EmitLabelPair(lsdaCallSite->csStart); emitter->Emit("\t.uleb128 "); - emitter->EmitLabelPair(funcName, lsdaCallSite->csLength); + emitter->EmitLabelPair(lsdaCallSite->csLength); if (lsdaCallSite->csLandingPad.GetStartOffset()) { emitter->Emit("\t.uleb128 "); - emitter->EmitLabelPair(funcName, lsdaCallSite->csLandingPad); + emitter->EmitLabelPair(lsdaCallSite->csLandingPad); } else { ASSERT(lsdaCallSite->csAction == 0, "csAction error!"); emitter->Emit("\t.uleb128 "); @@ -161,11 +161,13 @@ void AArch64AsmEmitter::EmitFullLSDA(FuncEmitInfo &funcEmitInfo) { LabelPair cleaupCode; cleaupCode.SetStartOffset(cgFunc.GetStartLabel()); cleaupCode.SetEndOffset(cgFunc.GetCleanupLabel()); - emitter->EmitLabelPair(funcName, cleaupCode); + emitter->EmitLabelPair(cleaupCode); } else if (cgFunc.GetFunction().IsJava()) { ASSERT(!cgFunc.GetExitBBsVec().empty(), "exitbbsvec is empty in AArch64Emitter::EmitFullLSDA"); - (void)emitter->Emit(".L." + funcName).Emit(".").Emit(cgFunc.GetExitBB(0)->GetLabIdx()); - (void)emitter->Emit(" - .L." + funcName).Emit(".").Emit(cgFunc.GetStartLabel()->GetLabelIdx()).Emit("\n"); + PUIdx pIdx = cgFunc.GetMirModule().CurFunction()->GetPuidx(); + const std::string &idx = strdup(std::to_string(pIdx).c_str()); + (void)emitter->Emit(".L." + idx).Emit("__").Emit(cgFunc.GetExitBB(0)->GetLabIdx()); + (void)emitter->Emit(" - .L." + idx).Emit("__").Emit(cgFunc.GetStartLabel()->GetLabelIdx()).Emit("\n"); } else { emitter->Emit("0\n"); } @@ -187,22 +189,24 @@ void AArch64AsmEmitter::EmitFullLSDA(FuncEmitInfo &funcEmitInfo) { funcStart.SetStartOffset(cgFunc.GetStartLabel()); funcStart.SetEndOffset(cgFunc.GetStartLabel()); emitter->Emit("\t.uleb128 "); - emitter->EmitLabelPair(funcName, funcStart); + emitter->EmitLabelPair(funcStart); LabelPair funcLength; funcLength.SetStartOffset(cgFunc.GetStartLabel()); funcLength.SetEndOffset(cgFunc.GetCleanupLabel()); emitter->Emit("\t.uleb128 "); - emitter->EmitLabelPair(funcName, funcLength); + emitter->EmitLabelPair(funcLength); LabelPair cleaupCode; cleaupCode.SetStartOffset(cgFunc.GetStartLabel()); cleaupCode.SetEndOffset(cgFunc.GetCleanupLabel()); emitter->Emit("\t.uleb128 "); if (aarchCGFunc.NeedCleanup()) { - emitter->EmitLabelPair(funcName, cleaupCode); + emitter->EmitLabelPair(cleaupCode); } else { ASSERT(!cgFunc.GetExitBBsVec().empty(), "exitbbsvec is empty in AArch64AsmEmitter::EmitFullLSDA"); - (void)emitter->Emit(".L." + funcName).Emit(".").Emit(cgFunc.GetExitBB(0)->GetLabIdx()); - (void)emitter->Emit(" - .L." + funcName).Emit(".").Emit(cgFunc.GetStartLabel()->GetLabelIdx()).Emit("\n"); + PUIdx pIdx = cgFunc.GetMirModule().CurFunction()->GetPuidx(); + const std::string &idx = strdup(std::to_string(pIdx).c_str()); + (void)emitter->Emit(".L." + idx).Emit("__").Emit(cgFunc.GetExitBB(0)->GetLabIdx()); + (void)emitter->Emit(" - .L." + idx).Emit("__").Emit(cgFunc.GetStartLabel()->GetLabelIdx()).Emit("\n"); } emitter->Emit("\t.uleb128 0\n"); if (!cgFunc.GetFunction().IsJava()) { @@ -211,18 +215,18 @@ void AArch64AsmEmitter::EmitFullLSDA(FuncEmitInfo &funcEmitInfo) { unwindStart.SetStartOffset(cgFunc.GetStartLabel()); unwindStart.SetEndOffset(cgFunc.GetCleanupLabel()); emitter->Emit("\t.uleb128 "); - emitter->EmitLabelPair(funcName, unwindStart); + emitter->EmitLabelPair(unwindStart); LabelPair unwindLength; unwindLength.SetStartOffset(cgFunc.GetCleanupLabel()); unwindLength.SetEndOffset(cgFunc.GetEndLabel()); emitter->Emit("\t.uleb128 "); - emitter->EmitLabelPair(funcName, unwindLength); + emitter->EmitLabelPair(unwindLength); emitter->Emit("\t.uleb128 0\n"); emitter->Emit("\t.uleb128 0\n"); } } /* callsite end label */ - emitter->EmitStmtLabel(funcName, ehFunc->GetLSDACallSiteTable()->GetCSTable().GetEndOffset()->GetLabelIdx()); + emitter->EmitStmtLabel(ehFunc->GetLSDACallSiteTable()->GetCSTable().GetEndOffset()->GetLabelIdx()); /* tt */ const LSDAActionTable *lsdaActionTable = ehFunc->GetLSDAActionTable(); for (size_t i = 0; i < lsdaActionTable->Size(); ++i) { @@ -247,7 +251,7 @@ void AArch64AsmEmitter::EmitFullLSDA(FuncEmitInfo &funcEmitInfo) { emitter->Emit("\t.4byte " + dwRefString + "\n"); } /* end of lsda */ - emitter->EmitStmtLabel(funcName, lsdaHeader->GetTTypeOffset().GetEndOffset()->GetLabelIdx()); + emitter->EmitStmtLabel(lsdaHeader->GetTTypeOffset().GetEndOffset()->GetLabelIdx()); } void AArch64AsmEmitter::EmitBBHeaderLabel(FuncEmitInfo &funcEmitInfo, const std::string &name, LabelIdx labIdx) { @@ -261,11 +265,20 @@ void AArch64AsmEmitter::EmitBBHeaderLabel(FuncEmitInfo &funcEmitInfo, const std: label.SetLabelOrder(currCG->GetLabelOrderCnt()); currCG->IncreaseLabelOrderCnt(); } + + PUIdx pIdx = currCG->GetMIRModule()->CurFunction()->GetPuidx(); + const char *puIdx = strdup(std::to_string(pIdx).c_str()); + const std::string &labelName = cgFunc.GetFunction().GetLabelTab()->GetName(labIdx); if (currCG->GenerateVerboseCG()) { - (void)emitter.Emit(".L.").Emit(name).Emit(".").Emit(labIdx).Emit(":\t//label order ").Emit(label.GetLabelOrder()); - emitter.Emit("\n"); + emitter.Emit(".L.").Emit(puIdx).Emit("__").Emit(labIdx).Emit(":\t//label order ").Emit(label.GetLabelOrder()); + if (!labelName.empty() && labelName.at(0) != '@') { + //If label name has @ as its first char, it is not from MIR + emitter.Emit(", MIR: @").Emit(labelName).Emit("\n"); + } else { + emitter.Emit("\n"); + } } else { - (void)emitter.Emit(".L.").Emit(name).Emit(".").Emit(labIdx).Emit(":\n"); + emitter.Emit(".L.").Emit(puIdx).Emit("__").Emit(labIdx).Emit(":\n"); } } @@ -304,7 +317,6 @@ void AArch64AsmEmitter::Run(FuncEmitInfo &funcEmitInfo) { MIRSymbol *funcSt = GlobalTables::GetGsymTable().GetSymbolFromStidx(cgFunc.GetFunction().GetStIdx().Idx()); const std::string &funcName = std::string(cgFunc.GetShortFuncName().c_str()); - std::string funcStName = funcSt->GetName(); if (funcSt->GetFunction()->GetAttr(FUNCATTR_weak)) { (void)emitter.Emit("\t.weak\t" + funcStName + "\n"); @@ -367,9 +379,12 @@ void AArch64AsmEmitter::Run(FuncEmitInfo &funcEmitInfo) { emitter.IncreaseJavaInsnCount(); } else if (ehFunc->NeedFullLSDA()) { LSDAHeader *lsdaHeader = ehFunc->GetLSDAHeader(); + PUIdx pIdx = emitter.GetCG()->GetMIRModule()->CurFunction()->GetPuidx(); + const std::string &idx = strdup(std::to_string(pIdx).c_str()); + /* .word .Label.lsda_label-func_start_label */ - (void)emitter.Emit("\t.word .L." + funcName).Emit(".").Emit(lsdaHeader->GetLSDALabel()->GetLabelIdx()); - (void)emitter.Emit("-.L." + funcName).Emit(".").Emit(cgFunc.GetStartLabel()->GetLabelIdx()).Emit("\n"); + (void)emitter.Emit("\t.word .L." + idx).Emit("__").Emit(lsdaHeader->GetLSDALabel()->GetLabelIdx()); + (void)emitter.Emit("-.L." + idx).Emit("__").Emit(cgFunc.GetStartLabel()->GetLabelIdx()).Emit("\n"); emitter.IncreaseJavaInsnCount(); } else if (ehFunc->NeedFastLSDA()) { EmitFastLSDA(funcEmitInfo); @@ -435,10 +450,12 @@ void AArch64AsmEmitter::Run(FuncEmitInfo &funcEmitInfo) { emitter.Emit(st->GetName() + ":\n"); MIRAggConst *arrayConst = safe_cast(st->GetKonst()); CHECK_FATAL(arrayConst != nullptr, "null ptr check"); + PUIdx pIdx = cgFunc.GetMirModule().CurFunction()->GetPuidx(); + const std::string &idx = strdup(std::to_string(pIdx).c_str()); for (size_t i = 0; i < arrayConst->GetConstVec().size(); i++) { MIRLblConst *lblConst = safe_cast(arrayConst->GetConstVecItem(i)); CHECK_FATAL(lblConst != nullptr, "null ptr check"); - (void)emitter.Emit("\t.quad\t.L." + funcName).Emit(".").Emit(lblConst->GetValue()); + (void)emitter.Emit("\t.quad\t.L." + idx).Emit("__").Emit(lblConst->GetValue()); (void)emitter.Emit(" - " + st->GetName() + "\n"); emitter.IncreaseJavaInsnCount(kQuadInsnCount); } diff --git a/src/mapleall/maple_be/src/cg/aarch64/aarch64_operand.cpp b/src/mapleall/maple_be/src/cg/aarch64/aarch64_operand.cpp index cf1248e5c2..6442c58140 100644 --- a/src/mapleall/maple_be/src/cg/aarch64/aarch64_operand.cpp +++ b/src/mapleall/maple_be/src/cg/aarch64/aarch64_operand.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) [2020] Huawei Technologies Co.,Ltd.All rights reserved. + * Copyright (c) [2020-2021] Huawei Technologies Co.,Ltd.All rights reserved. * * OpenArkCompiler is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. @@ -141,6 +141,8 @@ void AArch64OfstOperand::Emit(Emitter &emitter, const OpndProp *opndProp) const if (CGOptions::IsPIC() && (symbol->GetStorageClass() == kScGlobal || symbol->GetStorageClass() == kScExtern)) { emitter.Emit(":got:" + symbol->GetName()); + } else if (symbol->GetStorageClass() == kScPstatic && symbol->GetSKind() != kStConst && symbol->IsLocal()) { + emitter.Emit(symbol->GetName() + std::to_string(emitter.GetCG()->GetMIRModule()->CurFunction()->GetPuidx())); } else { emitter.Emit(symbol->GetName()); } @@ -180,6 +182,8 @@ void StImmOperand::Emit(Emitter &emitter, const OpndProp *opndProp) const { } if (CGOptions::IsPIC() && (symbol->GetStorageClass() == kScGlobal || symbol->GetStorageClass() == kScExtern)) { emitter.Emit(":got:" + GetName()); + } else if (symbol->GetStorageClass() == kScPstatic && symbol->GetSKind() != kStConst && symbol->IsLocal()) { + emitter.Emit(symbol->GetName() + std::to_string(emitter.GetCG()->GetMIRModule()->CurFunction()->GetPuidx())); } else { emitter.Emit(GetName()); } @@ -298,7 +302,12 @@ void AArch64MemOperand::Emit(Emitter &emitter, const OpndProp *opndProp) const { ASSERT(offset != nullptr, "nullptr check"); emitter.Emit(", #:lo12:"); - emitter.Emit(GetSymbolName()); + if (GetSymbol()->GetStorageClass() == kScPstatic && GetSymbol()->IsLocal()) { + PUIdx pIdx = emitter.GetCG()->GetMIRModule()->CurFunction()->GetPuidx(); + emitter.Emit(GetSymbolName() + std::to_string(pIdx)); + } else { + emitter.Emit(GetSymbolName()); + } if (!offset->IsZero()) { emitter.Emit("+"); emitter.Emit(std::to_string(offset->GetOffsetValue())); @@ -349,7 +358,14 @@ void AArch64MemOperand::Dump() const { GetBaseRegister()->Dump(); LogInfo::MapleLogger() << "offset:"; AArch64OfstOperand *offOpnd = GetOffsetImmediate(); - LogInfo::MapleLogger() << "#:lo12:" << GetSymbolName() << "+" << std::to_string(offOpnd->GetOffsetValue()); + LogInfo::MapleLogger() << "#:lo12:"; + if (GetSymbol()->GetStorageClass() == kScPstatic && GetSymbol()->IsLocal()) { + PUIdx pIdx = CG::GetCurCGFunc()->GetMirModule().CurFunction()->GetPuidx(); + LogInfo::MapleLogger() << GetSymbolName() << std::to_string(pIdx); + } else { + LogInfo::MapleLogger() << GetSymbolName(); + } + LogInfo::MapleLogger() << "+" << std::to_string(offOpnd->GetOffsetValue()); break; } default: diff --git a/src/mapleall/maple_be/src/cg/cfi.cpp b/src/mapleall/maple_be/src/cg/cfi.cpp index 03c7792f6f..033777b3c2 100644 --- a/src/mapleall/maple_be/src/cg/cfi.cpp +++ b/src/mapleall/maple_be/src/cg/cfi.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) [2020] Huawei Technologies Co.,Ltd.All rights reserved. + * Copyright (c) [2020-2021] Huawei Technologies Co.,Ltd.All rights reserved. * * OpenArkCompiler is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. @@ -108,7 +108,13 @@ void StrOperand::Dump() const { void LabelOperand::Emit(Emitter &emitter, const OpndProp *opndProp) const { (void)opndProp; - emitter.Emit(".label.").Emit(parentFunc).Emit(labelIndex); + if (emitter.GetCG()->GetMIRModule()->IsCModule()) { + PUIdx pIdx = emitter.GetCG()->GetMIRModule()->CurFunction()->GetPuidx(); + const char *idx = strdup(std::to_string(pIdx).c_str()); + emitter.Emit(".label.").Emit(idx).Emit("__").Emit(labelIndex); + } else { + emitter.Emit(".label.").Emit(parentFunc).Emit(labelIndex); + } } void LabelOperand::Dump() const { diff --git a/src/mapleall/maple_be/src/cg/cgfunc.cpp b/src/mapleall/maple_be/src/cg/cgfunc.cpp index 3857a14a29..05c78e23cf 100644 --- a/src/mapleall/maple_be/src/cg/cgfunc.cpp +++ b/src/mapleall/maple_be/src/cg/cgfunc.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) [2020] Huawei Technologies Co.,Ltd.All rights reserved. + * Copyright (c) [2020-2021] Huawei Technologies Co.,Ltd.All rights reserved. * * OpenArkCompiler is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. @@ -722,6 +722,10 @@ CGFunc::CGFunc(MIRModule &mod, CG &cg, MIRFunction &mirFunc, BECommon &beCommon, new (&GetVirtualRegNodeFromPseudoRegIdx(i)) VirtualRegNode(GetRegTyFromPrimTy(primType), byteLen); } firstCGGenLabelIdx = func.GetLabelTab()->GetLabelTableSize(); + lSymSize = 0; + if (func.GetSymTab()) { + lSymSize = func.GetSymTab()->GetSymbolTableSize(); + } } CGFunc::~CGFunc() { @@ -1096,7 +1100,9 @@ void CGFunc::HandleFunction() { if (!func.GetModule()->IsCModule()) { MergeReturn(); } - ASSERT(exitBBVec.size() <= 1, "there are more than one BB_return in func"); + if (func.IsJava()) { + ASSERT(exitBBVec.size() <= 1, "there are more than one BB_return in func"); + } ProcessExitBBVec(); if (func.IsJava()) { diff --git a/src/mapleall/maple_be/src/cg/emit.cpp b/src/mapleall/maple_be/src/cg/emit.cpp index 89aa2bf9dc..262b41d0d8 100644 --- a/src/mapleall/maple_be/src/cg/emit.cpp +++ b/src/mapleall/maple_be/src/cg/emit.cpp @@ -62,19 +62,23 @@ namespace maplebe { using namespace maple; using namespace cfi; -void Emitter::EmitLabelRef(const std::string &name, LabelIdx labIdx) { - outStream << ".L." << name << "." << labIdx; +void Emitter::EmitLabelRef(LabelIdx labIdx) { + PUIdx pIdx = GetCG()->GetMIRModule()->CurFunction()->GetPuidx(); + const char *idx = strdup(std::to_string(pIdx).c_str()); + outStream << ".L." << idx << "__" << labIdx; } -void Emitter::EmitStmtLabel(const std::string &name, LabelIdx labIdx) { - EmitLabelRef(name, labIdx); +void Emitter::EmitStmtLabel(LabelIdx labIdx) { + EmitLabelRef(labIdx); outStream << ":\n"; } -void Emitter::EmitLabelPair(const std::string &name, const LabelPair &pairLabel) { +void Emitter::EmitLabelPair(const LabelPair &pairLabel) { ASSERT(pairLabel.GetEndOffset() || pairLabel.GetStartOffset(), "NYI"); - outStream << ".L." << name << "." << pairLabel.GetEndOffset()->GetLabelIdx() << " - " - << ".L." << name << "." << pairLabel.GetStartOffset()->GetLabelIdx() << "\n"; + PUIdx pIdx = GetCG()->GetMIRModule()->CurFunction()->GetPuidx(); + const char *idx = strdup(std::to_string(pIdx).c_str()); + outStream << ".L." << idx << "__" << pairLabel.GetEndOffset()->GetLabelIdx() << " - " + << ".L." << idx << "__" << pairLabel.GetStartOffset()->GetLabelIdx() << "\n"; } AsmLabel Emitter::GetTypeAsmInfoName(PrimType primType) const { @@ -232,8 +236,13 @@ void Emitter::EmitAsmLabel(AsmLabel label) { void Emitter::EmitAsmLabel(const MIRSymbol &mirSymbol, AsmLabel label) { MIRType *mirType = mirSymbol.GetType(); - const std::string &symName = mirSymbol.GetName(); - + std::string symName; + if (mirSymbol.GetStorageClass() == kScPstatic && mirSymbol.IsLocal()) { + PUIdx pIdx = GetCG()->GetMIRModule()->CurFunction()->GetPuidx(); + symName = mirSymbol.GetName() + std::to_string(pIdx); + } else { + symName = mirSymbol.GetName(); + } if (Globals::GetInstance()->GetBECommon()->IsEmptyOfTypeAlignTable()) { ASSERT(false, "container empty check"); } @@ -266,10 +275,9 @@ void Emitter::EmitAsmLabel(const MIRSymbol &mirSymbol, AsmLabel label) { case kAsmComm: { std::string size; if (isFlexibleArray) { - size = std::to_string( - Globals::GetInstance()->GetBECommon()->GetTypeSize(mirType->GetTypeIndex()) + arraySize); + size = std::to_string(Globals::GetInstance()->GetBECommon()->GetTypeSize(mirType->GetTypeIndex()) + arraySize); } else { - size = std::to_string(Globals::GetInstance()->GetBECommon()->GetTypeSize(mirType->GetTypeIndex())); + size = std::to_string(Globals::GetInstance()->GetBECommon()->GetTypeSize(mirType->GetTypeIndex())); } Emit(asmInfo->GetComm()); Emit(symName); @@ -312,10 +320,9 @@ void Emitter::EmitAsmLabel(const MIRSymbol &mirSymbol, AsmLabel label) { case kAsmSize: { std::string size; if (isFlexibleArray) { - size = std::to_string( - Globals::GetInstance()->GetBECommon()->GetTypeSize(mirType->GetTypeIndex()) + arraySize); + size = std::to_string(Globals::GetInstance()->GetBECommon()->GetTypeSize(mirType->GetTypeIndex()) + arraySize); } else { - size = std::to_string(Globals::GetInstance()->GetBECommon()->GetTypeSize(mirType->GetTypeIndex())); + size = std::to_string(Globals::GetInstance()->GetBECommon()->GetTypeSize(mirType->GetTypeIndex())); } Emit(asmInfo->GetSize()); Emit(symName); @@ -398,7 +405,71 @@ void Emitter::EmitBitFieldConstant(StructEmitInfo &structEmitInfo, MIRConst &mir } } -void Emitter::EmitStrConstant(const MIRStrConst &mirStrConst) { +void Emitter::EmitStr(const std::string& mplStr, bool emitAscii, bool emitNewline) { + const char *str = mplStr.c_str(); + size_t len = mplStr.size(); + + if (emitAscii) { + Emit("\t.ascii\t\""); // Do not terminate with \0 + } else { + Emit("\t.string\t\""); + } + + // don't expand special character in a writeout to .s, + // convert all \s to \\s in string for storing in .string + for (int i = 0; i < len; i++) { + // Referred to GNU AS: 3.6.1.1 Strings + char buf[5]; + if (isprint(*str)) { + buf[0] = *str; + buf[1] = 0; + if (*str == '\\' || *str == '\"') { + buf[0] = '\\'; + buf[1] = *str; + buf[2] = 0; + } + Emit(buf); + } else if (*str == '\b') { + Emit("\\b"); + } else if (*str == '\n') { + Emit("\\n"); + } else if (*str == '\r') { + Emit("\\r"); + } else if (*str == '\t') { + Emit("\\t"); + } else if (*str == '\0') { + buf[0] = '\\'; + buf[1] = '0'; + buf[2] = 0; + Emit(buf); + } else { + // all others, print as number + int ret = snprintf_s(buf, sizeof(buf), 4, "\\%03o", (*str) & 0xFF); + if (ret < 0) { + FATAL(kLncFatal, "snprintf_s failed"); + } + buf[4] = '\0'; + Emit(buf); + } + str++; + } + + Emit("\""); + if (emitNewline) { + Emit("\n"); + } +} + +void Emitter::EmitStrConstant(const MIRStrConst &mirStrConst, bool isAscii, bool isIndirect) { + if (isIndirect) { + uint32 strId = mirStrConst.GetValue().GetIdx(); + std::vector &sPtr = GetStringPtr(); + if (sPtr[strId] == 0) { + sPtr[strId] = mirStrConst.GetValue(); + } + Emit("\t.dword\t").Emit(".LSTR__").Emit(std::to_string(strId).c_str()); + return; + } Emit("\t.string \""); /* * don't expand special character in a writeout to .s, @@ -406,7 +477,7 @@ void Emitter::EmitStrConstant(const MIRStrConst &mirStrConst) { */ const char *str = GlobalTables::GetUStrTable().GetStringFromStrIdx(mirStrConst.GetValue()).c_str(); if (isFlexibleArray) { - arraySize += static_cast(strlen(str)) + k1ByteSize; + arraySize += static_cast(strlen(str)) + k1ByteSize; } constexpr int bufSize = 6; while (*str) { @@ -466,7 +537,7 @@ void Emitter::EmitStr16Constant(const MIRStr16Const &mirStr16Const) { } } -void Emitter::EmitScalarConstant(MIRConst &mirConst, bool newLine, bool flag32) { +void Emitter::EmitScalarConstant(MIRConst &mirConst, bool newLine, bool flag32, bool isIndirect) { MIRType &mirType = mirConst.GetType(); AsmLabel asmName = GetTypeAsmInfoName(mirType.GetPrimType()); switch (mirConst.GetKind()) { @@ -483,7 +554,7 @@ void Emitter::EmitScalarConstant(MIRConst &mirConst, bool newLine, bool flag32) } Emit(intCt.GetValue()); if (isFlexibleArray) { - arraySize += (sizeInBits / kBitsPerByte); + arraySize += (sizeInBits / kBitsPerByte); } break; } @@ -492,7 +563,7 @@ void Emitter::EmitScalarConstant(MIRConst &mirConst, bool newLine, bool flag32) EmitAsmLabel(asmName); Emit(std::to_string(floatCt.GetIntValue())); if (isFlexibleArray) { - arraySize += k4ByteFloatSize; + arraySize += k4ByteFloatSize; } break; } @@ -501,13 +572,17 @@ void Emitter::EmitScalarConstant(MIRConst &mirConst, bool newLine, bool flag32) EmitAsmLabel(asmName); Emit(std::to_string(doubleCt.GetIntValue())); if (isFlexibleArray) { - arraySize += k8ByteDoubleSize; + arraySize += k8ByteDoubleSize; } break; } case kConstStrConst: { MIRStrConst &strCt = static_cast(mirConst); - EmitStrConstant(strCt); + if (cg->GetMIRModule()->IsCModule()) { + EmitStrConstant(strCt, false, isIndirect); + } else { + EmitStrConstant(strCt); + } break; } case kConstStr16Const: { @@ -517,9 +592,18 @@ void Emitter::EmitScalarConstant(MIRConst &mirConst, bool newLine, bool flag32) } case kConstAddrof: { MIRAddrofConst &symAddr = static_cast(mirConst); - MIRSymbol *symAddrSym = GlobalTables::GetGsymTable().GetSymbolFromStidx(symAddr.GetSymbolIndex().Idx()); - ASSERT(symAddrSym != nullptr, "null ptr check"); - EmitAddressString(symAddrSym->GetName()); + StIdx stIdx = symAddr.GetSymbolIndex(); + MIRSymbol *symAddrSym = stIdx.IsGlobal() ? GlobalTables::GetGsymTable().GetSymbolFromStidx(stIdx.Idx()) + : CG::GetCurCGFunc()->GetMirModule().CurFunction()->GetSymTab()->GetSymbolFromStIdx(stIdx.Idx()); + if (stIdx.IsGlobal() == false && symAddrSym->GetStorageClass() == kScPstatic) { + PUIdx pIdx = GetCG()->GetMIRModule()->CurFunction()->GetPuidx(); + Emit("\t.quad\t" + symAddrSym->GetName() + std::to_string(pIdx)); + } else { + Emit("\t.quad\t" + symAddrSym->GetName()); + } + if (symAddr.GetOffset() != 0) { + Emit(" + ").Emit(symAddr.GetOffset()); + } break; } case kConstAddrofFunc: { @@ -1373,7 +1457,18 @@ void Emitter::EmitArrayConstant(MIRConst &mirConst) { for (size_t i = 0; i < uNum; ++i) { MIRConst *elemConst = arrayCt.GetConstVecItem(i); if (IsPrimitiveScalar(elemConst->GetType().GetPrimType())) { - EmitScalarConstant(*elemConst); + if (cg->GetMIRModule()->IsCModule()) { + bool strLiteral = false; + if (arrayType.GetDim() == 1) { + MIRType *ety = arrayType.GetElemType(); + if (ety->GetPrimType() == PTY_i8 || ety->GetPrimType() == PTY_u8) { + strLiteral = true; + } + } + EmitScalarConstant(*elemConst, true, false, strLiteral == false); + } else { + EmitScalarConstant(*elemConst); + } } else if (elemConst->GetType().GetKind() == kTypeArray) { EmitArrayConstant(*elemConst); } else if (elemConst->GetType().GetKind() == kTypeStruct || elemConst->GetType().GetKind() == kTypeClass) { @@ -1408,8 +1503,8 @@ void Emitter::EmitStructConstant(MIRConst &mirConst) { uint32 fieldIdx = 1; for (uint32 i = 0; i < num; ++i) { if (((i + 1) == num) && cg->GetMIRModule()->GetSrcLang() == kSrcLangC) { - isFlexibleArray = Globals::GetInstance()->GetBECommon()->GetHasFlexibleArray(mirType.GetTypeIndex().GetIdx()); - arraySize = 0; + isFlexibleArray = Globals::GetInstance()->GetBECommon()->GetHasFlexibleArray(mirType.GetTypeIndex().GetIdx()); + arraySize = 0; } MIRConst *elemConst = structCt.GetAggConstElement(fieldIdx); MIRType &elemType = *structType.GetElemType(i); @@ -1435,7 +1530,7 @@ void Emitter::EmitStructConstant(MIRConst &mirConst) { } else { if (elemConst != nullptr) { if (IsPrimitiveScalar(elemType.GetPrimType())) { - EmitScalarConstant(*elemConst); + EmitScalarConstant(*elemConst, true, false, true); } else if (elemType.GetKind() == kTypeArray) { EmitArrayConstant(*elemConst); } else if ((elemType.GetKind() == kTypeStruct) || (elemType.GetKind() == kTypeClass)) { @@ -1728,6 +1823,65 @@ void Emitter::MarkVtabOrItabEndFlag(const std::vector &mirSymbolVec) } } +void Emitter::EmitStringPointers() { + Emit(asmInfo->GetSection()).Emit(asmInfo->GetData()).Emit("\n"); + for (auto idx: stringPtr) { + if (idx == 0) { + continue; + } + uint32 strId = idx.GetIdx(); + const char *str = GlobalTables::GetUStrTable().GetStringFromStrIdx(idx).c_str(); + Emit("\t.align 3\n"); + Emit(".LSTR__").Emit(strId).Emit(":\n"); + std::string mplstr(str); + EmitStr(mplstr, false, true); + } +} + +void Emitter::EmitLocalVariable(CGFunc &cgfunc) { + // function local pstatic initialization + if (cg->GetMIRModule()->IsCModule()) { + MIRSymbolTable *lSymTab = cgfunc.GetMirModule().CurFunction()->GetSymTab(); + if (lSymTab != nullptr) { + // anything larger than is created by cg + size_t lsize = cgfunc.GetLSymSize(); + for (size_t i = 0; i < lsize; i++) { + MIRSymbol *st = lSymTab->GetSymbolFromStIdx(i); + if (st != nullptr && st->GetStorageClass() == kScPstatic) { + // Local static names can repeat. + // Append the current program unit index to the name. + PUIdx pIdx = cgfunc.GetMirModule().CurFunction()->GetPuidx(); + std::string localname = st->GetName() + std::to_string(pIdx); + static std::vector emittedLocalSym; + bool found = false; + for (auto name : emittedLocalSym) { + if (name == localname) { + found = true; + break; + } + } + if (found) { + continue; + } + emittedLocalSym.push_back(localname); + + Emit(asmInfo->GetSection()); + Emit(asmInfo->GetData()); + Emit("\n"); + EmitAsmLabel(*st, kAsmAlign); + MIRConst *ct = st->GetKonst(); + if (ct == nullptr) { + EmitAsmLabel(*st, kAsmComm); + } else { + EmitAsmLabel(*st, kAsmSyname); + EmitScalarConstant(*ct, true, false, true/*isIndirect*/); + } + } + } + } + } +} + void Emitter::EmitGlobalVar(const MIRSymbol &globalVar) { EmitAsmLabel(globalVar, kAsmType); EmitAsmLabel(globalVar, kAsmLocal); @@ -2017,7 +2171,15 @@ void Emitter::EmitGlobalVariable() { EmitAsmLabel(*mirSymbol, kAsmSyname); MIRConst *mirConst = mirSymbol->GetKonst(); if (IsPrimitiveScalar(mirType->GetPrimType())) { - EmitScalarConstant(*mirConst); + if (IsAddress(mirType->GetPrimType())) { + uint32 sizeinbits = GetPrimTypeBitSize(mirConst->GetType().GetPrimType()); + CHECK_FATAL(sizeinbits == 64, "EmitGlobalVariable: pointer must be of size 8"); + } + if (cg->GetMIRModule()->IsCModule()) { + EmitScalarConstant(*mirConst, true, false, true); + } else { + EmitScalarConstant(*mirConst); + } } else if (mirType->GetKind() == kTypeArray) { if (mirSymbol->HasAddrOfValues()) { EmitConstantTable(*mirSymbol, *mirConst, strIdx2Type); @@ -2044,8 +2206,21 @@ void Emitter::EmitGlobalVariable() { EmitAsmLabel(*mirSymbol, kAsmSyname); MIRConst *mirConst = mirSymbol->GetKonst(); EmitScalarConstant(*mirConst); + } else if (mirSymbol->GetStorageClass() == kScPstatic) { + Emit(asmInfo->GetSection()); + Emit(asmInfo->GetData()); + Emit("\n"); + EmitAsmLabel(*mirSymbol, kAsmAlign); + MIRConst *ct = mirSymbol->GetKonst(); + if (ct == nullptr) { + EmitAsmLabel(*mirSymbol, kAsmComm); + } else { + EmitAsmLabel(*mirSymbol, kAsmSyname); + EmitScalarConstant(*ct, true, false, true); + } } } /* end proccess all mirSymbols. */ + EmitStringPointers(); /* emit global var */ EmitGlobalVars(globalVarVec); /* emit literal std::strings */ @@ -2462,7 +2637,9 @@ void ImmOperand::Dump() const { void LabelOperand::Emit(Emitter &emitter, const OpndProp *opndProp) const { (void)opndProp; - (void)emitter.Emit(".L.").Emit(parentFunc).Emit(".").Emit(labelIndex); + PUIdx pIdx = emitter.GetCG()->GetMIRModule()->CurFunction()->GetPuidx(); + const char *idx = strdup(std::to_string(pIdx).c_str()); + (void)emitter.Emit(".L.").Emit(idx).Emit("__").Emit(labelIndex); } void LabelOperand::Dump() const { diff --git a/src/mapleall/maple_driver/src/driver_runner.cpp b/src/mapleall/maple_driver/src/driver_runner.cpp index 0151575a75..0d0553b56d 100644 --- a/src/mapleall/maple_driver/src/driver_runner.cpp +++ b/src/mapleall/maple_driver/src/driver_runner.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) [2019-2020] Huawei Technologies Co.,Ltd.All rights reserved. + * Copyright (c) [2019-2021] Huawei Technologies Co.,Ltd.All rights reserved. * * OpenArkCompiler is licensed under Mulan PSL v2. * You can use this software according to the terms and conditions of the Mulan PSL v2. @@ -397,6 +397,8 @@ void DriverRunner::RunCGFunctions(CG &cg, CgFuncPhaseManager &cgfpm, std::vector cgfpm.Run(*cgFunc); + cg.GetEmitter()->EmitLocalVariable(*cgFunc); + // Invalid all analysis result. cgfpm.Emit(*cgFunc); cg.GetEmitter()->EmitHugeSoRoutines(); -- Gitee From e1c7748004b65f79447a40503fbb9d9a554676d6 Mon Sep 17 00:00:00 2001 From: Alfred Huang Date: Tue, 16 Feb 2021 13:59:37 -0800 Subject: [PATCH 2/2] Completed emit.cpp merge. --- src/mapleall/maple_be/src/be/becommon.cpp | 3 + src/mapleall/maple_be/src/cg/emit.cpp | 77 ++++++++++++++++++----- 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/src/mapleall/maple_be/src/be/becommon.cpp b/src/mapleall/maple_be/src/be/becommon.cpp index ba35b57fc3..da2e7bba69 100644 --- a/src/mapleall/maple_be/src/be/becommon.cpp +++ b/src/mapleall/maple_be/src/be/becommon.cpp @@ -146,6 +146,9 @@ void BECommon::ComputeStructTypeSizesAligns(MIRType &ty, const TyIdx &tyIdx) { allocedSizeInBits += fieldSize; allocedSize = std::max(allocedSize, RoundUp(allocedSizeInBits, fieldAlign * kBitsPerByte) / kBitsPerByte); + if (fieldSize == 0) { + allocedSizeInBits = allocedSize *8; + } } else { /* pad alloced_size according to the field alignment */ allocedSize = RoundUp(allocedSize, fieldAlign); diff --git a/src/mapleall/maple_be/src/cg/emit.cpp b/src/mapleall/maple_be/src/cg/emit.cpp index 262b41d0d8..913ebe5023 100644 --- a/src/mapleall/maple_be/src/cg/emit.cpp +++ b/src/mapleall/maple_be/src/cg/emit.cpp @@ -75,10 +75,10 @@ void Emitter::EmitStmtLabel(LabelIdx labIdx) { void Emitter::EmitLabelPair(const LabelPair &pairLabel) { ASSERT(pairLabel.GetEndOffset() || pairLabel.GetStartOffset(), "NYI"); - PUIdx pIdx = GetCG()->GetMIRModule()->CurFunction()->GetPuidx(); - const char *idx = strdup(std::to_string(pIdx).c_str()); - outStream << ".L." << idx << "__" << pairLabel.GetEndOffset()->GetLabelIdx() << " - " - << ".L." << idx << "__" << pairLabel.GetStartOffset()->GetLabelIdx() << "\n"; + EmitLabelRef(pairLabel.GetEndOffset()->GetLabelIdx()); + outStream << " - "; + EmitLabelRef(pairLabel.GetStartOffset()->GetLabelIdx()); + outStream << "\n"; } AsmLabel Emitter::GetTypeAsmInfoName(PrimType primType) const { @@ -305,8 +305,21 @@ void Emitter::EmitAsmLabel(const MIRSymbol &mirSymbol, AsmLabel label) { return; } case kAsmAlign: { - std::string align = std::to_string( - static_cast(log2(Globals::GetInstance()->GetBECommon()->GetTypeAlign(mirType->GetTypeIndex())))); + std::string align; + if (mirSymbol.GetType()->GetKind() == kTypeStruct || + mirSymbol.GetType()->GetKind() == kTypeClass || + mirSymbol.GetType()->GetKind() == kTypeArray || + mirSymbol.GetType()->GetKind() == kTypeUnion) { + align = "3"; + } else { +#if TARGARM32 || TARGAARCH64 || TARGARK || TARGRISCV64 + align = std::to_string(static_cast( + log2(Globals::GetInstance()->GetBECommon()->GetTypeAlign(mirSymbol.GetType()->GetTypeIndex())))); +#else + align = + std::to_string(Globals::GetInstance()->GetBECommon()->GetTypeAlign(mirSymbol.GetType()->GetTypeIndex())); +#endif + } Emit(asmInfo->GetAlign()); Emit(align); Emit("\n"); @@ -613,6 +626,12 @@ void Emitter::EmitScalarConstant(MIRConst &mirConst, bool newLine, bool flag32, (void)Emit("\t.quad\t" + symAddrSym->GetName()); break; } + case kConstLblConst: { + MIRLblConst &lbl = static_cast(mirConst); + Emit("\t.dword\t"); + EmitLabelRef(lbl.GetValue()); + break; + } default: ASSERT(false, "NYI"); break; @@ -1497,7 +1516,12 @@ void Emitter::EmitStructConstant(MIRConst &mirConst) { MIRStructType &structType = static_cast(mirType); ASSERT(structType.GetKind() != kTypeUnion, "NYI, not support now."); /* all elements of struct. */ - uint8 num = structType.GetFieldsSize(); + uint8 num; + if (structType.GetKind() == kTypeUnion) { + num = 1; + } else { + num = structType.GetFieldsSize(); + } /* total size of emitted elements size. */ uint32 size = Globals::GetInstance()->GetBECommon()->GetTypeSize(structType.GetTypeIndex()); uint32 fieldIdx = 1; @@ -1870,8 +1894,17 @@ void Emitter::EmitLocalVariable(CGFunc &cgfunc) { Emit("\n"); EmitAsmLabel(*st, kAsmAlign); MIRConst *ct = st->GetKonst(); + MIRType *ty = st->GetType(); if (ct == nullptr) { EmitAsmLabel(*st, kAsmComm); + } else if (kTypeStruct == ty->GetKind() || kTypeUnion == ty->GetKind() || kTypeClass == ty->GetKind()) { + EmitAsmLabel(*st, kAsmSyname); + EmitStructConstant(*ct); + } else if (kTypeArray == ty->GetKind()) { + if (ty->GetSize() != 0) { + EmitAsmLabel(*st, kAsmSyname); + EmitArrayConstant(*ct); + } } else { EmitAsmLabel(*st, kAsmSyname); EmitScalarConstant(*ct, true, false, true/*isIndirect*/); @@ -2033,11 +2066,15 @@ void Emitter::EmitGlobalVariable() { /* _PTR__cinf is emitted in dataDefTab and dataUndefTab */ continue; } else if (mirSymbol->IsMuidTab()) { - muidVec[0] = mirSymbol; - EmitMuidTable(muidVec, strIdx2Type, mirSymbol->GetMuidTabName()); + if (GetCG()->GetMIRModule()->IsJavaModule()) { + muidVec[0] = mirSymbol; + EmitMuidTable(muidVec, strIdx2Type, mirSymbol->GetMuidTabName()); + } continue; } else if (mirSymbol->IsCodeLayoutInfo()) { - EmitFuncLayoutInfo(*mirSymbol); + if (GetCG()->GetMIRModule()->IsJavaModule()) { + EmitFuncLayoutInfo(*mirSymbol); + } continue; } else if (mirSymbol->GetName().find(kStaticFieldNamePrefixStr) == 0) { staticFieldsVec.emplace_back(mirSymbol); @@ -2186,7 +2223,7 @@ void Emitter::EmitGlobalVariable() { } else { EmitArrayConstant(*mirConst); } - } else if (mirType->GetKind() == kTypeStruct || mirType->GetKind() == kTypeClass) { + } else if (mirType->GetKind() == kTypeStruct || mirType->GetKind() == kTypeClass || mirType->GetKind() == kTypeUnion) { if (mirSymbol->HasAddrOfValues()) { EmitConstantTable(*mirSymbol, *mirConst, strIdx2Type); } else { @@ -2214,9 +2251,17 @@ void Emitter::EmitGlobalVariable() { MIRConst *ct = mirSymbol->GetKonst(); if (ct == nullptr) { EmitAsmLabel(*mirSymbol, kAsmComm); - } else { + } else if (IsPrimitiveScalar(mirType->GetPrimType())) { EmitAsmLabel(*mirSymbol, kAsmSyname); EmitScalarConstant(*ct, true, false, true); + } else if (kTypeArray == mirType->GetKind()) { + EmitAsmLabel(*mirSymbol, kAsmSyname); + EmitArrayConstant(*ct); + } else if (kTypeStruct == mirType->GetKind() || kTypeClass == mirType->GetKind() || kTypeUnion == mirType->GetKind()) { + EmitAsmLabel(*mirSymbol, kAsmSyname); + EmitStructConstant(*ct); + } else { + CHECK_FATAL(0, "Unknown type in Global pstatic"); } } } /* end proccess all mirSymbols. */ @@ -2228,6 +2273,10 @@ void Emitter::EmitGlobalVariable() { /* emit static field std::strings */ EmitStaticFields(staticFieldsVec); + if (GetCG()->GetMIRModule()->IsCModule()) { + return; + } + EmitMuidTable(constStrVec, strIdx2Type, kMuidConststrPrefixStr); /* emit classinfo, field, method */ @@ -2637,9 +2686,7 @@ void ImmOperand::Dump() const { void LabelOperand::Emit(Emitter &emitter, const OpndProp *opndProp) const { (void)opndProp; - PUIdx pIdx = emitter.GetCG()->GetMIRModule()->CurFunction()->GetPuidx(); - const char *idx = strdup(std::to_string(pIdx).c_str()); - (void)emitter.Emit(".L.").Emit(idx).Emit("__").Emit(labelIndex); + emitter.EmitLabelRef(labelIndex); } void LabelOperand::Dump() const { -- Gitee