12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053 |
- #include "BeIRCodeGen.h"
- #include "../Compiler/BfIRCodeGen.h"
- #include "BeDbgModule.h"
- #include "BeefySysLib/util/BeefPerf.h"
- #include "BeefySysLib/util/AllocDebug.h"
- USING_NS_BF;
- //#define CODEGEN_TRACK
- #ifdef CODEGEN_TRACK
- #include "../Compiler/MemReporter.h"
- MemReporter gBEMemReporter;
- CritSect gBEMemReporterCritSect;
- int gBEMemReporterSize = 0;
- #define BE_MEM_START \
- int startPos = mStream->GetReadPos();
- #define BE_MEM_END(name) \
- gBEMemReporter.Add(name, mStream->GetReadPos() - startPos);
- static const char* gIRCmdNames[] =
- {
- "Module_Start",
- "Module_SetTargetTriple",
- "Module_AddModuleFlag",
- "WriteIR",
- "SetType",
- "SetInstType",
- "PrimitiveType",
- "CreateStruct",
- "StructSetBody",
- "Type",
- "TypeInst",
- "TypeInstPtr",
- "GetType",
- "GetPointerToFuncType",
- "GetPointerToType",
- "GetSizedArrayType",
- "CreateConstStruct",
- "CreateConstStructZero",
- "CreateConstArray",
- "CreateConstString",
- "SetName",
- "CreateUndefValue",
- "NumericCast",
- "CmpEQ",
- "CmpNE",
- "CmpSLT",
- "CmpULT",
- "CmpSLE",
- "CmpULE",
- "CmpSGT",
- "CmpUGT",
- "CmpSGE",
- "CmpUGE",
- "Add",
- "Sub",
- "Mul",
- "SDiv",
- "UDiv",
- "SRem",
- "URem",
- "And",
- "Or",
- "Xor",
- "Shl",
- "AShr",
- "LShr",
- "Neg",
- "Not",
- "BitCast",
- "PtrToInt",
- "IntToPtr",
- "InboundsGEP1_32",
- "InboundsGEP2_32",
- "InBoundsGEP1",
- "InBoundsGEP2",
- "IsNull",
- "IsNotNull",
- "ExtractValue",
- "InsertValue",
- "Alloca",
- "AllocaArray",
- "SetAllocaAlignment",
- "SetAllocaNoChkStkHint",
- "SetAllocaForceMem",
- "LifetimeStart",
- "LifetimeEnd",
- "LifetimeExtend",
- "ValueScopeStart",
- "ValueScopeRetain",
- "ValueScopeSoftEnd",
- "ValueScopeHardEnd",
- "Load",
- "AlignedLoad",
- "Store",
- "AlignedStore",
- "MemSet",
- "Fence",
- "StackSave",
- "StackRestore",
- "GlobalVariable",
- "GlobalVar_SetUnnamedAddr",
- "GlobalVar_SetInitializer",
- "GlobalVar_SetAlignment",
- "GlobalStringPtr",
- "CreateBlock",
- "MaybeChainNewBlock",
- "AddBlock",
- "DropBlocks",
- "MergeBlockDown",
- "SetInsertPoint",
- "SetInsertPointAtStart",
- "EraseFromParent",
- "DeleteBlock",
- "EraseInstFromParent",
- "CreateBr",
- "CreateBr_NoCollapse",
- "CreateCondBr",
- "MoveBlockToEnd",
- "CreateSwitch",
- "AddSwitchCase",
- "SetSwitchDefaultDest",
- "CreatePhi",
- "AddPhiIncoming",
- "GetIntrinsic",
- "CreateFunctionType",
- "CreateFunction",
- "EnsureFunctionPatchable",
- "RemapBindFunction",
- "SetActiveFunction",
- "CreateCall",
- "SetCallCallingConv",
- "SetFuncCallingConv",
- "SetTailCall",
- "SetCallAttribute",
- "CreateRet",
- "CreateRetVoid",
- "CreateUnreachable",
- "Call_AddAttribute",
- "Call_AddAttribute1",
- "Func_AddAttribute",
- "Func_AddAttribute1",
- "Func_SetParamName",
- "Func_DeleteBody",
- "Func_EraseFromParent",
- "Func_SetLinkage",
- "SaveDebugLocation",
- "RestoreDebugLocation",
- "ClearDebugLocation",
- "ClearDebugLocationInst",
- "UpdateDebugLocation",
- "SetCurrentDebugLocation",
- "Nop",
- "EnsureInstructionAt",
- "StatementStart",
- "ObjectAccessCheck",
- "DbgInit",
- "DbgFinalize",
- "DbgCreateCompileUnit",
- "DbgCreateFile",
- "ConstValueI64",
- "DbgGetCurrentLocation",
- "DbgSetType",
- "DbgSetInstType",
- "DbgGetType",
- "DbgGetTypeInst",
- "DbgTrackDITypes",
- "DbgCreateNamespace",
- "DbgCreateImportedModule",
- "DbgCreateBasicType",
- "DbgCreateStructType",
- "DbgCreateEnumerationType",
- "DbgCreatePointerType",
- "DbgCreateReferenceType",
- "DbgCreateConstType",
- "DbgCreateArtificialType",
- "DbgCreateArrayType",
- "DbgCreateReplaceableCompositeType",
- "DbgCreateForwardDecl",
- "DbgCreateSizedForwardDecl",
- "BeIRCmd_DbgSetTypeSize",
- "DbgReplaceAllUses",
- "DbgDeleteTemporary",
- "DbgMakePermanent",
- "CreateEnumerator",
- "DbgCreateMemberType",
- "DbgStaticCreateMemberType",
- "DbgCreateInheritance",
- "DbgCreateMethod",
- "DbgCreateFunction",
- "DbgCreateParameterVariable",
- "DbgCreateSubroutineType",
- "DbgCreateAutoVariable",
- "DbgInsertValueIntrinsic",
- "DbgInsertDeclare",
- "DbgLifetimeEnd",
- "DbgCreateGlobalVariable",
- "DbgCreateLexicalBlock",
- "DbgCreateLexicalBlockFile",
- "DbgCreateAnnotation"
- };
- BF_STATIC_ASSERT(BF_ARRAY_COUNT(gIRCmdNames) == BfIRCmd_COUNT);
- #else
- #define BE_MEM_START
- #define BE_MEM_END(name)
- #endif
- #pragma warning(disable:4146)
- #define CMD_PARAM(ty, name) ty name; Read(name);
- template <typename T>
- class CmdParamVec : public SizedArray<T, 8>
- {};
- BeIRCodeGen::BeIRCodeGen()
- {
- mBfIRBuilder = NULL;
- mStream = NULL;
- mActiveFunction = NULL;
- mBeContext = NULL;
- mBeModule = NULL;
- mHasDebugLoc = false;
- mDebugging = false;
- mCmdCount = 0;
- }
- BeIRCodeGen::~BeIRCodeGen()
- {
- BF_ASSERT(mSavedDebugLocs.size() == 0);
- delete mBeModule;
- delete mBeContext;
- delete mStream;
- }
- void BeIRCodeGen::Hash(BeHashContext& hashCtx)
- {
- hashCtx.Mixin(mPtrSize);
- hashCtx.Mixin(mIsOptimized);
- if (mBeModule != NULL)
- mBeModule->Hash(hashCtx);
- }
- bool BeIRCodeGen::IsModuleEmpty()
- {
- if (!mBeModule->mFunctions.IsEmpty())
- return false;
- if (!mBeModule->mGlobalVariables.IsEmpty())
- return false;
- return true;
- }
- void BeIRCodeGen::NotImpl()
- {
- BF_FATAL("Not implemented");
- }
- BeType* BeIRCodeGen::GetBeType(BfTypeCode typeCode, bool& isSigned)
- {
- isSigned = false;
- BeTypeCode beTypeCode = BeTypeCode_None;
- switch (typeCode)
- {
- case BfTypeCode_None:
- beTypeCode = BeTypeCode_None;
- break;
- case BfTypeCode_NullPtr:
- beTypeCode = BeTypeCode_NullPtr;
- break;
- case BfTypeCode_Boolean:
- beTypeCode = BeTypeCode_Boolean;
- break;
- case BfTypeCode_Int8:
- isSigned = true;
- beTypeCode = BeTypeCode_Int8;
- break;
- case BfTypeCode_UInt8:
- case BfTypeCode_Char8:
- beTypeCode = BeTypeCode_Int8;
- break;
- case BfTypeCode_Int16:
- isSigned = true;
- beTypeCode = BeTypeCode_Int16;
- break;
- case BfTypeCode_Char16:
- case BfTypeCode_UInt16:
- beTypeCode = BeTypeCode_Int16;
- break;
- case BfTypeCode_Int32:
- isSigned = true;
- beTypeCode = BeTypeCode_Int32;
- break;
- case BfTypeCode_UInt32:
- case BfTypeCode_Char32:
- beTypeCode = BeTypeCode_Int32;
- break;
- case BfTypeCode_Int64:
- isSigned = true;
- beTypeCode = BeTypeCode_Int64;
- break;
- case BfTypeCode_UInt64:
- beTypeCode = BeTypeCode_Int64;
- break;
- case BfTypeCode_IntPtr:
- BF_FATAL("Illegal");
- /*isSigned = true;
- if (mModule->mSystem->mPtrSize == 4)
- return llvm::Type::getInt32Ty(*mLLVMContext);
- else
- return llvm::Type::getInt64Ty(*mLLVMContext);*/
- case BfTypeCode_UIntPtr:
- BF_FATAL("Illegal");
- /*if (mModule->mSystem->mPtrSize == 4)
- return llvm::Type::getInt32Ty(*mLLVMContext);
- else
- return llvm::Type::getInt64Ty(*mLLVMContext);*/
- case BfTypeCode_Single:
- beTypeCode = BeTypeCode_Float;
- break;
- case BfTypeCode_Double:
- beTypeCode = BeTypeCode_Double;
- break;
- }
- return mBeContext->GetPrimitiveType(beTypeCode);
- }
- BeIRTypeEntry& BeIRCodeGen::GetTypeEntry(int typeId)
- {
- BeIRTypeEntry& typeEntry = mTypes[typeId];
- if (typeEntry.mTypeId == -1)
- typeEntry.mTypeId = typeId;
- return typeEntry;
- }
- void BeIRCodeGen::Init(const BfSizedArray<uint8>& buffer)
- {
- BP_ZONE("BeIRCodeGen::Init");
- BF_ASSERT(mStream == NULL);
- mStream = new ChunkedDataBuffer();
- mStream->InitFlatRef(buffer.mVals, buffer.mSize);
- #ifdef CODEGEN_TRACK
- AutoCrit autoCrit(gBEMemReporterCritSect);
- AutoMemReporter autoMemReporter(&gBEMemReporter, "BeIRCodeGen");
- #endif
- //
- {
- BP_ZONE("BeIRCodeGen::ProcessBfIRData.HandleNextCmds");
- while (mStream->GetReadPos() < buffer.mSize)
- {
- if (mFailed)
- break;
- HandleNextCmd();
- }
- }
- BF_ASSERT((mFailed) || (mStream->GetReadPos() == buffer.mSize));
- }
- void BeIRCodeGen::Process()
- {
- BP_ZONE("BeIRCodeGen::process");
- //mDebugging |= ((mBeModule->mDbgModule != NULL) && (mBeModule->mDbgModule->mFileName == "ClassQ"));
- if (mDebugging)
- {
- String dbgStr;
- dbgStr = mBeModule->ToString();
- OutputDebugStr(dbgStr);
- }
- mBeModule->DoInlining();
- if (mDebugging)
- {
- String dbgStr = "-------------- AFTER INLINING --------------\n";
- dbgStr += mBeModule->ToString();
- OutputDebugStr(dbgStr);
- }
- }
- void BeIRCodeGen::ProcessBfIRData(const BfSizedArray<uint8>& buffer)
- {
- BP_ZONE("BeIRCodeGen::ProcessBfIRData");
- Init(buffer);
- Process();
- }
- BfTypeCode BeIRCodeGen::GetTypeCode(BeType * type, bool isSigned)
- {
- switch (type->mTypeCode)
- {
- case BeTypeCode_Int8:
- return (isSigned) ? BfTypeCode_Int8 : BfTypeCode_UInt8;
- case BeTypeCode_Int16:
- return (isSigned) ? BfTypeCode_Int16 : BfTypeCode_UInt16;
- case BeTypeCode_Int32:
- return (isSigned) ? BfTypeCode_Int32 : BfTypeCode_UInt32;
- case BeTypeCode_Int64:
- return (isSigned) ? BfTypeCode_Int64 : BfTypeCode_UInt64;
- case BeTypeCode_Float:
- return BfTypeCode_Single;
- case BeTypeCode_Double:
- return BfTypeCode_Double;
- default:
- return BfTypeCode_None;
- }
- }
- void BeIRCodeGen::SetResult(int id, BeValue* value)
- {
- BeIRCodeGenEntry entry;
- entry.mKind = BeIRCodeGenEntryKind_Value;
- entry.mBeValue = value;
- mResults.TryAdd(id, entry);
- }
- void BeIRCodeGen::SetResult(int id, BeType* type)
- {
- BeIRCodeGenEntry entry;
- entry.mKind = BeIRCodeGenEntryKind_Type;
- entry.mBeType = type;
- mResults.TryAdd(id, entry);
- }
- void BeIRCodeGen::SetResult(int id, BeBlock* value)
- {
- BeIRCodeGenEntry entry;
- entry.mKind = BeIRCodeGenEntryKind_Block;
- entry.mBeBlock = value;
- mResults.TryAdd(id, entry);
- }
- void BeIRCodeGen::SetResult(int id, BeMDNode* md)
- {
- BeIRCodeGenEntry entry;
- entry.mKind = BeIRCodeGenEntryKind_Metadata;
- entry.mBeMetadata = md;
- mResults.TryAdd(id, entry);
- }
- int64 BeIRCodeGen::ReadSLEB128()
- {
- int64 val = 0;
- int64 shift = 0;
- uint8 byteVal;
- do
- {
- byteVal = mStream->Read();
- val |= ((int64)(byteVal & 0x7f)) << shift;
- shift += 7;
- } while (byteVal >= 128);
- // Sign extend negative numbers.
- if ((byteVal & 0x40) && (shift < 64))
- val |= (-1ULL) << shift;
- return val;
- }
- void BeIRCodeGen::Read(StringImpl& str)
- {
- BE_MEM_START;
- int len = (int)ReadSLEB128();
- str.Append('?', len);
- mStream->Read((void*)str.c_str(), len);
- BE_MEM_END("String");
- }
- void BeIRCodeGen::Read(int& i)
- {
- BE_MEM_START;
- i = (int)ReadSLEB128();
- BE_MEM_END("int");
- }
- void BeIRCodeGen::Read(int64& i)
- {
- BE_MEM_START;
- i = ReadSLEB128();
- BE_MEM_END("int64");
- }
- void BeIRCodeGen::Read(bool& val)
- {
- BE_MEM_START;
- val = mStream->Read() != 0;
- BE_MEM_END("bool");
- }
- void BeIRCodeGen::Read(BeIRTypeEntry*& type)
- {
- BE_MEM_START;
- int typeId = (int)ReadSLEB128();
- type = &GetTypeEntry(typeId);
- BE_MEM_END("BeIRTypeEntry");
- }
- void BeIRCodeGen::Read(BeType*& beType)
- {
- BE_MEM_START;
- BfIRType::TypeKind typeKind = (BfIRType::TypeKind)mStream->Read();
- if (typeKind == BfIRType::TypeKind::TypeKind_None)
- {
- beType = NULL;
- BE_MEM_END("BeType");
- return;
- }
- if (typeKind == BfIRType::TypeKind::TypeKind_Stream)
- {
- int streamId = (int)ReadSLEB128();
- if (streamId == -1)
- {
- beType = NULL;
- BE_MEM_END("BeType");
- return;
- }
- auto& result = mResults[streamId];
- BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Type);
- beType = result.mBeType;
- BE_MEM_END("BeType");
- return;
- }
- int typeId = (int)ReadSLEB128();
- auto& typeEntry = GetTypeEntry(typeId);
- if (typeKind == BfIRType::TypeKind::TypeKind_TypeId)
- beType = typeEntry.mBeType;
- else if (typeKind == BfIRType::TypeKind::TypeKind_TypeInstId)
- beType = typeEntry.mInstBeType;
- else if (typeKind == BfIRType::TypeKind::TypeKind_TypeInstPtrId)
- beType = mBeContext->GetPointerTo(typeEntry.mInstBeType);
- BE_MEM_END("BeType");
- }
- void BeIRCodeGen::Read(BeFunctionType*& beType)
- {
- BE_MEM_START;
- int streamId = (int)ReadSLEB128();
- auto& result = mResults[streamId];
- BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Type);
- beType = (BeFunctionType*)result.mBeType;
- BE_MEM_END("BeFunctionType");
- }
- void BeIRCodeGen::Read(BeValue*& beValue)
- {
- BE_MEM_START;
- BfIRParamType paramType = (BfIRParamType)mStream->Read();
- if (paramType == BfIRParamType_None)
- {
- beValue = NULL;
- BE_MEM_END("ParamType_None");
- }
- else if (paramType == BfIRParamType_Const)
- {
- BfTypeCode typeCode = (BfTypeCode)mStream->Read();
- BfConstType constType = (BfConstType)typeCode;
- if (constType == BfConstType_GlobalVar)
- {
- CMD_PARAM(int, streamId);
- if (streamId == -1)
- {
- int streamId = mCmdCount++;
- CMD_PARAM(BeType*, varType);
- CMD_PARAM(bool, isConstant);
- BfIRLinkageType linkageType = (BfIRLinkageType)mStream->Read();
- CMD_PARAM(BeConstant*, initializer);
- CMD_PARAM(String, name);
- CMD_PARAM(bool, isTLS);
-
- auto globalVariable = mBeModule->mGlobalVariables.Alloc();
- globalVariable->mModule = mBeModule;
- globalVariable->mType = varType;
- globalVariable->mIsConstant = isConstant;
- globalVariable->mLinkageType = linkageType;
- globalVariable->mInitializer = initializer;
- globalVariable->mName = name;
- globalVariable->mIsTLS = isTLS;
- globalVariable->mAlign = varType->mAlign;
- globalVariable->mUnnamedAddr = false;
- BF_ASSERT(varType->mAlign > 0);
- SetResult(streamId, globalVariable);
- beValue = globalVariable;
- }
- else
- beValue = GetBeValue(streamId);
- BE_MEM_END("ParamType_Const_GlobalVar");
- return;
- }
- else if ((constType == BfConstType_BitCast) || (constType == BfConstType_BitCastNull))
- {
- CMD_PARAM(BeConstant*, target);
- CMD_PARAM(BeType*, toType);
- auto castedVal = mBeModule->mAlloc.Alloc<BeCastConstant>();
- castedVal->mInt64 = target->mInt64;
- castedVal->mType = toType;
- castedVal->mTarget = target;
- BF_ASSERT(target->GetType() != NULL);
- BF_ASSERT(!target->GetType()->IsComposite());
- beValue = castedVal;
- BE_MEM_END("ParamType_Const_BitCast");
- return;
- }
- else if (constType == BfConstType_GEP32_2)
- {
- CMD_PARAM(BeConstant*, target);
- CMD_PARAM(int, idx0);
- CMD_PARAM(int, idx1);
-
- BF_ASSERT(target->GetType()->IsPointer());
- auto gepConstant = mBeModule->mAlloc.Alloc<BeGEPConstant>();
- gepConstant->mTarget = target;
- gepConstant->mIdx0 = idx0;
- gepConstant->mIdx1 = idx1;
- beValue = gepConstant;
- BE_MEM_END("ParamType_Const_GEP32_2");
- return;
- }
- else if (constType == BfConstType_PtrToInt)
- {
- CMD_PARAM(BeConstant*, target);
- BfTypeCode toTypeCode = (BfTypeCode)mStream->Read();
- BF_ASSERT(target->GetType()->IsPointer());
- bool isSigned = false;
- BeType* toType = GetBeType(toTypeCode, isSigned);
- auto castedVal = mBeModule->mAlloc.Alloc<BeCastConstant>();
- castedVal->mInt64 = target->mInt64;
- castedVal->mType = toType;
- castedVal->mTarget = target;
- BF_ASSERT(target->GetType() != NULL);
- beValue = castedVal;
- BE_MEM_END("ParamType_Const_PtrToInt");
- return;
- }
- else if (constType == BfConstType_AggZero)
- {
- CMD_PARAM(BeType*, type);
- auto beConst = mBeModule->mAlloc.Alloc<BeConstant>();
- beConst->mType = type;
- beValue = beConst;
- BE_MEM_END("ParamType_Const_AggZero");
- return;
- }
- else if (constType == BfConstType_Array)
- {
- CMD_PARAM(BeType*, type);
- CMD_PARAM(CmdParamVec<BeConstant*>, values);
- auto arrayType = (BeSizedArrayType*)type;
- int fillCount = (int)(arrayType->mLength - values.size());
- if (fillCount > 0)
- {
- auto lastValue = values.back();
- for (int i = 0; i < fillCount; i++)
- values.push_back(lastValue);
- }
- auto constStruct = mBeModule->mOwnedValues.Alloc<BeStructConstant>();
- constStruct->mType = type;
- for (auto val : values)
- constStruct->mMemberValues.push_back(BeValueDynCast<BeConstant>(val));
- beValue = constStruct;
- BE_MEM_END("ParamType_Const_Array");
- return;
- }
- bool isSigned = false;
- BeType* llvmConstType = GetBeType(typeCode, isSigned);
- if (typeCode == BfTypeCode_Single)
- {
- float f;
- mStream->Read(&f, sizeof(float));
- beValue = mBeModule->GetConstant(llvmConstType, f);
- BE_MEM_END("ParamType_Single");
- }
- else if (typeCode == BfTypeCode_Double)
- {
- double d;
- mStream->Read(&d, sizeof(double));
- beValue = mBeModule->GetConstant(llvmConstType, d);
- BE_MEM_END("ParamType_Const_Double");
- }
- else if (typeCode == BfTypeCode_Boolean)
- {
- CMD_PARAM(bool, boolVal);
- beValue = mBeModule->GetConstant(llvmConstType, boolVal);
- BE_MEM_END("ParamType_Const_Boolean");
- }
- else if (typeCode == BfTypeCode_None)
- {
- beValue = NULL;
- BE_MEM_END("ParamType_Const_None");
- }
- else if (typeCode == BfTypeCode_NullPtr)
- {
- CMD_PARAM(BeType*, nullType);
- beValue = mBeModule->GetConstantNull((BePointerType*)nullType);
- BE_MEM_END("ParamType_Const_NullPtr");
- }
- else if (BfIRBuilder::IsInt(typeCode))
- {
- int64 intVal = ReadSLEB128();
- auto constVal = mBeModule->GetConstant(llvmConstType, intVal);
- beValue = constVal;
- BE_MEM_END("ParamType_Const_Int");
- }
- else
- {
- BF_FATAL("Unhandled");
- }
- }
- else if (paramType == BfIRParamType_Arg)
- {
- int argIdx = mStream->Read();
- beValue = mBeModule->GetArgument(argIdx);
- BE_MEM_END("ParamType_Arg");
- }
- else if (paramType == BfIRParamType_StreamId_Abs8)
- {
- int cmdId = mStream->Read();
- auto& result = mResults[cmdId];
- BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Value);
- beValue = result.mBeValue;
- BE_MEM_END("ParamType_StreamId");
- }
- else if (paramType == BfIRParamType_StreamId_Rel)
- {
- int cmdId = mCmdCount - (int)ReadSLEB128();
- auto& result = mResults[cmdId];
- BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Value);
- beValue = result.mBeValue;
- BE_MEM_END("ParamType_StreamId");
- }
- else
- {
- int cmdId = mCmdCount - (paramType - BfIRParamType_StreamId_Back1) - 1;
- auto& result = mResults[cmdId];
- BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Value);
- beValue = result.mBeValue;
- BE_MEM_END("ParamType_StreamId");
- }
- }
- void BeIRCodeGen::Read(BeConstant*& llvmConstant)
- {
- BE_MEM_START;
- BeValue* value;
- Read(value);
- if (value == NULL)
- {
- llvmConstant = NULL;
- }
- else
- {
- BF_ASSERT(BeValueDynCast<BeConstant>(value));
- llvmConstant = (BeConstant*)value;
- }
- BE_MEM_END("BeConstant");
- }
- void BeIRCodeGen::Read(BeFunction*& beFunc)
- {
- BE_MEM_START;
- int streamId = (int)ReadSLEB128();
- if (streamId == -1)
- {
- beFunc = NULL;
- return;
- }
- auto& result = mResults[streamId];
- BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Value);
- BF_ASSERT(BeValueDynCast<BeFunction>(result.mBeValue));
- beFunc = (BeFunction*)result.mBeValue;
- BE_MEM_END("BeFunction");
- }
- void BeIRCodeGen::Read(BeBlock*& beBlock)
- {
- BE_MEM_START;
- int streamId = (int)ReadSLEB128();
- auto& result = mResults[streamId];
- BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Block);
- beBlock = (BeBlock*)result.mBeType;
- BE_MEM_END("BeBlock");
- }
- void BeIRCodeGen::Read(BeMDNode*& llvmMD)
- {
- BE_MEM_START;
- int streamId = (int)ReadSLEB128();
- if (streamId == -1)
- {
- llvmMD = NULL;
- return;
- }
- auto& result = mResults[streamId];
- BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Metadata);
- llvmMD = result.mBeMetadata;
- BE_MEM_END("BeMDNode");
- }
- void BeIRCodeGen::HandleNextCmd()
- {
- int curId = mCmdCount;
- BfIRCmd cmd = (BfIRCmd)mStream->Read();
- mCmdCount++;
- #ifdef CODEGEN_TRACK
- gBEMemReporter.BeginSection(gIRCmdNames[cmd]);
- gBEMemReporter.Add(1);
- #endif
- switch (cmd)
- {
- case BfIRCmd_Module_Start:
- {
- CMD_PARAM(String, moduleName);
- CMD_PARAM(int, ptrSize);
- CMD_PARAM(bool, isOptimized);
- BF_ASSERT(mBeModule == NULL);
- mPtrSize = ptrSize;
- mIsOptimized = isOptimized;
- mBeContext = new BeContext();
- mBeModule = new BeModule(moduleName, mBeContext);
- mBeModule->mBeIRCodeGen = this;
- for (auto constInt : mConfigConsts)
- {
- auto constVal = mBeModule->mAlloc.Alloc<BeConstant>();
- constVal->mType = mBeContext->GetPrimitiveType(BeTypeCode_Int32);
- constVal->mInt64 = constInt;
- mBeModule->mConfigConsts32.Add(constVal);
- constVal = mBeModule->mAlloc.Alloc<BeConstant>();
- constVal->mType = mBeContext->GetPrimitiveType(BeTypeCode_Int64);
- constVal->mInt64 = constInt;
- mBeModule->mConfigConsts64.Add(constVal);
- }
- }
- break;
- case BfIRCmd_Module_SetTargetTriple:
- {
- CMD_PARAM(String, targetTriple);
- mBeModule->mTargetTriple = targetTriple;
- }
- break;
- case BfIRCmd_Module_AddModuleFlag:
- {
- CMD_PARAM(String, flag);
- CMD_PARAM(int, val);
- //mBeModule->addModuleFlag(BeModule::Warning, flag, val);
- }
- break;
- case BfIRCmd_WriteIR:
- {
- /*CMD_PARAM(String, fileName);
- std::error_code ec;
- Beraw_fd_ostream outStream(fileName.c_str(), ec, Besys::fs::OpenFlags::F_Text);
- if (ec)
- {
- Fail("Failed writing IR '" + fileName + "': " + ec.message());
- }
- else
- mBeModule->print(outStream, NULL);*/
- }
- break;
- case BfIRCmd_SetType:
- {
- CMD_PARAM(int, typeId);
- CMD_PARAM(BeType*, type);
- GetTypeEntry(typeId).mBeType = type;
- }
- break;
- case BfIRCmd_SetInstType:
- {
- CMD_PARAM(int, typeId);
- CMD_PARAM(BeType*, type);
- GetTypeEntry(typeId).mInstBeType = type;
- }
- break;
- case BfIRCmd_PrimitiveType:
- {
- BfTypeCode typeCode = (BfTypeCode)mStream->Read();
- bool isSigned = false;
- SetResult(curId, GetBeType(typeCode, isSigned));
- }
- break;
- case BfIRCmd_CreateStruct:
- {
- CMD_PARAM(String, typeName);
- SetResult(curId, mBeContext->CreateStruct(typeName));
- }
- break;
- case BfIRCmd_StructSetBody:
- {
- CMD_PARAM(BeType*, type);
- CMD_PARAM(CmdParamVec<BeType*>, members);
- CMD_PARAM(bool, isPacked);
- BF_ASSERT(type->mTypeCode == BeTypeCode_Struct);
- auto structType = (BeStructType*)type;
- mBeContext->SetStructBody(structType, members, isPacked);
- }
- break;
- case BfIRCmd_Type:
- {
- CMD_PARAM(BeIRTypeEntry*, typeEntry);
- auto type = typeEntry->mBeType;
- SetResult(curId, type);
- }
- break;
- case BfIRCmd_TypeInst:
- {
- CMD_PARAM(BeIRTypeEntry*, typeEntry);
- SetResult(curId, typeEntry->mInstBeType);
- }
- break;
- case BfIRCmd_TypeInstPtr:
- {
- CMD_PARAM(BeIRTypeEntry*, typeEntry);
- SetResult(curId, mBeContext->GetPointerTo(typeEntry->mInstBeType));
- }
- break;
- case BfIRCmd_GetType:
- {
- CMD_PARAM(BeValue*, value);
- auto type = value->GetType();
- SetResult(curId, type);
- }
- break;
- case BfIRCmd_GetPointerToFuncType:
- {
- CMD_PARAM(BeFunctionType*, funcType);
- SetResult(curId, mBeContext->GetPointerTo(funcType));
- }
- break;
- case BfIRCmd_GetPointerToType:
- {
- CMD_PARAM(BeType*, type);
- SetResult(curId, mBeContext->GetPointerTo(type));
- }
- break;
- case BfIRCmd_GetSizedArrayType:
- {
- CMD_PARAM(BeType*, elementType);
- CMD_PARAM(int, length);
- SetResult(curId, mBeContext->CreateSizedArrayType(elementType, length));
- }
- break;
- case BfIRCmd_CreateConstStruct:
- {
- CMD_PARAM(BeType*, type);
- CMD_PARAM(CmdParamVec<BeValue*>, values)
- auto constStruct = mBeModule->mOwnedValues.Alloc<BeStructConstant>();
- constStruct->mType = type;
- BF_ASSERT(type->IsStruct());
- BF_ASSERT(((BeStructType*)type)->mMembers.size() == values.size());
- for (int i = 0; i < (int)values.size(); i++)
- {
- auto val = values[i];
- BF_ASSERT(((BeStructType*)type)->mMembers[i].mType == val->GetType());
- constStruct->mMemberValues.push_back(BeValueDynCast<BeConstant>(val));
- }
- SetResult(curId, constStruct);
- }
- break;
- case BfIRCmd_CreateConstStructZero:
- {
- CMD_PARAM(BeType*, type);
- auto beConst = mBeModule->mAlloc.Alloc<BeConstant>();
- beConst->mType = type;
- SetResult(curId, beConst);
- }
- break;
- case BfIRCmd_CreateConstArray:
- {
- CMD_PARAM(BeType*, type);
- CMD_PARAM(CmdParamVec<BeConstant*>, values);
- auto constStruct = mBeModule->mOwnedValues.Alloc<BeStructConstant>();
- constStruct->mType = type;
- for (auto val : values)
- constStruct->mMemberValues.push_back(BeValueDynCast<BeConstant>(val));
- SetResult(curId, constStruct);
- }
- break;
- case BfIRCmd_CreateConstString:
- {
- CMD_PARAM(String, str);
- auto constStruct = mBeModule->mOwnedValues.Alloc<BeStringConstant>();
- constStruct->mString = str;
- auto charType = mBeContext->GetPrimitiveType(BeTypeCode_Int8);
- constStruct->mType = mBeContext->CreateSizedArrayType(charType, str.length() + 1);
- SetResult(curId, constStruct);
- }
- break;
- case BfIRCmd_ConfigConst:
- {
- CMD_PARAM(int, constIdx);
- BfTypeCode typeCode = (BfTypeCode)mStream->Read();
- if (typeCode == BfTypeCode_IntPtr)
- typeCode = (mPtrSize == 4) ? BfTypeCode_Int32 : BfTypeCode_Int64;
- BeConstant* constVal = (typeCode == BfTypeCode_Int32) ?
- mBeModule->mConfigConsts32[constIdx] :
- mBeModule->mConfigConsts64[constIdx];
- SetResult(curId, constVal);
- }
- break;
- case BfIRCmd_SetName:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(String, name);
- val->SetName(name);
- }
- break;
- case BfIRCmd_CreateUndefValue:
- {
- CMD_PARAM(BeType*, type);
- SetResult(curId, mBeModule->CreateUndefValue(type));
- }
- break;
- case BfIRCmd_NumericCast:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(bool, valIsSigned);
- BfTypeCode typeCode = (BfTypeCode)mStream->Read();
- BfTypeCode valTypeCode = GetTypeCode(val->GetType(), valIsSigned);
- bool toSigned = false;
- auto toBeType = GetBeType(typeCode, toSigned);
- BeValue* retVal = mBeModule->CreateNumericCast(val, toBeType, valIsSigned, toSigned);
- SetResult(curId, retVal);
- }
- break;
- case BfIRCmd_CmpEQ:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateCmp(BeCmpKind_EQ, lhs, rhs));
- }
- break;
- case BfIRCmd_CmpNE:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateCmp(BeCmpKind_NE, lhs, rhs));
- }
- break;
- case BfIRCmd_CmpSLT:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateCmp(BeCmpKind_SLT, lhs, rhs));
- }
- break;
- case BfIRCmd_CmpULT:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateCmp(BeCmpKind_ULT, lhs, rhs));
- }
- break;
- case BfIRCmd_CmpSLE:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateCmp(BeCmpKind_SLE, lhs, rhs));
- }
- break;
- case BfIRCmd_CmpULE:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateCmp(BeCmpKind_ULE, lhs, rhs));
- }
- break;
- case BfIRCmd_CmpSGT:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateCmp(BeCmpKind_SGT, lhs, rhs));
- }
- break;
- case BfIRCmd_CmpUGT:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateCmp(BeCmpKind_UGT, lhs, rhs));
- }
- break;
- case BfIRCmd_CmpSGE:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateCmp(BeCmpKind_SGE, lhs, rhs));
- }
- break;
- case BfIRCmd_CmpUGE:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateCmp(BeCmpKind_UGE, lhs, rhs));
- }
- break;
- case BfIRCmd_Add:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_Add, lhs, rhs));
- }
- break;
- case BfIRCmd_Sub:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_Subtract, lhs, rhs));
- }
- break;
- case BfIRCmd_Mul:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_Multiply, lhs, rhs));
- }
- break;
- case BfIRCmd_SDiv:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_SDivide, lhs, rhs));
- }
- break;
- case BfIRCmd_UDiv:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_UDivide, lhs, rhs));
- }
- break;
- case BfIRCmd_SRem:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_SModulus, lhs, rhs));
- }
- break;
- case BfIRCmd_URem:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_UModulus, lhs, rhs));
- }
- break;
- case BfIRCmd_And:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_BitwiseAnd, lhs, rhs));
- }
- break;
- case BfIRCmd_Or:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_BitwiseOr, lhs, rhs));
- }
- break;
- case BfIRCmd_Xor:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_ExclusiveOr, lhs, rhs));
- }
- break;
- case BfIRCmd_Shl:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_LeftShift, lhs, rhs));
- }
- break;
- case BfIRCmd_AShr:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_ARightShift, lhs, rhs));
- }
- break;
- case BfIRCmd_LShr:
- {
- CMD_PARAM(BeValue*, lhs);
- CMD_PARAM(BeValue*, rhs);
- SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_RightShift, lhs, rhs));
- }
- break;
- case BfIRCmd_Neg:
- {
- CMD_PARAM(BeValue*, val);
- auto negInst = mBeModule->AllocInst<BeNegInst>();
- negInst->mValue = val;
- SetResult(curId, negInst);
- }
- break;
- case BfIRCmd_Not:
- {
- CMD_PARAM(BeValue*, val);
- auto negInst = mBeModule->AllocInst<BeNotInst>();
- negInst->mValue = val;
- SetResult(curId, negInst);
- }
- break;
- case BfIRCmd_BitCast:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(BeType*, toType);
- if (auto funcVal = BeValueDynCast<BeFunction>(val))
- {
- auto beConst = mBeModule->mAlloc.Alloc<BeConstant>();
- beConst->mTarget = funcVal;
- BF_ASSERT(funcVal->mType != NULL);
- beConst->mType = toType;
- SetResult(curId, beConst);
- break;
- }
- SetResult(curId, mBeModule->CreateBitCast(val, toType));
- }
- break;
- case BfIRCmd_PtrToInt:
- {
- CMD_PARAM(BeValue*, val);
- auto typeCode = (BfTypeCode)mStream->Read();
- bool isSigned = false;
- auto beType = GetBeType(typeCode, isSigned);
- BF_ASSERT(beType != NULL);
- auto numericCastInst = mBeModule->AllocInst<BeNumericCastInst>();
- numericCastInst->mValue = val;
- numericCastInst->mValSigned = false;
- numericCastInst->mToType = beType;
- numericCastInst->mToSigned = isSigned;
- SetResult(curId, numericCastInst);
- }
- break;
- case BfIRCmd_IntToPtr:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(BeType*, toType);
-
- auto bitcastInst = mBeModule->AllocInst<BeBitCastInst>();
- bitcastInst->mValue = val;
- bitcastInst->mToType = toType;
- SetResult(curId, bitcastInst);
- }
- break;
- case BfIRCmd_InboundsGEP1_32:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(int, idx0);
- BF_ASSERT(val->GetType()->IsPointer());
- BeType* int32Type = mBeContext->GetPrimitiveType(BeTypeCode_Int32);
- SetResult(curId, mBeModule->CreateGEP(val, mBeModule->GetConstant(int32Type, (int64)idx0), NULL));
- }
- break;
- case BfIRCmd_InboundsGEP2_32:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(int, idx0);
- CMD_PARAM(int, idx1);
- BF_ASSERT(val->GetType()->IsPointer());
- BeType* int32Type = mBeContext->GetPrimitiveType(BeTypeCode_Int32);
- SetResult(curId, mBeModule->CreateGEP(val, mBeModule->GetConstant(int32Type, (int64)idx0), mBeModule->GetConstant(int32Type, (int64)idx1)));
- }
- break;
- case BfIRCmd_InBoundsGEP1:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(BeValue*, idx0);
- BF_ASSERT(val->GetType()->IsPointer());
- SetResult(curId, mBeModule->CreateGEP(val, idx0, NULL));
- }
- break;
- case BfIRCmd_InBoundsGEP2:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(BeValue*, idx0);
- CMD_PARAM(BeValue*, idx1);
- BF_ASSERT(val->GetType()->IsPointer());
- SetResult(curId, mBeModule->CreateGEP(val, idx0, idx1));
- }
- break;
- case BfIRCmd_IsNull:
- {
- CMD_PARAM(BeValue*, val);
- BF_ASSERT(val->GetType()->IsPointer());
- SetResult(curId, mBeModule->CreateCmp(BeCmpKind_EQ, val, mBeModule->GetConstantNull((BePointerType*)val->GetType())));
- }
- break;
- case BfIRCmd_IsNotNull:
- {
- CMD_PARAM(BeValue*, val);
- BF_ASSERT(val->GetType()->IsPointer());
- SetResult(curId, mBeModule->CreateCmp(BeCmpKind_NE, val, mBeModule->GetConstantNull((BePointerType*)val->GetType())));
- }
- break;
- case BfIRCmd_ExtractValue:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(int, idx);
- auto extractValueInst = mBeModule->AllocInst<BeExtractValueInst>();
- extractValueInst->mAggVal = val;
- extractValueInst->mIdx = idx;
- BF_ASSERT(val->GetType()->IsComposite());
- SetResult(curId, extractValueInst);
- }
- break;
- case BfIRCmd_InsertValue:
- {
- CMD_PARAM(BeValue*, agg);
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(int, idx);
- auto insertValueInst = mBeModule->AllocInst<BeInsertValueInst>();
- insertValueInst->mAggVal = agg;
- insertValueInst->mMemberVal = val;
- insertValueInst->mIdx = idx;
- SetResult(curId, insertValueInst);
- }
- break;
- case BfIRCmd_Alloca:
- {
- CMD_PARAM(BeType*, type);
- if (type->IsStruct())
- {
- BF_ASSERT(!((BeStructType*)type)->mIsOpaque);
- }
- SetResult(curId, mBeModule->CreateAlloca(type));
- }
- break;
- case BfIRCmd_AllocaArray:
- {
- CMD_PARAM(BeType*, type);
- CMD_PARAM(BeValue*, arraySize);
- auto allocaInst = mBeModule->AllocInst<BeAllocaInst>();
- allocaInst->mType = type;
- allocaInst->mArraySize = arraySize;
- SetResult(curId, allocaInst);
- }
- break;
- case BfIRCmd_SetAllocaAlignment:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(int, alignment);
- auto inst = BeValueDynCast<BeAllocaInst>(val);
- //TODO: Implement
- /*inst->setAlignment(alignment);*/
- }
- break;
- case BfIRCmd_AliasValue:
- {
- CMD_PARAM(BeValue*, val);
- auto inst = mBeModule->AllocInst<BeAliasValueInst>();
- inst->mPtr = val;
- SetResult(curId, inst);
- }
- break;
- case BfIRCmd_LifetimeStart:
- {
- CMD_PARAM(BeValue*, val);
- auto inst = mBeModule->AllocInst<BeLifetimeStartInst>();
- inst->mPtr = val;
- SetResult(curId, inst);
- }
- break;
- case BfIRCmd_LifetimeEnd:
- {
- CMD_PARAM(BeValue*, val);
- auto inst = mBeModule->AllocInst<BeLifetimeEndInst>();
- inst->mPtr = val;
- SetResult(curId, inst);
- }
- break;
- case BfIRCmd_LifetimeExtend:
- {
- CMD_PARAM(BeValue*, val);
- auto inst = mBeModule->AllocInst<BeLifetimeExtendInst>();
- inst->mPtr = val;
- SetResult(curId, inst);
- }
- break;
- case BfIRCmd_ValueScopeStart:
- {
- auto inst = mBeModule->AllocInst<BeValueScopeStartInst>();
- SetResult(curId, inst);
- }
- break;
- case BfIRCmd_ValueScopeRetain:
- {
- CMD_PARAM(BeValue*, val);
- auto inst = mBeModule->AllocInst<BeValueScopeRetainInst>();
- inst->mValue = val;
- }
- break;
- case BfIRCmd_ValueScopeSoftEnd:
- {
- CMD_PARAM(BeValue*, val);
- auto inst = mBeModule->AllocInst<BeValueScopeEndInst>();
- inst->mScopeStart = (BeValueScopeStartInst*)val;
- inst->mIsSoft = true;
- //TODO: Is this always correct? This keeps there from being nops inserted on block opens ( { )
- inst->mDbgLoc = NULL;
- }
- break;
- case BfIRCmd_ValueScopeHardEnd:
- {
- CMD_PARAM(BeValue*, val);
- auto inst = mBeModule->AllocInst<BeValueScopeEndInst>();
- inst->mScopeStart = (BeValueScopeStartInst*)val;
- inst->mIsSoft = false;
- //TODO: Is this always correct? This keeps there from being nops inserted on block opens ( { )
- inst->mDbgLoc = NULL;
- }
- break;
- case BfIRCmd_SetAllocaNoChkStkHint:
- {
- CMD_PARAM(BeValue*, val);
- auto inst = BeValueDynCast<BeAllocaInst>(val);
- inst->mNoChkStk = true;
- }
- break;
- case BfIRCmd_SetAllocaForceMem:
- {
- CMD_PARAM(BeValue*, val);
- auto inst = BeValueDynCast<BeAllocaInst>(val);
- inst->mForceMem = true;
- }
- break;
- case BfIRCmd_Load:
- {
- CMD_PARAM(BeValue*, val);
- #ifdef _DEBUG
- auto ptrType = val->GetType();
- BF_ASSERT(ptrType->IsPointer());
- // We call via a function pointer so there's never a reason to allow loading of a funcPtr
- BF_ASSERT(((BePointerType*)ptrType)->mElementType->mTypeCode != BeTypeCode_Function);
- #endif
- CMD_PARAM(bool, isVolatile);
- SetResult(curId, mBeModule->CreateLoad(val, isVolatile));
- }
- break;
- case BfIRCmd_AlignedLoad:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(int, alignment);
- CMD_PARAM(bool, isVolatile);
- #ifdef _DEBUG
- auto ptrType = val->GetType();
- BF_ASSERT(ptrType->IsPointer());
- #endif
- SetResult(curId, mBeModule->CreateAlignedLoad(val, alignment, isVolatile));
- }
- break;
- case BfIRCmd_Store:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(BeValue*, ptr);
- #ifdef _DEBUG
- auto ptrType = ptr->GetType();
- auto valType = val->GetType();
- BF_ASSERT(ptrType->IsPointer());
- BF_ASSERT(mBeContext->AreTypesEqual(((BePointerType*)ptrType)->mElementType, valType));
- #endif
- CMD_PARAM(bool, isVolatile);
- SetResult(curId, mBeModule->CreateStore(val, ptr, isVolatile));
- }
- break;
- case BfIRCmd_AlignedStore:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(BeValue*, ptr);
- CMD_PARAM(int, alignment);
- CMD_PARAM(bool, isVolatile);
- #ifdef _DEBUG
- auto ptrType = ptr->GetType();
- auto valType = val->GetType();
- BF_ASSERT(ptrType->IsPointer());
- BF_ASSERT(mBeContext->AreTypesEqual(((BePointerType*)ptrType)->mElementType, valType));
- #endif
- SetResult(curId, mBeModule->CreateAlignedStore(val, ptr, alignment, isVolatile));
- }
- break;
- case BfIRCmd_MemSet:
- {
- auto inst = mBeModule->AllocInst<BeMemSetInst>();
- Read(inst->mAddr);
- Read(inst->mVal);
- Read(inst->mSize);
- Read(inst->mAlignment);
- SetResult(curId, inst);
- }
- break;
- case BfIRCmd_Fence:
- {
- BfIRFenceType fenceType = (BfIRFenceType)mStream->Read();
- mBeModule->AllocInst<BeFenceInst>();
- }
- break;
- case BfIRCmd_StackSave:
- {
- SetResult(curId, mBeModule->AllocInst<BeStackSaveInst>());
- }
- break;
- case BfIRCmd_StackRestore:
- {
- CMD_PARAM(BeValue*, stackVal);
- auto stackRestoreInst = mBeModule->AllocInst<BeStackRestoreInst>();
- stackRestoreInst->mStackVal = stackVal;
- SetResult(curId, stackRestoreInst);
- }
- break;
- case BfIRCmd_GlobalVariable:
- {
- CMD_PARAM(BeType*, varType);
- CMD_PARAM(bool, isConstant);
- BfIRLinkageType linkageType = (BfIRLinkageType)mStream->Read();
- CMD_PARAM(BeConstant*, initializer);
- CMD_PARAM(String, name);
- CMD_PARAM(bool, isTLS);
-
- auto globalVariable = mBeModule->mGlobalVariables.Alloc();
- globalVariable->mModule = mBeModule;
- globalVariable->mType = varType;
- globalVariable->mIsConstant = isConstant;
- globalVariable->mLinkageType = linkageType;
- globalVariable->mInitializer = initializer;
- globalVariable->mName = name;
- globalVariable->mIsTLS = isTLS;
- globalVariable->mUnnamedAddr = false;
- if (initializer != NULL)
- {
- globalVariable->mAlign = varType->mAlign;
- BF_ASSERT(varType->mAlign > 0);
- }
- else
- globalVariable->mAlign = -1;
-
- SetResult(curId, globalVariable);
- }
- break;
- case BfIRCmd_GlobalVar_SetUnnamedAddr:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(bool, unnamedAddr);
- ((BeGlobalVariable*)val)->mUnnamedAddr = true;
- }
- break;
- case BfIRCmd_GlobalVar_SetInitializer:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(BeConstant*, initializer);
- auto globalVariable = (BeGlobalVariable*)val;
- globalVariable->mInitializer = initializer;
- if (globalVariable->mInitializer != NULL)
- {
- globalVariable->mAlign = globalVariable->mType->mAlign;
- BF_ASSERT(globalVariable->mAlign != -1);
- }
- }
- break;
- case BfIRCmd_GlobalVar_SetAlignment:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(int, alignment);
- auto globalVariable = (BeGlobalVariable*)val;
- globalVariable->mAlign = alignment;
- BF_ASSERT(alignment > 0);
- if (globalVariable->mInitializer != NULL)
- {
- BF_ASSERT(globalVariable->mAlign != -1);
- }
- }
- break;
- case BfIRCmd_GlobalStringPtr:
- {
- CMD_PARAM(String, str);
- auto constStruct = mBeModule->mOwnedValues.Alloc<BeStringConstant>();
- constStruct->mString = str;
- auto charType = mBeContext->GetPrimitiveType(BeTypeCode_Int8);
- constStruct->mType = mBeContext->CreateSizedArrayType(charType, str.length() + 1);
- auto globalVariable = mBeModule->mGlobalVariables.Alloc();
- globalVariable->mModule = mBeModule;
- globalVariable->mType = constStruct->mType;
- globalVariable->mIsConstant = true;
- globalVariable->mLinkageType = BfIRLinkageType_Internal;
- globalVariable->mInitializer = constStruct;
- globalVariable->mName = StrFormat("__str%d", (int)mBeModule->mGlobalVariables.size() - 1);
- globalVariable->mIsTLS = false;
- globalVariable->mAlign = 1;
- globalVariable->mUnnamedAddr = false;
- auto castedVal = mBeModule->mAlloc.Alloc<BeCastConstant>();
- castedVal->mType = mBeContext->GetPointerTo(charType);
- castedVal->mTarget = globalVariable;
- SetResult(curId, castedVal);
-
- //SetResult(curId, globalVariable);
- }
- break;
- case BfIRCmd_CreateBlock:
- {
- CMD_PARAM(String, name);
- CMD_PARAM(bool, addNow);
- auto block = mBeModule->CreateBlock(name);
- if (addNow)
- mBeModule->AddBlock(mActiveFunction, block);
- SetResult(curId, block);
- }
- break;
- case BfIRCmd_MaybeChainNewBlock:
- {
- CMD_PARAM(String, name);
- auto newBlock = mBeModule->GetInsertBlock();
- if (!newBlock->IsEmpty())
- {
- auto bb = mBeModule->CreateBlock(name);
- mBeModule->CreateBr(bb);
- mBeModule->AddBlock(mActiveFunction, bb);
- mBeModule->SetInsertPoint(bb);
- newBlock = bb;
- }
- SetResult(curId, newBlock);
- }
- break;
- case BfIRCmd_AddBlock:
- {
- CMD_PARAM(BeBlock*, block);
- mBeModule->AddBlock(mActiveFunction, block);
- }
- break;
- case BfIRCmd_DropBlocks:
- {
- CMD_PARAM(BeBlock*, startingBlock);
- auto& basicBlockList = mActiveFunction->mBlocks;
- int postExitBlockIdx = -1;
- /*auto itr = basicBlockList.begin();
- while (itr != basicBlockList.end())
- {
- auto block = *itr;
- if (block == startingBlock)
- {
- basicBlockList.erase(itr, basicBlockList.end());
- break;
- }
- ++itr;
- }*/
- for (int i = 0; i < (int)basicBlockList.size(); i++)
- {
- if (basicBlockList[i] == startingBlock)
- {
- basicBlockList.RemoveRange(i, basicBlockList.size() - i);
- break;
- }
- }
- }
- break;
- case BfIRCmd_MergeBlockDown:
- {
- CMD_PARAM(BeBlock*, fromBlock);
- CMD_PARAM(BeBlock*, intoBlock);
- for (auto inst : fromBlock->mInstructions)
- inst->mParentBlock = intoBlock;
- if (!fromBlock->mInstructions.IsEmpty())
- intoBlock->mInstructions.Insert(0, &fromBlock->mInstructions[0], fromBlock->mInstructions.size());
- mBeModule->RemoveBlock(mActiveFunction, fromBlock);
- }
- break;
- case BfIRCmd_SetInsertPoint:
- {
- CMD_PARAM(BeBlock*, block);
- mBeModule->SetInsertPoint(block);
- }
- break;
- case BfIRCmd_SetInsertPointAtStart:
- {
- CMD_PARAM(BeBlock*, block);
- mBeModule->SetInsertPointAtStart(block);
- }
- break;
- case BfIRCmd_EraseFromParent:
- {
- CMD_PARAM(BeBlock*, block);
- mBeModule->RemoveBlock(mActiveFunction, block);
- }
- break;
- case BfIRCmd_DeleteBlock:
- {
- CMD_PARAM(BeBlock*, block);
- }
- break;
- case BfIRCmd_EraseInstFromParent:
- {
- CMD_PARAM(BeValue*, instVal);
- BeInst* inst = (BeInst*)instVal;
- auto itr = std::find(inst->mParentBlock->mInstructions.begin(), inst->mParentBlock->mInstructions.end(), inst);
- inst->mParentBlock->mInstructions.erase(itr);
- }
- break;
- case BfIRCmd_CreateBr:
- {
- CMD_PARAM(BeBlock*, block);
- mBeModule->CreateBr(block);
- }
- break;
- case BfIRCmd_CreateBr_Fake:
- {
- CMD_PARAM(BeBlock*, block);
- auto inst = mBeModule->CreateBr(block);
- inst->mIsFake = true;
- }
- break;
- case BfIRCmd_CreateBr_NoCollapse:
- {
- CMD_PARAM(BeBlock*, block);
- auto inst = mBeModule->CreateBr(block);
- inst->mNoCollapse = true;
- }
- break;
- case BfIRCmd_CreateCondBr:
- {
- CMD_PARAM(BeValue*, condVal);
- CMD_PARAM(BeBlock*, trueBlock);
- CMD_PARAM(BeBlock*, falseBlock);
- mBeModule->CreateCondBr(condVal, trueBlock, falseBlock);
- }
- break;
- case BfIRCmd_MoveBlockToEnd:
- {
- CMD_PARAM(BeBlock*, block);
- mBeModule->RemoveBlock(mActiveFunction, block);
- mBeModule->AddBlock(mActiveFunction, block);
- }
- break;
- case BfIRCmd_CreateSwitch:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(BeBlock*, dest);
- CMD_PARAM(int, numCases);
- auto switchInst = mBeModule->AllocInstOwned<BeSwitchInst>();
- switchInst->mValue = val;
- switchInst->mDefaultBlock = dest;
- switchInst->mCases.Reserve(numCases);
- SetResult(curId, switchInst);
- }
- break;
- case BfIRCmd_AddSwitchCase:
- {
- CMD_PARAM(BeValue*, switchVal);
- CMD_PARAM(BeValue*, caseVal);
- CMD_PARAM(BeBlock*, caseBlock);
- BeSwitchCase switchCase;
- switchCase.mValue = (BeConstant*)caseVal;
- switchCase.mBlock = caseBlock;
- ((BeSwitchInst*)switchVal)->mCases.push_back(switchCase);
- }
- break;
- case BfIRCmd_SetSwitchDefaultDest:
- {
- CMD_PARAM(BeValue*, switchVal);
- CMD_PARAM(BeBlock*, caseBlock);
- ((BeSwitchInst*)switchVal)->mDefaultBlock = caseBlock;
- }
- break;
- case BfIRCmd_CreatePhi:
- {
- CMD_PARAM(BeType*, type);
- CMD_PARAM(int, incomingCount);
- auto phiInst = mBeModule->AllocInstOwned<BePhiInst>();
- phiInst->mType = type;
- SetResult(curId, phiInst);
- }
- break;
- case BfIRCmd_AddPhiIncoming:
- {
- CMD_PARAM(BeValue*, phiValue);
- CMD_PARAM(BeValue*, value);
- CMD_PARAM(BeBlock*, comingFrom);
- auto phiIncoming = mBeModule->mAlloc.Alloc<BePhiIncoming>();
- phiIncoming->mBlock = comingFrom;
- phiIncoming->mValue = value;
- ((BePhiInst*)phiValue)->mIncoming.push_back(phiIncoming);
- }
- break;
- case BfIRCmd_GetIntrinsic:
- {
- CMD_PARAM(int, intrinId);
- CMD_PARAM(CmdParamVec<BeType*>, paramTypes);
- auto intrin = mBeModule->mAlloc.Alloc<BeIntrinsic>();
- intrin->mKind = (BfIRIntrinsic)intrinId;
- switch (intrin->mKind)
- {
- case BfIRIntrinsic_AtomicAdd:
- case BfIRIntrinsic_AtomicAnd:
- case BfIRIntrinsic_AtomicCmpXChg:
- case BfIRIntrinsic_AtomicLoad:
- case BfIRIntrinsic_AtomicMax:
- case BfIRIntrinsic_AtomicMin:
- case BfIRIntrinsic_AtomicNAnd:
- case BfIRIntrinsic_AtomicOr:
- case BfIRIntrinsic_AtomicSub:
- case BfIRIntrinsic_AtomicUMax:
- case BfIRIntrinsic_AtomicUMin:
- case BfIRIntrinsic_AtomicXChg:
- case BfIRIntrinsic_AtomicXor:
- if (!paramTypes.IsEmpty())
- {
- BF_ASSERT(paramTypes[0]->IsPointer());
- if (paramTypes[0]->IsPointer())
- intrin->mReturnType = ((BePointerType*)paramTypes[0])->mElementType;
- }
- else
- intrin->mReturnType = mBeContext->GetPrimitiveType(BeTypeCode_None);
- break;
- case BfIRIntrinsic_AtomicCmpStore:
- case BfIRIntrinsic_AtomicCmpStore_Weak:
- intrin->mReturnType = mBeContext->GetPrimitiveType(BeTypeCode_Boolean);
- break;
- }
- SetResult(curId, intrin);
- }
- break;
- case BfIRCmd_CreateFunctionType:
- {
- CMD_PARAM(BeType*, resultType);
- CMD_PARAM(CmdParamVec<BeType*>, paramTypes);
- CMD_PARAM(bool, isVarArg);
- auto functionType = mBeContext->CreateFunctionType(resultType, paramTypes, isVarArg);
- SetResult(curId, functionType);
- }
- break;
- case BfIRCmd_CreateFunction:
- {
- CMD_PARAM(BeFunctionType*, type);
- BfIRLinkageType linkageType = (BfIRLinkageType)mStream->Read();
- CMD_PARAM(String, name);
- SetResult(curId, mBeModule->CreateFunction(type, linkageType, name));
- }
- break;
- case BfIRCmd_EnsureFunctionPatchable:
- {
- }
- break;
- case BfIRCmd_RemapBindFunction:
- {
- CMD_PARAM(BeValue*, func);
- // We need to store this value to a data segment so we get a symbol we can remap during hot swap
- // We actually do this to ensure that we don't bind to the NEW method but rather the old one- so
- // delegate equality checks still work
- BeFunction* beFunc = BeValueDynCast<BeFunction>(func);
- if (beFunc != NULL)
- {
- if (beFunc->mRemapBindVar == NULL)
- {
- auto globalVariable = mBeModule->mGlobalVariables.Alloc();
- globalVariable->mModule = mBeModule;
- globalVariable->mType = beFunc->mType;
- globalVariable->mIsConstant = true;
- globalVariable->mLinkageType = BfIRLinkageType_External;
- globalVariable->mInitializer = beFunc;
- globalVariable->mName = StrFormat("bf_hs_preserve@%s_%s", beFunc->mName.c_str(), mBeModule->mModuleName.c_str());
- globalVariable->mIsTLS = false;
- globalVariable->mAlign = 8;
- globalVariable->mUnnamedAddr = false;
- beFunc->mRemapBindVar = globalVariable;
- /*if (mBeModule->mDbgModule != NULL)
- {
- auto dbgGlobalVariable = mBeModule->mDbgModule->mGlobalVariables.Alloc();
- dbgGlobalVariable->mContext = mBeContext;
- dbgGlobalVariable->mName = name;
- dbgGlobalVariable->mLinkageName = globalVariable->mName;
- dbgGlobalVariable->mFile = (BeDbgFile*)file;
- dbgGlobalVariable->mLineNum = lineNum;
- dbgGlobalVariable->mType = (BeDbgType*)type;
- dbgGlobalVariable->mIsLocalToUnit = isLocalToUnit;
- dbgGlobalVariable->mValue = val;
- dbgGlobalVariable->mDecl = decl;
- }*/
- }
-
- SetResult(curId, mBeModule->CreateLoad(beFunc->mRemapBindVar, false));
- }
- else
- SetResult(curId, func);
- }
- break;
- case BfIRCmd_SetActiveFunction:
- {
- CMD_PARAM(BeFunction*, func);
- mActiveFunction = func;
- mBeModule->mActiveFunction = func;
- }
- break;
- case BfIRCmd_CreateCall:
- {
- CMD_PARAM(BeValue*, func);
- CMD_PARAM(CmdParamVec<BeValue*>, args);
-
- #ifdef _DEBUG
- auto funcPtrType = func->GetType();
- if (funcPtrType != NULL)
- {
- BF_ASSERT(funcPtrType->IsPointer());
- auto funcType = (BeFunctionType*)((BePointerType*)funcPtrType)->mElementType;
- BF_ASSERT(funcType->mTypeCode == BeTypeCode_Function);
- if (!funcType->mIsVarArg)
- {
- BF_ASSERT(funcType->mParams.size() == args.size());
- int argIdx = 0;
- for (int argIdx = 0; argIdx < (int)args.size(); argIdx++)
- {
- BF_ASSERT(funcType->mParams[argIdx].mType == args[argIdx]->GetType());
- }
- }
- }
- else
- {
- BF_ASSERT(func->GetTypeId() == BeIntrinsic::TypeId);
- }
- #endif
- SetResult(curId, mBeModule->CreateCall(func, args));
- }
- break;
- case BfIRCmd_SetCallCallingConv:
- {
- CMD_PARAM(BeValue*, callInst);
- BfIRCallingConv callingConv = (BfIRCallingConv)mStream->Read();
- ((BeCallInst*)callInst)->mCallingConv = callingConv;
- }
- break;
- case BfIRCmd_SetFuncCallingConv:
- {
- CMD_PARAM(BeFunction*, func);
- BfIRCallingConv callingConv = (BfIRCallingConv)mStream->Read();
- func->mCallingConv = callingConv;
- }
- break;
- case BfIRCmd_SetTailCall:
- {
- CMD_PARAM(BeValue*, callInstVal);
- BeCallInst* callInst = (BeCallInst*)callInstVal;
- callInst->mTailCall = true;
- }
- break;
- case BfIRCmd_SetCallAttribute:
- {
- CMD_PARAM(BeValue*, callInstVal);
- CMD_PARAM(int, paramIdx);
- BfIRAttribute attribute = (BfIRAttribute)mStream->Read();
- BeCallInst* callInst = (BeCallInst*)callInstVal;
- if (attribute == BfIRAttribute_NoReturn)
- callInst->mNoReturn = true;
- }
- break;
- case BfIRCmd_CreateRet:
- {
- CMD_PARAM(BeValue*, val);
- SetResult(curId, mBeModule->CreateRet(val));
- }
- break;
- case BfIRCmd_CreateRetVoid:
- {
- mBeModule->CreateRetVoid();
- }
- break;
- case BfIRCmd_CreateUnreachable:
- {
- mBeModule->AllocInst<BeUnreachableInst>();
- }
- break;
- case BfIRCmd_Call_AddAttribute:
- {
- CMD_PARAM(BeValue*, callInstVal);
- CMD_PARAM(int, argIdx);
- BfIRAttribute attribute = (BfIRAttribute)mStream->Read();
- BeCallInst* callInst = (BeCallInst*)callInstVal;
- if (argIdx > 0)
- {
- if (attribute == BfIRAttribute_StructRet)
- {
- BF_ASSERT(argIdx == 1);
- callInst->mArgs[argIdx - 1].mStructRet = true;
- }
- else if (attribute == BfIRAttribute_ZExt)
- callInst->mArgs[argIdx - 1].mZExt = true;
- else if (attribute == BfIRAttribute_NoAlias)
- callInst->mArgs[argIdx - 1].mNoAlias = true;
- else if (attribute == BfIRAttribute_NoCapture)
- callInst->mArgs[argIdx - 1].mNoCapture = true;
- else
- BF_FATAL("Unhandled");
- }
- else
- {
- if (attribute == BfIRAttribute_NoReturn)
- callInst->mNoReturn = true;
- else
- BF_FATAL("Unhandled");
- }
- }
- break;
- case BfIRCmd_Call_AddAttribute1:
- {
- CMD_PARAM(BeValue*, inst);
- CMD_PARAM(int, argIdx);
- BfIRAttribute attribute = (BfIRAttribute)mStream->Read();
- CMD_PARAM(int, arg);
-
- BeCallInst* callInst = BeValueDynCast<BeCallInst>(inst);
- if (callInst != NULL)
- {
- if (argIdx > 0)
- {
- if (attribute == BfIRAttribute_Dereferencable)
- callInst->mArgs[argIdx - 1].mDereferenceableSize = arg;
- else
- BF_FATAL("Unhandled");
- }
- else
- {
- BF_FATAL("Unhandled");
- }
- }
- }
- break;
- case BfIRCmd_Func_AddAttribute:
- {
- CMD_PARAM(BeFunction*, func);
- CMD_PARAM(int, argIdx);
- BfIRAttribute attribute = (BfIRAttribute)mStream->Read();
- if (argIdx > 0)
- {
- if (attribute == BfIRAttribute_StructRet)
- func->mParams[argIdx - 1].mStructRet = true;
- else if (attribute == BfIRAttribute_NoAlias)
- func->mParams[argIdx - 1].mNoAlias = true;
- else if (attribute == BfIRAttribute_NoCapture)
- func->mParams[argIdx - 1].mNoCapture = true;
- else if (attribute == BfIRAttribute_ZExt)
- func->mParams[argIdx - 1].mZExt = true;
- else
- BF_FATAL("Unhandled");
- }
- else
- {
- if (attribute == BFIRAttribute_AlwaysInline)
- func->mAlwaysInline = true;
- else if (attribute == BFIRAttribute_NoUnwind)
- func->mNoUnwind = true;
- else if (attribute == BFIRAttribute_UWTable)
- func->mUWTable = true;
- else if (attribute == BfIRAttribute_NoReturn)
- func->mNoReturn = true;
- else if (attribute == BFIRAttribute_NoFramePointerElim)
- func->mNoFramePointerElim = true;
- else if (attribute == BFIRAttribute_DllExport)
- func->mIsDLLExport = true;
- else if (attribute == BFIRAttribute_DllImport)
- func->mIsDLLImport = true;
- else
- BF_FATAL("Unhandled");
- }
- }
- break;
- case BfIRCmd_Func_AddAttribute1:
- {
- CMD_PARAM(BeFunction*, func);
- CMD_PARAM(int, argIdx);
- BfIRAttribute attribute = (BfIRAttribute)mStream->Read();
- CMD_PARAM(int, arg);
- // This is for adding things like Dereferencable, which we don't use
- if (argIdx > 0)
- {
- if (attribute == BfIRAttribute_Dereferencable)
- func->mParams[argIdx - 1].mDereferenceableSize = arg;
- else
- BF_FATAL("Unhandled");
- }
- else
- BF_FATAL("Unhandled");
- }
- break;
- case BfIRCmd_Func_SetParamName:
- {
- CMD_PARAM(BeFunction*, func);
- CMD_PARAM(int, argIdx);
- CMD_PARAM(String, name);
- if (argIdx > 0)
- func->mParams[argIdx - 1].mName = name;
- }
- break;
- case BfIRCmd_Func_DeleteBody:
- {
- CMD_PARAM(BeFunction*, func);
- func->mBlocks.Clear();
- }
- break;
- case BfIRCmd_Func_SetLinkage:
- {
- CMD_PARAM(BeFunction*, func);
- BfIRLinkageType linkageType = (BfIRLinkageType)mStream->Read();
- func->mLinkageType = linkageType;
- }
- break;
- case BfIRCmd_SaveDebugLocation:
- {
- mSavedDebugLocs.push_back(mBeModule->GetCurrentDebugLocation());
- }
- break;
- case BfIRCmd_RestoreDebugLocation:
- {
- mBeModule->SetCurrentDebugLocation(mSavedDebugLocs[mSavedDebugLocs.size() - 1]);
- mSavedDebugLocs.pop_back();
- }
- break;
- case BfIRCmd_ClearDebugLocation:
- {
- mBeModule->SetCurrentDebugLocation(NULL);
- }
- break;
- case BfIRCmd_ClearDebugLocationInst:
- {
- CMD_PARAM(BeValue*, instValue);
- auto inst = (BeInst*)instValue;
- inst->mDbgLoc = NULL;
- }
- break;
- case BfIRCmd_ClearDebugLocationInstLast:
- {
- if ((mBeModule->mActiveBlock != NULL) && (!mBeModule->mActiveBlock->mInstructions.IsEmpty()))
- {
- auto inst = mBeModule->mActiveBlock->mInstructions.back();
- inst->mDbgLoc = NULL;
- }
- }
- break;
- case BfIRCmd_UpdateDebugLocation:
- {
- CMD_PARAM(BeValue*, instValue);
- auto inst = (BeInst*)instValue;
- inst->mDbgLoc = mBeModule->mCurDbgLoc;
- }
- break;
- case BfIRCmd_SetCurrentDebugLocation:
- {
- CMD_PARAM(int, line);
- CMD_PARAM(int, column);
- CMD_PARAM(BeMDNode*, diScope);
- CMD_PARAM(BeMDNode*, diInlinedAt);
- mBeModule->SetCurrentDebugLocation(line - 1, column - 1, diScope, (BeDbgLoc*)diInlinedAt);
- }
- break;
- case BfIRCmd_Nop:
- {
- mBeModule->CreateNop();
- }
- break;
- case BfIRCmd_EnsureInstructionAt:
- {
- mBeModule->AllocInst<BeEnsureInstructionAtInst>();
- }
- break;
- case BfIRCmd_StatementStart:
- {
- }
- break;
- case BfIRCmd_ObjectAccessCheck:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(bool, useAsm);
- auto inst = mBeModule->AllocInst<BeObjectAccessCheckInst>();
- inst->mValue = val;
- SetResult(curId, mBeModule->GetInsertBlock());
- }
- break;
- case BfIRCmd_DbgInit:
- {
- /*mDIBuilder = new BeDIBuilder(*mBeModule); */
- mBeModule->mDbgModule = new BeDbgModule();
- mBeModule->mDbgModule->mBeModule = mBeModule;
- }
- break;
- case BfIRCmd_DbgFinalize:
- {
- /*for (auto typeEntryPair : mTypes)
- {
- auto& typeEntry = typeEntryPair.second;
- if (typeEntry.mInstDIType != NULL)
- typeEntry.mInstDIType->resolveCycles();
- }
- mDIBuilder->finalize();*/
-
- }
- break;
- case BfIRCmd_DbgCreateCompileUnit:
- {
- CMD_PARAM(int, lang);
- CMD_PARAM(String, fileName);
- CMD_PARAM(String, directory);
- CMD_PARAM(String, producer);
- CMD_PARAM(bool, isOptimized);
- CMD_PARAM(String, flags);
- CMD_PARAM(int, runtimeVer);
- CMD_PARAM(bool, linesOnly);
-
- mBeModule->mDbgModule->mFileName = fileName;
- mBeModule->mDbgModule->mDirectory = directory;
- mBeModule->mDbgModule->mProducer = producer;
- //mDebugging = fileName == "TestToots";
- SetResult(curId, mBeModule->mDbgModule);
- }
- break;
- case BfIRCmd_DbgCreateFile:
- {
- CMD_PARAM(String, fileName);
- CMD_PARAM(String, directory);
- auto dbgFile = mBeModule->mDbgModule->mFiles.Alloc();
- dbgFile->mFileName = fileName;
- dbgFile->mDirectory = directory;
- dbgFile->mIdx = (int)mBeModule->mDbgModule->mFiles.size() - 1;
- SetResult(curId, dbgFile);
- }
- break;
- case BfIRCmd_DbgGetCurrentLocation:
- {
- SetResult(curId, mBeModule->mCurDbgLoc);
- }
- break;
- case BfIRCmd_DbgSetType:
- {
- CMD_PARAM(int, typeId);
- CMD_PARAM(BeMDNode*, type);
- GetTypeEntry(typeId).mDIType = (BeDbgType*)type;
- }
- break;
- case BfIRCmd_DbgSetInstType:
- {
- CMD_PARAM(int, typeId);
- CMD_PARAM(BeMDNode*, type);
- GetTypeEntry(typeId).mInstDIType = (BeDbgType*)type;
- }
- break;
- case BfIRCmd_DbgGetType:
- {
- CMD_PARAM(int, typeId);
- SetResult(curId, GetTypeEntry(typeId).mDIType);
- }
- break;
- case BfIRCmd_DbgGetTypeInst:
- {
- CMD_PARAM(int, typeId);
- SetResult(curId, GetTypeEntry(typeId).mInstDIType);
- }
- break;
- case BfIRCmd_DbgTrackDITypes:
- {
- CMD_PARAM(int, typeId);
- auto& typeEntry = GetTypeEntry(typeId);
- /*if (typeEntry.mDIType != NULL)
- BeMetadataTracking::track(*(BeMetadata**)&typeEntry.mDIType);
- if (typeEntry.mInstDIType != NULL)
- BeMetadataTracking::track(*(BeMetadata**)&typeEntry.mInstDIType);*/
- //NotImpl();
- }
- break;
- case BfIRCmd_DbgCreateNamespace:
- {
- CMD_PARAM(BeMDNode*, scope);
- CMD_PARAM(String, name);
- CMD_PARAM(BeMDNode*, file);
- CMD_PARAM(int, lineNum);
- auto dbgNamespace = mBeModule->mOwnedValues.Alloc<BeDbgNamespace>();
- dbgNamespace->mScope = scope;
- dbgNamespace->mName = name;
- SetResult(curId, dbgNamespace);
- }
- break;
- case BfIRCmd_DbgCreateImportedModule:
- {
- CMD_PARAM(BeMDNode*, context);
- CMD_PARAM(BeMDNode*, namespaceNode);
- CMD_PARAM(int, lineNum);
- /*SetResult(curId, mDIBuilder->createImportedModule((BeDIScope*)context, (BeDINamespace*)namespaceNode, lineNum));*/
- //NotImpl();
- }
- break;
- case BfIRCmd_DbgCreateBasicType:
- {
- CMD_PARAM(String, name);
- CMD_PARAM(int64, sizeInBits);
- CMD_PARAM(int64, alignInBits);
- CMD_PARAM(int, encoding);
- auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgBasicType>();
- dbgType->mName = name;
- dbgType->mSize = (int)(sizeInBits / 8);
- dbgType->mAlign = (int)(alignInBits / 8);
- dbgType->mEncoding = encoding;
-
- SetResult(curId, dbgType);
- }
- break;
- case BfIRCmd_DbgCreateStructType:
- {
- CMD_PARAM(BeMDNode*, context);
- CMD_PARAM(String, name);
- CMD_PARAM(BeMDNode*, file);
- CMD_PARAM(int, lineNum);
- CMD_PARAM(int64, sizeInBits);
- CMD_PARAM(int64, alignInBits);
- CMD_PARAM(int, flags);
- CMD_PARAM(BeMDNode*, derivedFrom);
- CMD_PARAM(CmdParamVec<BeMDNode*>, members);
-
- auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgStructType>();
- dbgType->mScope = context;
- dbgType->mName = name;
- dbgType->mSize = (int)(sizeInBits / 8);
- dbgType->mAlign = (int)(alignInBits / 8);
- dbgType->mDerivedFrom = (BeDbgType*)derivedFrom;
- dbgType->mDefFile = (BeDbgFile*)file;
- dbgType->mDefLine = lineNum - 1;
- dbgType->mIsFullyDefined = true;
- dbgType->SetMembers(members);
-
- SetResult(curId, dbgType);
- }
- break;
- case BfIRCmd_DbgCreateEnumerationType:
- {
- CMD_PARAM(BeMDNode*, context);
- CMD_PARAM(String, name);
- CMD_PARAM(BeMDNode*, file);
- CMD_PARAM(int, lineNum);
- CMD_PARAM(int64, sizeInBits);
- CMD_PARAM(int64, alignInBits);
- CMD_PARAM(CmdParamVec<BeMDNode*>, members);
- CMD_PARAM(BeMDNode*, underlyingType);
-
- auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgEnumType>();
- dbgType->mScope = context;
- dbgType->mName = name;
- dbgType->mSize = (int)(sizeInBits / 8);
- dbgType->mAlign = (int)(alignInBits / 8);
- dbgType->mIsFullyDefined = true;
- dbgType->mElementType = (BeDbgType*)underlyingType;
- for (auto member : members)
- {
- if (auto enumMember = BeValueDynCast<BeDbgEnumMember>(member))
- {
- dbgType->mMembers.push_back(enumMember);
- }
- else
- NotImpl();
- }
- //dbgType->mDefFile = (BeDbgFile*)file;
- //dbgType->mDefLine = line - 1;
- SetResult(curId, dbgType);
- }
- break;
- case BfIRCmd_DbgCreatePointerType:
- {
- CMD_PARAM(BeMDNode*, elementTypeNode);
-
- BeDbgType* elementType = (BeDbgType*)elementTypeNode;
- BeDbgType* useType = elementType->FindDerivedType(BeDbgPointerType::TypeId);
- if (useType == NULL)
- {
- auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgPointerType>();
- dbgType->mElement = elementType;
- dbgType->mSize = mPtrSize;
- dbgType->mAlign = mPtrSize;
- elementType->mDerivedTypes.PushFront(dbgType, &mBeModule->mAlloc);
- useType = dbgType;
- }
- SetResult(curId, useType);
- }
- break;
- case BfIRCmd_DbgCreateReferenceType:
- {
- CMD_PARAM(BeMDNode*, elementTypeNode);
- auto useType = mBeModule->mDbgModule->CreateReferenceType((BeDbgType*)elementTypeNode);
- SetResult(curId, useType);
- }
- break;
- case BfIRCmd_DbgCreateConstType:
- {
- CMD_PARAM(BeMDNode*, elementTypeNode);
- BeDbgType* elementType = (BeDbgType*)elementTypeNode;
- BeDbgType* useType = elementType->FindDerivedType(BeDbgConstType::TypeId);
- if (useType == NULL)
- {
- auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgConstType>();
- dbgType->mElement = elementType;
- elementType->mDerivedTypes.PushFront(dbgType, &mBeModule->mAlloc);
- useType = dbgType;
- }
- SetResult(curId, useType);
- }
- break;
- case BfIRCmd_DbgCreateArtificialType:
- {
- CMD_PARAM(BeMDNode*, diType);
- //auto dbgType = mBeModule->mOwnedValues.Alloc<BeDbgArtificialType>();
- //dbgType->mElement = (BeDbgType*)diType;
- // Does the artificial thing do anything for us actually?
- auto dbgType = diType;
- SetResult(curId, dbgType);
- }
- break;
- case BfIRCmd_DbgCreateArrayType:
- {
- CMD_PARAM(int64, sizeInBits);
- CMD_PARAM(int64, alignInBits);
- CMD_PARAM(BeMDNode*, elementType);
- CMD_PARAM(int64, numElements);
- auto dbgArray = mBeModule->mDbgModule->mTypes.Alloc<BeDbgArrayType>();
- dbgArray->mSize = (int)(sizeInBits / 8);
- dbgArray->mAlign = (int)(alignInBits / 8);
- dbgArray->mElement = (BeDbgType*)elementType;
- dbgArray->mNumElements = numElements;
- SetResult(curId, dbgArray);
- }
- break;
- case BfIRCmd_DbgCreateReplaceableCompositeType:
- {
- CMD_PARAM(int, tag);
- CMD_PARAM(String, name);
- CMD_PARAM(BeMDNode*, scope);
- CMD_PARAM(BeMDNode*, file);
- CMD_PARAM(int, line);
- CMD_PARAM(int64, sizeInBits);
- CMD_PARAM(int64, alignInBits);
- CMD_PARAM(int, flags);
- if (tag == llvm::dwarf::DW_TAG_structure_type)
- {
- auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgStructType>();
- dbgType->mScope = scope;
- dbgType->mName = name;
- dbgType->mSize = (int)(sizeInBits / 8);
- dbgType->mAlign = (int)(alignInBits / 8);
- dbgType->mDefFile = (BeDbgFile*)file;
- dbgType->mDefLine = line - 1;
- SetResult(curId, dbgType);
- }
- else if (tag == llvm::dwarf::DW_TAG_enumeration_type)
- {
- auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgEnumType>();
- dbgType->mScope = scope;
- dbgType->mName = name;
- dbgType->mSize = (int)(sizeInBits / 8);
- dbgType->mAlign = (int)(alignInBits / 8);
- //dbgType->mDefFile = (BeDbgFile*)file;
- //dbgType->mDefLine = line - 1;
- SetResult(curId, dbgType);
- }
- else
- NotImpl();
- }
- break;
- case BfIRCmd_DbgCreateForwardDecl:
- {
- CMD_PARAM(int, tag);
- CMD_PARAM(String, name);
- CMD_PARAM(BeMDNode*, scope);
- CMD_PARAM(BeMDNode*, file);
- CMD_PARAM(int, line);
- if (tag == llvm::dwarf::DW_TAG_structure_type)
- {
- auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgStructType>();
- dbgType->mScope = scope;
- dbgType->mName = name;
- dbgType->mDefFile = (BeDbgFile*)file;
- dbgType->mDefLine = line;
- SetResult(curId, dbgType);
- }
- else if (tag == llvm::dwarf::DW_TAG_enumeration_type)
- {
- auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgEnumType>();
- dbgType->mScope = scope;
- dbgType->mName = name;
- SetResult(curId, dbgType);
- }
- else
- NotImpl();
- }
- break;
- case BfIRCmd_DbgCreateSizedForwardDecl:
- {
- CMD_PARAM(int, tag);
- CMD_PARAM(String, name);
- CMD_PARAM(BeMDNode*, scope);
- CMD_PARAM(BeMDNode*, file);
- CMD_PARAM(int, line);
- CMD_PARAM(int64, sizeInBits);
- CMD_PARAM(int64, alignInBits);
-
- if (tag == llvm::dwarf::DW_TAG_structure_type)
- {
- auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgStructType>();
- dbgType->mDefFile = (BeDbgFile*)file;
- dbgType->mDefLine = line;
- dbgType->mScope = scope;
- dbgType->mName = name;
- dbgType->mSize = (int)(sizeInBits / 8);
- dbgType->mAlign = (int)(alignInBits / 8);
- SetResult(curId, dbgType);
- }
- else if (tag == llvm::dwarf::DW_TAG_enumeration_type)
- {
- auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgEnumType>();
- dbgType->mScope = scope;
- dbgType->mName = name;
- dbgType->mSize = (int)(sizeInBits / 8);
- dbgType->mAlign = (int)(alignInBits / 8);
- SetResult(curId, dbgType);
- }
- else
- NotImpl();
- }
- break;
- case BeIRCmd_DbgSetTypeSize:
- {
- CMD_PARAM(BeMDNode*, mdType);
- CMD_PARAM(int64, sizeInBits);
- CMD_PARAM(int64, alignInBits);
- auto dbgType = (BeDbgType*)mdType;
- dbgType->mSize = (int)(sizeInBits / 8);
- dbgType->mAlign = (int)(alignInBits / 8);
- }
- break;
- case BfIRCmd_DbgReplaceAllUses:
- {
- CMD_PARAM(BeMDNode*, diPrevNode);
- CMD_PARAM(BeMDNode*, diNewNode);
- /*diPrevNode->replaceAllUsesWith(diNewNode); */
- NotImpl();
- }
- break;
- case BfIRCmd_DbgDeleteTemporary:
- {
- CMD_PARAM(BeMDNode*, diNode);
- /*BeMDNode::deleteTemporary(diNode);*/
- NotImpl();
- }
- break;
- case BfIRCmd_DbgMakePermanent:
- {
- CMD_PARAM(BeMDNode*, diNode);
- CMD_PARAM(BeMDNode*, diBaseType);
- CMD_PARAM(CmdParamVec<BeMDNode*>, members);
-
- if (auto dbgType = BeValueDynCast<BeDbgStructType>(diNode))
- {
- dbgType->SetMembers(members);
- }
- else if (auto dbgType = BeValueDynCast<BeDbgEnumType>(diNode))
- {
- dbgType->mElementType = (BeDbgType*)diBaseType;
- dbgType->SetMembers(members);
- }
- else
- NotImpl();
- SetResult(curId, diNode);
- break;
- }
- case BfIRCmd_CreateEnumerator:
- {
- CMD_PARAM(String, name);
- CMD_PARAM(int64, val);
- auto enumValue = mBeModule->mOwnedValues.Alloc<BeDbgEnumMember>();
- enumValue->mName = name;
- enumValue->mValue = val;
- SetResult(curId, enumValue);
- }
- break;
- case BfIRCmd_DbgCreateMemberType:
- {
- CMD_PARAM(BeMDNode*, scope);
- CMD_PARAM(String, name);
- CMD_PARAM(BeMDNode*, file);
- CMD_PARAM(int, lineNumber);
- CMD_PARAM(int64, sizeInBits);
- CMD_PARAM(int64, alignInBits);
- CMD_PARAM(int64, offsetInBits);
- CMD_PARAM(int, flags);
- CMD_PARAM(BeMDNode*, type);
- auto dbgMember = mBeModule->mOwnedValues.Alloc<BeDbgStructMember>();
- dbgMember->mName = name;
- dbgMember->mType = (BeDbgType*)type;
- dbgMember->mOffset = (int)(offsetInBits / 8);
- dbgMember->mFlags = flags;
- SetResult(curId, dbgMember);
- }
- break;
- case BfIRCmd_DbgStaticCreateMemberType:
- {
- CMD_PARAM(BeMDNode*, scope);
- CMD_PARAM(String, name);
- CMD_PARAM(BeMDNode*, file);
- CMD_PARAM(int, lineNumber);
- CMD_PARAM(BeMDNode*, type);
- CMD_PARAM(int, flags);
- CMD_PARAM(BeConstant*, val);
-
- auto dbgMember = mBeModule->mOwnedValues.Alloc<BeDbgStructMember>();
- dbgMember->mName = name;
- dbgMember->mType = (BeDbgType*)type;
- dbgMember->mOffset = -1;
- dbgMember->mStaticValue = val;
- dbgMember->mFlags = flags;
- dbgMember->mIsStatic = true;
- SetResult(curId, dbgMember);
- }
- break;
- case BfIRCmd_DbgCreateInheritance:
- {
- CMD_PARAM(BeMDNode*, type);
- CMD_PARAM(BeMDNode*, baseType);
- CMD_PARAM(int64, baseOffset);
- CMD_PARAM(int, flags);
- auto dbgInheritance = mBeModule->mAlloc.Alloc<BeDbgInheritance>();
- dbgInheritance->mBaseType = (BeDbgType*)baseType;
- SetResult(curId, dbgInheritance);
- }
- break;
- case BfIRCmd_DbgCreateMethod:
- {
- CMD_PARAM(BeMDNode*, context);
- CMD_PARAM(String, name);
- CMD_PARAM(String, linkageName);
- CMD_PARAM(BeMDNode*, file);
- CMD_PARAM(int, lineNum);
- CMD_PARAM(BeMDNode*, type);
- CMD_PARAM(bool, isLocalToUnit);
- CMD_PARAM(bool, isDefinition);
- CMD_PARAM(int, vk);
- CMD_PARAM(int, vIndex);
- CMD_PARAM(BeMDNode*, vTableHolder);
- CMD_PARAM(int, flags);
- CMD_PARAM(bool, isOptimized);
- CMD_PARAM(BeValue*, fn);
- CMD_PARAM(CmdParamVec<BeMDNode*>, genericArgs);
- CMD_PARAM(CmdParamVec<BeConstant*>, genericConstValueArgs);
- auto dbgFunc = mBeModule->mOwnedValues.Alloc<BeDbgFunction>();
- dbgFunc->mScope = context;
- dbgFunc->mFile = (BeDbgFile*)file;
- dbgFunc->mLine = lineNum - 1;
- dbgFunc->mType = (BeDbgFunctionType*)type;
- dbgFunc->mName = name;
- dbgFunc->mLinkageName = linkageName;
- dbgFunc->mValue = (BeFunction*)fn;
- dbgFunc->mIsLocalToUnit = isLocalToUnit;
- dbgFunc->mVK = vk;
- dbgFunc->mVIndex = vIndex;
- dbgFunc->mIsStaticMethod = (flags & llvm::DINode::FlagStaticMember) != 0;
- dbgFunc->mFlags = flags;
- for (auto arg : genericArgs)
- {
- BF_ASSERT(arg != NULL);
- dbgFunc->mGenericArgs.Add((BeDbgType*)arg);
- }
- for (auto genericConstValue : genericConstValueArgs)
- dbgFunc->mGenericConstValueArgs.Add(genericConstValue);
-
- if (dbgFunc->mValue != NULL)
- dbgFunc->mValue->mDbgFunction = dbgFunc;
- mBeModule->mDbgModule->mFuncs.push_back(dbgFunc);
- SetResult(curId, dbgFunc);
- }
- break;
- case BfIRCmd_DbgCreateFunction:
- {
- CMD_PARAM(BeMDNode*, context);
- CMD_PARAM(String, name);
- CMD_PARAM(String, linkageName);
- CMD_PARAM(BeMDNode*, file);
- CMD_PARAM(int, lineNum);
- CMD_PARAM(BeMDNode*, type);
- CMD_PARAM(bool, isLocalToUnit);
- CMD_PARAM(bool, isDefinition);
- CMD_PARAM(int, scopeLine);
- CMD_PARAM(int, flags);
- CMD_PARAM(bool, isOptimized);
- CMD_PARAM(BeValue*, fn);
-
- auto dbgFunc = mBeModule->mOwnedValues.Alloc<BeDbgFunction>();
- dbgFunc->mScope = context;
- dbgFunc->mFile = (BeDbgFile*)file;
- dbgFunc->mLine = lineNum - 1;
- dbgFunc->mType = (BeDbgFunctionType*)type;
- dbgFunc->mName = name;
- dbgFunc->mLinkageName = linkageName;
- dbgFunc->mValue = (BeFunction*)fn;
- dbgFunc->mIsLocalToUnit = isLocalToUnit;
- dbgFunc->mFlags = flags;
- /*if (auto dbgStructType = BeValueDynCast<BeDbgStructType>(context))
- {
- // This will get added to the struct later
- }
- else
- {
-
- }*/
- if (dbgFunc->mValue != NULL)
- dbgFunc->mValue->mDbgFunction = dbgFunc;
- mBeModule->mDbgModule->mFuncs.push_back(dbgFunc);
- SetResult(curId, dbgFunc);
- }
- break;
- case BfIRCmd_DbgCreateParameterVariable:
- {
- CMD_PARAM(BeMDNode*, scope);
- CMD_PARAM(String, name);
- CMD_PARAM(int, argNo);
- CMD_PARAM(BeMDNode*, file);
- CMD_PARAM(int, lineNum);
- CMD_PARAM(BeMDNode*, type);
- CMD_PARAM(bool, alwaysPreserve);
- CMD_PARAM(int, flags);
- auto dbgFunc = (BeDbgFunction*)scope;
- auto dbgVar = mBeModule->mOwnedValues.Alloc<BeDbgVariable>();
- dbgVar->mName = name;
- dbgVar->mType = (BeDbgType*)type;
- dbgVar->mParamNum = argNo - 1;
- int argIdx = argNo - 1;
- //for (int i = ; i <= argNo - 1; i++)
- while (argIdx >= (int)dbgFunc->mVariables.size())
- dbgFunc->mVariables.push_back(NULL);
- if (dbgFunc->mVariables[argIdx] == NULL)
- dbgFunc->mVariables[argIdx] = dbgVar;
- else
- {
- BF_ASSERT(dbgFunc->mVariables[argIdx]->mParamNum == -1);
- dbgFunc->mVariables.Insert(argIdx, dbgVar);
- }
- //mActiveFunction->mDbgFunction->mVariables.push_back(dbgVar);
-
- //dbgVar->mValue = mBeModule->GetArgument(argNo - 1);
- SetResult(curId, dbgVar);
- }
- break;
- case BfIRCmd_DbgCreateSubroutineType:
- {
- CMD_PARAM(CmdParamVec<BeMDNode*>, elements);
- auto dbgFuncType = mBeModule->mOwnedValues.Alloc<BeDbgFunctionType>();
- if (!elements.empty())
- {
- dbgFuncType->mReturnType = (BeDbgType*)elements[0];
- for (int i = 1; i < (int)elements.size(); i++)
- dbgFuncType->mParams.push_back((BeDbgType*)elements[i]);
- }
- SetResult(curId, dbgFuncType);
- }
- break;
- case BfIRCmd_DbgCreateAutoVariable:
- {
- CMD_PARAM(BeMDNode*, scope);
- CMD_PARAM(String, name);
- CMD_PARAM(BeMDNode*, file);
- CMD_PARAM(int, lineNo);
- CMD_PARAM(BeMDNode*, type);
- CMD_PARAM(int, initType);
-
- auto dbgVar = mBeModule->mOwnedValues.Alloc<BeDbgVariable>();
- dbgVar->mName = name;
- dbgVar->mType = (BeDbgType*)type;
- dbgVar->mScope = scope;
- dbgVar->mInitType = (BfIRInitType)initType;
- mActiveFunction->mDbgFunction->mVariables.push_back(dbgVar);
- BF_ASSERT(name != "__CRASH_AUTOVARIABLE__");
- SetResult(curId, dbgVar);
- }
- break;
- case BfIRCmd_DbgInsertValueIntrinsic:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(BeMDNode*, varInfo);
- auto dbgVar = BeValueDynCast<BeDbgVariable>(varInfo);
- //dbgVar->mValue = val;
- //dbgVar->mDeclDbgLoc = mBeModule->mCurDbgLoc;
- if (val == NULL)
- {
- val = mBeModule->GetConstant(mBeContext->GetPrimitiveType(BeTypeCode_Int32), (int64)0);
- }
- auto inst = mBeModule->AllocInst<BeDbgDeclareInst>();
- inst->mValue = val;
- inst->mDbgVar = dbgVar;
- inst->mIsValue = true;
- SetResult(curId, inst);
- }
- break;
- case BfIRCmd_DbgInsertDeclare:
- {
- CMD_PARAM(BeValue*, val);
- CMD_PARAM(BeMDNode*, varInfo);
- CMD_PARAM(BeValue*, insertBefore);
- auto dbgVar = BeValueDynCast<BeDbgVariable>(varInfo);
- //dbgVar->mValue = val;
- //dbgVar->mDeclDbgLoc = mBeModule->mCurDbgLoc;
- auto inst = mBeModule->mAlloc.Alloc<BeDbgDeclareInst>();
- inst->mValue = val;
- inst->mDbgVar = dbgVar;
- inst->mIsValue = false;
- if (insertBefore == NULL)
- mBeModule->AddInst(inst);
- else
- NotImpl();
- SetResult(curId, inst);
- }
- break;
- case BfIRCmd_DbgLifetimeEnd:
- {
- CMD_PARAM(BeMDNode*, varInfo);
- auto inst = mBeModule->AllocInst<BeLifetimeEndInst>();
- inst->mPtr = varInfo;
- }
- break;
- case BfIRCmd_DbgCreateGlobalVariable:
- {
- CMD_PARAM(BeMDNode*, context);
- CMD_PARAM(String, name);
- CMD_PARAM(String, linkageName);
- CMD_PARAM(BeMDNode*, file);
- CMD_PARAM(int, lineNum);
- CMD_PARAM(BeMDNode*, type);
- CMD_PARAM(bool, isLocalToUnit);
- CMD_PARAM(BeConstant*, val);
- CMD_PARAM(BeMDNode*, decl);
- auto dbgGlobalVariable = mBeModule->mDbgModule->mGlobalVariables.Alloc();
- dbgGlobalVariable->mContext = context;
- dbgGlobalVariable->mName = name;
- dbgGlobalVariable->mLinkageName = linkageName;
- dbgGlobalVariable->mFile = (BeDbgFile*)file;
- dbgGlobalVariable->mLineNum = lineNum;
- dbgGlobalVariable->mType = (BeDbgType*)type;
- dbgGlobalVariable->mIsLocalToUnit = isLocalToUnit;
- dbgGlobalVariable->mValue = val;
- dbgGlobalVariable->mDecl = decl;
- SetResult(curId, dbgGlobalVariable);
- }
- break;
- case BfIRCmd_DbgCreateLexicalBlock:
- {
- CMD_PARAM(BeMDNode*, scope);
- CMD_PARAM(BeMDNode*, file);
- CMD_PARAM(int, lineNum);
- CMD_PARAM(int, col);
- auto dbgLexicalBlock = mBeModule->mOwnedValues.Alloc<BeDbgLexicalBlock>();
- BF_ASSERT(BeValueDynCast<BeDbgFile>(file) != NULL);
- dbgLexicalBlock->mFile = (BeDbgFile*)file;
- dbgLexicalBlock->mScope = scope;
- dbgLexicalBlock->mId = mBeModule->mCurLexBlockId++;
-
- SetResult(curId, dbgLexicalBlock);
- }
- break;
- case BfIRCmd_DbgCreateAnnotation:
- {
- CMD_PARAM(BeMDNode*, scope);
- CMD_PARAM(String, name);
- CMD_PARAM(BeValue*, value);
- if (auto dbgFunc = BeValueDynCast<BeDbgFunction>(scope))
- {
- auto beType = value->GetType();
- BeDbgType** dbgTypePtr;
- if (mOnDemandTypeMap.TryAdd(beType, NULL, &dbgTypePtr))
- {
- auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgBasicType>();
- dbgType->mSize = beType->mSize;
- dbgType->mAlign = beType->mAlign;
- dbgType->mEncoding = llvm::dwarf::DW_ATE_signed;
- *dbgTypePtr = dbgType;
- }
-
- auto dbgVar = mBeModule->mOwnedValues.Alloc<BeDbgVariable>();
- dbgVar->mName = "#" + name;
- dbgVar->mType = *dbgTypePtr;
- dbgVar->mValue = value;
- dbgVar->mScope = scope;
- dbgFunc->mVariables.Add(dbgVar);
- auto inst = mBeModule->AllocInst<BeDbgDeclareInst>();
- inst->mValue = value;
- inst->mDbgVar = dbgVar;
- inst->mIsValue = true;
- }
- else
- NotImpl();
- }
- break;
- default:
- BF_FATAL("Unhandled");
- break;
- }
- #ifdef CODEGEN_TRACK
- gBEMemReporter.EndSection();
- gBEMemReporterSize += mStream->GetReadPos() - curId;
- #endif
- }
- void BeIRCodeGen::SetConfigConst(int idx, int value)
- {
- BF_ASSERT(idx == (int)mConfigConsts.size());
- mConfigConsts.Add(value);
- }
- BeValue* BeIRCodeGen::GetBeValue(int id)
- {
- auto& result = mResults[id];
- BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Value);
- return result.mBeValue;
- }
- BeType* BeIRCodeGen::GetBeType(int id)
- {
- auto& result = mResults[id];
- BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Type);
- return result.mBeType;
- }
- BeBlock* BeIRCodeGen::GetBeBlock(int id)
- {
- auto& result = mResults[id];
- BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Block);
- return result.mBeBlock;
- }
- BeMDNode* BeIRCodeGen::GetBeMetadata(int id)
- {
- auto& result = mResults[id];
- BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Metadata);
- return result.mBeMetadata;
- }
- BeType* BeIRCodeGen::GetBeTypeById(int id)
- {
- return GetTypeEntry(id).mBeType;
- }
|