diff --git a/src/mapleall/maple_ir/include/keywords.def b/src/mapleall/maple_ir/include/keywords.def index 3172ca2553235abd2003043e9b911d8937e22731..2b2e82f90b7daf184c28ed0e5736d60470eb680a 100644 --- a/src/mapleall/maple_ir/include/keywords.def +++ b/src/mapleall/maple_ir/include/keywords.def @@ -99,3 +99,7 @@ // storage class KEYWORD(pstatic) KEYWORD(fstatic) + // file-scope asm + KEYWORD(asmdecl) + // section attribute + KEYWORD(section) diff --git a/src/mapleall/maple_ir/include/mir_module.h b/src/mapleall/maple_ir/include/mir_module.h index 68ceff7c5969ec82260c4d05c33a031ba45f7e43..5f36c6fd07f05684a4fb70fbd2a9ec149753bc9b 100644 --- a/src/mapleall/maple_ir/include/mir_module.h +++ b/src/mapleall/maple_ir/include/mir_module.h @@ -511,6 +511,10 @@ class MIRModule { importPaths.push_back(path); } + MapleVector &GetAsmDecls() { + return asmDecls; + } + const MapleSet &GetClassList() const { return classList; } @@ -633,6 +637,7 @@ class MIRModule { uint32 numFuncs = 0; // because puIdx 0 is reserved, numFuncs is also the highest puIdx MapleVector importFiles; MapleVector importPaths; + MapleVector asmDecls; MapleSet classList; std::map> method2TargetMap; diff --git a/src/mapleall/maple_ir/include/mir_nodes.h b/src/mapleall/maple_ir/include/mir_nodes.h index 138cc5a2833e38f6682819a8a6cd984924b825da..f39856f41254c3ed16c85bca82f52cbe780f39dc 100644 --- a/src/mapleall/maple_ir/include/mir_nodes.h +++ b/src/mapleall/maple_ir/include/mir_nodes.h @@ -29,6 +29,8 @@ namespace maple { extern MIRModule *theMIRModule; +extern void EmitStr(const MapleString &mplStr); + class MIRPregTable; // circular dependency exists, no other choice class TypeTable; // circular dependency exists, no other choice class VerifyResult; // circular dependency exists, no other choice diff --git a/src/mapleall/maple_ir/include/mir_parser.h b/src/mapleall/maple_ir/include/mir_parser.h index 01448aecdc733a5c657758c4b7aa6cc831debce5..f9e673a0ea8fefee1972c2f0131cb335b5505678 100644 --- a/src/mapleall/maple_ir/include/mir_parser.h +++ b/src/mapleall/maple_ir/include/mir_parser.h @@ -235,6 +235,7 @@ class MIRParser { bool ParseMIRForSrcFileInfo(); bool ParseMIRForImport(); bool ParseMIRForImportPath(); + bool ParseMIRForAsmdecl(); // func for ParseExpr using FuncPtrParseExpr = bool (MIRParser::*)(BaseNodePtr &ptr); diff --git a/src/mapleall/maple_ir/include/mir_symbol.h b/src/mapleall/maple_ir/include/mir_symbol.h index 036afe4779de01df9d602744fead63203d012cc9..8543c9cc2376ca09acfa78c545e45a5bdffc323d 100644 --- a/src/mapleall/maple_ir/include/mir_symbol.h +++ b/src/mapleall/maple_ir/include/mir_symbol.h @@ -463,6 +463,9 @@ class MIRSymbol { StIdx stIdx { 0, 0 }; TypeAttrs typeAttrs; GStrIdx nameStrIdx{ 0 }; + public: + UStrIdx sectionAttr { 0 }; // if not 0, the string for the name in C's section attribute + private: SymbolType value = { nullptr }; SrcPosition srcPosition; // where the symbol is defined // following cannot be assumed final even though they are declared final diff --git a/src/mapleall/maple_ir/include/prim_types.def b/src/mapleall/maple_ir/include/prim_types.def index dee82b5488bbc40eee9765918e45df975aff7a90..3d2324f0c5b8f14a58f71a95e0bb267f4848695e 100644 --- a/src/mapleall/maple_ir/include/prim_types.def +++ b/src/mapleall/maple_ir/include/prim_types.def @@ -20,10 +20,12 @@ PRIMTYPE(i16) PRIMTYPE(i32) PRIMTYPE(i64) + PRIMTYPE(i128) PRIMTYPE(u8) PRIMTYPE(u16) PRIMTYPE(u32) PRIMTYPE(u64) + PRIMTYPE(u128) PRIMTYPE(u1) PRIMTYPE(ptr) PRIMTYPE(ref) @@ -68,6 +70,16 @@ PRIMTYPE(v4u16) PRIMTYPE(v8u8) PRIMTYPE(v2f32) + PRIMTYPE(reservedpty1) + PRIMTYPE(reservedpty2) + PRIMTYPE(reservedpty3) + PRIMTYPE(reservedpty4) + PRIMTYPE(reservedpty5) + PRIMTYPE(reservedpty6) + PRIMTYPE(reservedpty7) + PRIMTYPE(reservedpty8) + PRIMTYPE(reservedpty9) + PRIMTYPE(reservedpty10) PRIMTYPE(unknown) #endif // ~LOAD_ALGO_PRIMARY_TYPE @@ -111,6 +123,12 @@ static const PrimitiveTypeProperty PTProperty_i64 = { /*isVector*/false }; +static const PrimitiveTypeProperty PTProperty_i128 = { + /*type=*/PTY_i128, /*isInteger=*/true, /*isUnsigned=*/false, /*isAddress=*/false, /*isFloat=*/false, + /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, + /*isVector*/false +}; + static const PrimitiveTypeProperty PTProperty_u8 = { /*type=*/PTY_u8, /*isInteger=*/true, /*isUnsigned=*/true, /*isAddress=*/false, /*isFloat=*/false, /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, @@ -135,6 +153,12 @@ static const PrimitiveTypeProperty PTProperty_u64 = { /*isVector*/false }; +static const PrimitiveTypeProperty PTProperty_u128 = { + /*type=*/PTY_u128, /*isInteger=*/true, /*isUnsigned=*/true, /*isAddress=*/false, /*isFloat=*/false, + /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, + /*isVector*/false +}; + static const PrimitiveTypeProperty PTProperty_u1 = { /*type=*/PTY_u1, /*isInteger=*/true, /*isUnsigned=*/true, /*isAddress=*/false, /*isFloat=*/false, /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, @@ -389,10 +413,64 @@ static const PrimitiveTypeProperty PTProperty_v2f32 = { /*isVector*/true }; -static const PrimitiveTypeProperty PTProperty_end = { - /*type=*/PTY_end, /*isInteger=*/false, /*isUnsigned=*/false, /*isAddress=*/false, /*isFloat=*/false, +static const PrimitiveTypeProperty PTProperty_reservedpty1 = { + /*type=*/PTY_reservedpty1, /*isInteger=*/false, /*isUnsigned=*/false, /*isAddress=*/false, /*isFloat=*/false, /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, - /*isVector*/true + /*isVector*/false +}; + +static const PrimitiveTypeProperty PTProperty_reservedpty2 = { + /*type=*/PTY_reservedpty2, /*isInteger=*/false, /*isUnsigned=*/false, /*isAddress=*/false, /*isFloat=*/false, + /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, + /*isVector*/false +}; + +static const PrimitiveTypeProperty PTProperty_reservedpty3 = { + /*type=*/PTY_reservedpty3, /*isInteger=*/false, /*isUnsigned=*/false, /*isAddress=*/false, /*isFloat=*/false, + /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, + /*isVector*/false +}; + +static const PrimitiveTypeProperty PTProperty_reservedpty4 = { + /*type=*/PTY_reservedpty4, /*isInteger=*/false, /*isUnsigned=*/false, /*isAddress=*/false, /*isFloat=*/false, + /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, + /*isVector*/false +}; + +static const PrimitiveTypeProperty PTProperty_reservedpty5 = { + /*type=*/PTY_reservedpty5, /*isInteger=*/false, /*isUnsigned=*/false, /*isAddress=*/false, /*isFloat=*/false, + /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, + /*isVector*/false +}; + +static const PrimitiveTypeProperty PTProperty_reservedpty6 = { + /*type=*/PTY_reservedpty6, /*isInteger=*/false, /*isUnsigned=*/false, /*isAddress=*/false, /*isFloat=*/false, + /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, + /*isVector*/false +}; + +static const PrimitiveTypeProperty PTProperty_reservedpty7 = { + /*type=*/PTY_reservedpty7, /*isInteger=*/false, /*isUnsigned=*/false, /*isAddress=*/false, /*isFloat=*/false, + /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, + /*isVector*/false +}; + +static const PrimitiveTypeProperty PTProperty_reservedpty8 = { + /*type=*/PTY_reservedpty8, /*isInteger=*/false, /*isUnsigned=*/false, /*isAddress=*/false, /*isFloat=*/false, + /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, + /*isVector*/false +}; + +static const PrimitiveTypeProperty PTProperty_reservedpty9 = { + /*type=*/PTY_reservedpty9, /*isInteger=*/false, /*isUnsigned=*/false, /*isAddress=*/false, /*isFloat=*/false, + /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, + /*isVector*/false +}; + +static const PrimitiveTypeProperty PTProperty_reservedpty10 = { + /*type=*/PTY_reservedpty10, /*isInteger=*/false, /*isUnsigned=*/false, /*isAddress=*/false, /*isFloat=*/false, + /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, + /*isVector*/false }; static const PrimitiveTypeProperty PTProperty_unknown = { @@ -401,4 +479,10 @@ static const PrimitiveTypeProperty PTProperty_unknown = { /*isVector*/false }; +static const PrimitiveTypeProperty PTProperty_end = { + /*type=*/PTY_end, /*isInteger=*/false, /*isUnsigned=*/false, /*isAddress=*/false, /*isFloat=*/false, + /*isPointer=*/false, /*isSimple=*/false, /*isDynamic=*/false, /*isDynamicAny=*/false, /*isDynamicNone=*/false, + /*isVector*/true +}; + #endif // ~LOAD_PRIMARY_TYPE_PROPERTY diff --git a/src/mapleall/maple_ir/src/bin_mpl_export.cpp b/src/mapleall/maple_ir/src/bin_mpl_export.cpp index 22bc6768270942bfcdb4cc01c8a7af96ba4354bf..611e7bca18972c68f649266c1c5e5a97e0d1d2d6 100644 --- a/src/mapleall/maple_ir/src/bin_mpl_export.cpp +++ b/src/mapleall/maple_ir/src/bin_mpl_export.cpp @@ -620,6 +620,7 @@ void BinaryMplExport::OutputSymbol(MIRSymbol *sym) { WriteNum(kBinSymbol); WriteNum(sym->GetScopeIdx()); OutputStr(sym->GetNameStrIdx()); + OutputUsrStr(sym->sectionAttr); WriteNum(sym->GetSKind()); WriteNum(sym->GetStorageClass()); size_t mark = symMark.size(); @@ -751,6 +752,12 @@ void BinaryMplExport::WriteHeaderField(uint64 contentIdx) { OutputStr(strIdx); } + WriteNum(mod.GetAsmDecls().size()); + for (MapleString mapleStr : mod.GetAsmDecls()) { + std::string str(mapleStr.c_str()); + WriteAsciiStr(str); + } + Fixup(totalSizeIdx, buf.size() - totalSizeIdx); WriteNum(~kBinHeaderStart); return; diff --git a/src/mapleall/maple_ir/src/bin_mpl_import.cpp b/src/mapleall/maple_ir/src/bin_mpl_import.cpp index 5b769948fcd542520f6df32bf68752f068ac5103..d105063e6fbcae0fe33eceea7f4317eae737c618 100644 --- a/src/mapleall/maple_ir/src/bin_mpl_import.cpp +++ b/src/mapleall/maple_ir/src/bin_mpl_import.cpp @@ -973,10 +973,14 @@ MIRSymbol *BinaryMplImport::InSymbol(MIRFunction *func) { CHECK_FATAL(tag == kBinSymbol, "expecting kBinSymbol"); int64 scope = ReadNum(); GStrIdx stridx = ImportStr(); + UStrIdx secAttr = ImportUsrStr(); auto skind = static_cast(ReadNum()); auto sclass = static_cast(ReadNum()); TyIdx tyTmp(0); MIRSymbol *sym = GetOrCreateSymbol(tyTmp, stridx, skind, sclass, func, scope); + if (secAttr != 0) { + sym->sectionAttr = secAttr; + } symTab.push_back(sym); sym->SetAttrs(ImportTypeAttrs()); sym->SetIsTmp(ReadNum() != 0); @@ -1166,6 +1170,14 @@ void BinaryMplImport::ReadHeaderField() { mod.SetBinMplt(binMplt); } } + + size = ReadNum(); + for (int32 i = 0; i < size; i++) { + std::string str; + ReadAsciiStr(str); + mod.GetAsmDecls().push_back(MapleString(str, mod.GetMemPool())); + } + int32 tag = ReadNum(); CHECK_FATAL(tag == ~kBinHeaderStart, "pattern mismatch in Read Import"); return; diff --git a/src/mapleall/maple_ir/src/mir_module.cpp b/src/mapleall/maple_ir/src/mir_module.cpp index e401c5de06cb87f9d6cf29e61736e0283ff5dc48..c04ab297700654074693fc0196b107dd926454ab 100644 --- a/src/mapleall/maple_ir/src/mir_module.cpp +++ b/src/mapleall/maple_ir/src/mir_module.cpp @@ -49,6 +49,7 @@ MIRModule::MIRModule(const std::string &fn) srcFileInfo(memPoolAllocator.Adapter()), importFiles(memPoolAllocator.Adapter()), importPaths(memPoolAllocator.Adapter()), + asmDecls(memPoolAllocator.Adapter()), classList(memPoolAllocator.Adapter()), optimizedFuncs(memPoolAllocator.Adapter()), puIdxFieldInitializedMap(std::less(), memPoolAllocator.Adapter()), @@ -165,6 +166,13 @@ void MIRModule::DumpGlobals(bool emitStructureType) const { << "\"\n"; } } + if (!asmDecls.empty()) { + size_t size = asmDecls.size(); + for (size_t i = 0; i < size; ++i) { + LogInfo::MapleLogger() << "asmdecl "; + EmitStr(asmDecls[i]); + } + } if (entryFuncName.length()) { LogInfo::MapleLogger() << "entryfunc &" << entryFuncName << '\n'; } diff --git a/src/mapleall/maple_ir/src/mir_nodes.cpp b/src/mapleall/maple_ir/src/mir_nodes.cpp index e42df2e5dfeb13751e8ffab79ed2977109292c9c..e22c63debbd299b6af1cabcf1d31bd08f4a23e78 100644 --- a/src/mapleall/maple_ir/src/mir_nodes.cpp +++ b/src/mapleall/maple_ir/src/mir_nodes.cpp @@ -1176,7 +1176,7 @@ void CommentNode::Dump(int32 indent) const { LogInfo::MapleLogger() << "#" << comment << '\n'; } -static void EmitStr(const MapleString &mplStr) { +void EmitStr(const MapleString &mplStr) { const char *str = mplStr.c_str(); size_t len = mplStr.length(); LogInfo::MapleLogger() << "\""; diff --git a/src/mapleall/maple_ir/src/mir_symbol.cpp b/src/mapleall/maple_ir/src/mir_symbol.cpp index c193d4edfe4ac7097a422f5de1187714a773ee11..66960d5c93beae0ba5e36c27d746797809c9013d 100644 --- a/src/mapleall/maple_ir/src/mir_symbol.cpp +++ b/src/mapleall/maple_ir/src/mir_symbol.cpp @@ -352,6 +352,11 @@ void MIRSymbol::Dump(bool isLocal, int32 indent, bool suppressInit, const MIRSym if (GetTyIdx() != 0u) { GlobalTables::GetTypeTable().GetTypeFromTyIdx(GetTyIdx())->Dump(indent + 1); } + if (sectionAttr != UStrIdx(0)) { + LogInfo::MapleLogger() << " section ("; + PrintString(GlobalTables::GetUStrTable().GetStringFromStrIdx(sectionAttr)); + LogInfo::MapleLogger() << " )"; + } typeAttrs.DumpAttributes(); if (sKind == kStJavaClass || sKind == kStJavaInterface || GetStorageClass() == kScTypeInfoName || GetStorageClass() == kScTypeInfo || GetStorageClass() == kScTypeCxxAbi) { diff --git a/src/mapleall/maple_ir/src/parser.cpp b/src/mapleall/maple_ir/src/parser.cpp index b26d71680202e77e979318b8cbd0e43023226c0c..59e9306159fc5229a21fb45ead42736d86645c94 100644 --- a/src/mapleall/maple_ir/src/parser.cpp +++ b/src/mapleall/maple_ir/src/parser.cpp @@ -1734,6 +1734,26 @@ bool MIRParser::ParseDeclareVar(MIRSymbol &symbol) { return false; } symbol.SetTyIdx(tyIdx); + if (lexer.GetTokenKind() == TK_section) { // parse section attribute + lexer.NextToken(); + if (lexer.GetTokenKind() != TK_lparen) { + Error("expect ( for section attribute but get "); + return false; + } + lexer.NextToken(); + if (lexer.GetTokenKind() != TK_string) { + Error("expect string literal for section attribute but get "); + return false; + } + UStrIdx literalStrIdx = GlobalTables::GetUStrTable().GetOrCreateStrIdxFromName(lexer.GetName()); + symbol.sectionAttr = literalStrIdx; + lexer.NextToken(); + if (lexer.GetTokenKind() != TK_rparen) { + Error("expect ) for section attribute but get "); + return false; + } + lexer.NextToken(); + } if (!ParseVarTypeAttrs(symbol)) { Error("bad type attribute in variable declaration at "); return false; @@ -2432,6 +2452,7 @@ std::map MIRParser::InitFuncPtrMap funcPtrMap[TK_srcfileinfo] = &MIRParser::ParseMIRForSrcFileInfo; funcPtrMap[TK_import] = &MIRParser::ParseMIRForImport; funcPtrMap[TK_importpath] = &MIRParser::ParseMIRForImportPath; + funcPtrMap[TK_asmdecl] = &MIRParser::ParseMIRForAsmdecl; funcPtrMap[TK_LOC] = &MIRParser::ParseLoc; return funcPtrMap; } @@ -2476,10 +2497,14 @@ bool MIRParser::ParseMIRForVar() { if (prevSt->GetStorageClass() == kScExtern) { prevSt->SetStorageClass(st.GetStorageClass()); } + if (prevSt->sectionAttr == UStrIdx(0)) { + prevSt->sectionAttr = st.sectionAttr; + } } else if (prevSt->GetSKind() == maple::kStVar && prevSt->GetStorageClass() == maple::kScInvalid) { prevSt->SetStorageClass(kScGlobal); prevSt->SetAttrs(st.GetAttrs()); prevSt->SetNameStrIdx(st.GetNameStrIdx()); + prevSt->sectionAttr = st.sectionAttr; prevSt->SetValue(st.GetValue()); prevSt->SetTyIdx(st.GetTyIdx()); SetSrcPos(prevSt->GetSrcPosition(), lexer.GetLineNum()); @@ -2497,6 +2522,7 @@ bool MIRParser::ParseMIRForVar() { } newst->SetAttrs(st.GetAttrs()); newst->SetNameStrIdx(st.GetNameStrIdx()); + newst->sectionAttr = st.sectionAttr; newst->SetValue(st.GetValue()); SetSrcPos(newst->GetSrcPosition(), lexer.GetLineNum()); if (!ParseDeclareVarInitValue(*newst)) { @@ -2803,6 +2829,17 @@ bool MIRParser::ParseMIRForImportPath() { return true; } +bool MIRParser::ParseMIRForAsmdecl() { + lexer.NextToken(); + if (lexer.GetTokenKind() != TK_string) { + Error("expect asm string after iasm but get "); + return false; + } + mod.GetAsmDecls().push_back(MapleString(lexer.GetName(), mod.GetMemPool())); + lexer.NextToken(); + return true; +} + void MIRParser::PrepareParsingMIR() { dummyFunction = CreateDummyFunction(); mod.SetCurFunction(dummyFunction);