BeIRCodeGen.cpp 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053
  1. #include "BeIRCodeGen.h"
  2. #include "../Compiler/BfIRCodeGen.h"
  3. #include "BeDbgModule.h"
  4. #include "BeefySysLib/util/BeefPerf.h"
  5. #include "BeefySysLib/util/AllocDebug.h"
  6. USING_NS_BF;
  7. //#define CODEGEN_TRACK
  8. #ifdef CODEGEN_TRACK
  9. #include "../Compiler/MemReporter.h"
  10. MemReporter gBEMemReporter;
  11. CritSect gBEMemReporterCritSect;
  12. int gBEMemReporterSize = 0;
  13. #define BE_MEM_START \
  14. int startPos = mStream->GetReadPos();
  15. #define BE_MEM_END(name) \
  16. gBEMemReporter.Add(name, mStream->GetReadPos() - startPos);
  17. static const char* gIRCmdNames[] =
  18. {
  19. "Module_Start",
  20. "Module_SetTargetTriple",
  21. "Module_AddModuleFlag",
  22. "WriteIR",
  23. "SetType",
  24. "SetInstType",
  25. "PrimitiveType",
  26. "CreateStruct",
  27. "StructSetBody",
  28. "Type",
  29. "TypeInst",
  30. "TypeInstPtr",
  31. "GetType",
  32. "GetPointerToFuncType",
  33. "GetPointerToType",
  34. "GetSizedArrayType",
  35. "CreateConstStruct",
  36. "CreateConstStructZero",
  37. "CreateConstArray",
  38. "CreateConstString",
  39. "SetName",
  40. "CreateUndefValue",
  41. "NumericCast",
  42. "CmpEQ",
  43. "CmpNE",
  44. "CmpSLT",
  45. "CmpULT",
  46. "CmpSLE",
  47. "CmpULE",
  48. "CmpSGT",
  49. "CmpUGT",
  50. "CmpSGE",
  51. "CmpUGE",
  52. "Add",
  53. "Sub",
  54. "Mul",
  55. "SDiv",
  56. "UDiv",
  57. "SRem",
  58. "URem",
  59. "And",
  60. "Or",
  61. "Xor",
  62. "Shl",
  63. "AShr",
  64. "LShr",
  65. "Neg",
  66. "Not",
  67. "BitCast",
  68. "PtrToInt",
  69. "IntToPtr",
  70. "InboundsGEP1_32",
  71. "InboundsGEP2_32",
  72. "InBoundsGEP1",
  73. "InBoundsGEP2",
  74. "IsNull",
  75. "IsNotNull",
  76. "ExtractValue",
  77. "InsertValue",
  78. "Alloca",
  79. "AllocaArray",
  80. "SetAllocaAlignment",
  81. "SetAllocaNoChkStkHint",
  82. "SetAllocaForceMem",
  83. "LifetimeStart",
  84. "LifetimeEnd",
  85. "LifetimeExtend",
  86. "ValueScopeStart",
  87. "ValueScopeRetain",
  88. "ValueScopeSoftEnd",
  89. "ValueScopeHardEnd",
  90. "Load",
  91. "AlignedLoad",
  92. "Store",
  93. "AlignedStore",
  94. "MemSet",
  95. "Fence",
  96. "StackSave",
  97. "StackRestore",
  98. "GlobalVariable",
  99. "GlobalVar_SetUnnamedAddr",
  100. "GlobalVar_SetInitializer",
  101. "GlobalVar_SetAlignment",
  102. "GlobalStringPtr",
  103. "CreateBlock",
  104. "MaybeChainNewBlock",
  105. "AddBlock",
  106. "DropBlocks",
  107. "MergeBlockDown",
  108. "SetInsertPoint",
  109. "SetInsertPointAtStart",
  110. "EraseFromParent",
  111. "DeleteBlock",
  112. "EraseInstFromParent",
  113. "CreateBr",
  114. "CreateBr_NoCollapse",
  115. "CreateCondBr",
  116. "MoveBlockToEnd",
  117. "CreateSwitch",
  118. "AddSwitchCase",
  119. "SetSwitchDefaultDest",
  120. "CreatePhi",
  121. "AddPhiIncoming",
  122. "GetIntrinsic",
  123. "CreateFunctionType",
  124. "CreateFunction",
  125. "EnsureFunctionPatchable",
  126. "RemapBindFunction",
  127. "SetActiveFunction",
  128. "CreateCall",
  129. "SetCallCallingConv",
  130. "SetFuncCallingConv",
  131. "SetTailCall",
  132. "SetCallAttribute",
  133. "CreateRet",
  134. "CreateRetVoid",
  135. "CreateUnreachable",
  136. "Call_AddAttribute",
  137. "Call_AddAttribute1",
  138. "Func_AddAttribute",
  139. "Func_AddAttribute1",
  140. "Func_SetParamName",
  141. "Func_DeleteBody",
  142. "Func_EraseFromParent",
  143. "Func_SetLinkage",
  144. "SaveDebugLocation",
  145. "RestoreDebugLocation",
  146. "ClearDebugLocation",
  147. "ClearDebugLocationInst",
  148. "UpdateDebugLocation",
  149. "SetCurrentDebugLocation",
  150. "Nop",
  151. "EnsureInstructionAt",
  152. "StatementStart",
  153. "ObjectAccessCheck",
  154. "DbgInit",
  155. "DbgFinalize",
  156. "DbgCreateCompileUnit",
  157. "DbgCreateFile",
  158. "ConstValueI64",
  159. "DbgGetCurrentLocation",
  160. "DbgSetType",
  161. "DbgSetInstType",
  162. "DbgGetType",
  163. "DbgGetTypeInst",
  164. "DbgTrackDITypes",
  165. "DbgCreateNamespace",
  166. "DbgCreateImportedModule",
  167. "DbgCreateBasicType",
  168. "DbgCreateStructType",
  169. "DbgCreateEnumerationType",
  170. "DbgCreatePointerType",
  171. "DbgCreateReferenceType",
  172. "DbgCreateConstType",
  173. "DbgCreateArtificialType",
  174. "DbgCreateArrayType",
  175. "DbgCreateReplaceableCompositeType",
  176. "DbgCreateForwardDecl",
  177. "DbgCreateSizedForwardDecl",
  178. "BeIRCmd_DbgSetTypeSize",
  179. "DbgReplaceAllUses",
  180. "DbgDeleteTemporary",
  181. "DbgMakePermanent",
  182. "CreateEnumerator",
  183. "DbgCreateMemberType",
  184. "DbgStaticCreateMemberType",
  185. "DbgCreateInheritance",
  186. "DbgCreateMethod",
  187. "DbgCreateFunction",
  188. "DbgCreateParameterVariable",
  189. "DbgCreateSubroutineType",
  190. "DbgCreateAutoVariable",
  191. "DbgInsertValueIntrinsic",
  192. "DbgInsertDeclare",
  193. "DbgLifetimeEnd",
  194. "DbgCreateGlobalVariable",
  195. "DbgCreateLexicalBlock",
  196. "DbgCreateLexicalBlockFile",
  197. "DbgCreateAnnotation"
  198. };
  199. BF_STATIC_ASSERT(BF_ARRAY_COUNT(gIRCmdNames) == BfIRCmd_COUNT);
  200. #else
  201. #define BE_MEM_START
  202. #define BE_MEM_END(name)
  203. #endif
  204. #pragma warning(disable:4146)
  205. #define CMD_PARAM(ty, name) ty name; Read(name);
  206. template <typename T>
  207. class CmdParamVec : public SizedArray<T, 8>
  208. {};
  209. BeIRCodeGen::BeIRCodeGen()
  210. {
  211. mBfIRBuilder = NULL;
  212. mStream = NULL;
  213. mActiveFunction = NULL;
  214. mBeContext = NULL;
  215. mBeModule = NULL;
  216. mHasDebugLoc = false;
  217. mDebugging = false;
  218. mCmdCount = 0;
  219. }
  220. BeIRCodeGen::~BeIRCodeGen()
  221. {
  222. BF_ASSERT(mSavedDebugLocs.size() == 0);
  223. delete mBeModule;
  224. delete mBeContext;
  225. delete mStream;
  226. }
  227. void BeIRCodeGen::Hash(BeHashContext& hashCtx)
  228. {
  229. hashCtx.Mixin(mPtrSize);
  230. hashCtx.Mixin(mIsOptimized);
  231. if (mBeModule != NULL)
  232. mBeModule->Hash(hashCtx);
  233. }
  234. bool BeIRCodeGen::IsModuleEmpty()
  235. {
  236. if (!mBeModule->mFunctions.IsEmpty())
  237. return false;
  238. if (!mBeModule->mGlobalVariables.IsEmpty())
  239. return false;
  240. return true;
  241. }
  242. void BeIRCodeGen::NotImpl()
  243. {
  244. BF_FATAL("Not implemented");
  245. }
  246. BeType* BeIRCodeGen::GetBeType(BfTypeCode typeCode, bool& isSigned)
  247. {
  248. isSigned = false;
  249. BeTypeCode beTypeCode = BeTypeCode_None;
  250. switch (typeCode)
  251. {
  252. case BfTypeCode_None:
  253. beTypeCode = BeTypeCode_None;
  254. break;
  255. case BfTypeCode_NullPtr:
  256. beTypeCode = BeTypeCode_NullPtr;
  257. break;
  258. case BfTypeCode_Boolean:
  259. beTypeCode = BeTypeCode_Boolean;
  260. break;
  261. case BfTypeCode_Int8:
  262. isSigned = true;
  263. beTypeCode = BeTypeCode_Int8;
  264. break;
  265. case BfTypeCode_UInt8:
  266. case BfTypeCode_Char8:
  267. beTypeCode = BeTypeCode_Int8;
  268. break;
  269. case BfTypeCode_Int16:
  270. isSigned = true;
  271. beTypeCode = BeTypeCode_Int16;
  272. break;
  273. case BfTypeCode_Char16:
  274. case BfTypeCode_UInt16:
  275. beTypeCode = BeTypeCode_Int16;
  276. break;
  277. case BfTypeCode_Int32:
  278. isSigned = true;
  279. beTypeCode = BeTypeCode_Int32;
  280. break;
  281. case BfTypeCode_UInt32:
  282. case BfTypeCode_Char32:
  283. beTypeCode = BeTypeCode_Int32;
  284. break;
  285. case BfTypeCode_Int64:
  286. isSigned = true;
  287. beTypeCode = BeTypeCode_Int64;
  288. break;
  289. case BfTypeCode_UInt64:
  290. beTypeCode = BeTypeCode_Int64;
  291. break;
  292. case BfTypeCode_IntPtr:
  293. BF_FATAL("Illegal");
  294. /*isSigned = true;
  295. if (mModule->mSystem->mPtrSize == 4)
  296. return llvm::Type::getInt32Ty(*mLLVMContext);
  297. else
  298. return llvm::Type::getInt64Ty(*mLLVMContext);*/
  299. case BfTypeCode_UIntPtr:
  300. BF_FATAL("Illegal");
  301. /*if (mModule->mSystem->mPtrSize == 4)
  302. return llvm::Type::getInt32Ty(*mLLVMContext);
  303. else
  304. return llvm::Type::getInt64Ty(*mLLVMContext);*/
  305. case BfTypeCode_Single:
  306. beTypeCode = BeTypeCode_Float;
  307. break;
  308. case BfTypeCode_Double:
  309. beTypeCode = BeTypeCode_Double;
  310. break;
  311. }
  312. return mBeContext->GetPrimitiveType(beTypeCode);
  313. }
  314. BeIRTypeEntry& BeIRCodeGen::GetTypeEntry(int typeId)
  315. {
  316. BeIRTypeEntry& typeEntry = mTypes[typeId];
  317. if (typeEntry.mTypeId == -1)
  318. typeEntry.mTypeId = typeId;
  319. return typeEntry;
  320. }
  321. void BeIRCodeGen::Init(const BfSizedArray<uint8>& buffer)
  322. {
  323. BP_ZONE("BeIRCodeGen::Init");
  324. BF_ASSERT(mStream == NULL);
  325. mStream = new ChunkedDataBuffer();
  326. mStream->InitFlatRef(buffer.mVals, buffer.mSize);
  327. #ifdef CODEGEN_TRACK
  328. AutoCrit autoCrit(gBEMemReporterCritSect);
  329. AutoMemReporter autoMemReporter(&gBEMemReporter, "BeIRCodeGen");
  330. #endif
  331. //
  332. {
  333. BP_ZONE("BeIRCodeGen::ProcessBfIRData.HandleNextCmds");
  334. while (mStream->GetReadPos() < buffer.mSize)
  335. {
  336. if (mFailed)
  337. break;
  338. HandleNextCmd();
  339. }
  340. }
  341. BF_ASSERT((mFailed) || (mStream->GetReadPos() == buffer.mSize));
  342. }
  343. void BeIRCodeGen::Process()
  344. {
  345. BP_ZONE("BeIRCodeGen::process");
  346. //mDebugging |= ((mBeModule->mDbgModule != NULL) && (mBeModule->mDbgModule->mFileName == "ClassQ"));
  347. if (mDebugging)
  348. {
  349. String dbgStr;
  350. dbgStr = mBeModule->ToString();
  351. OutputDebugStr(dbgStr);
  352. }
  353. mBeModule->DoInlining();
  354. if (mDebugging)
  355. {
  356. String dbgStr = "-------------- AFTER INLINING --------------\n";
  357. dbgStr += mBeModule->ToString();
  358. OutputDebugStr(dbgStr);
  359. }
  360. }
  361. void BeIRCodeGen::ProcessBfIRData(const BfSizedArray<uint8>& buffer)
  362. {
  363. BP_ZONE("BeIRCodeGen::ProcessBfIRData");
  364. Init(buffer);
  365. Process();
  366. }
  367. BfTypeCode BeIRCodeGen::GetTypeCode(BeType * type, bool isSigned)
  368. {
  369. switch (type->mTypeCode)
  370. {
  371. case BeTypeCode_Int8:
  372. return (isSigned) ? BfTypeCode_Int8 : BfTypeCode_UInt8;
  373. case BeTypeCode_Int16:
  374. return (isSigned) ? BfTypeCode_Int16 : BfTypeCode_UInt16;
  375. case BeTypeCode_Int32:
  376. return (isSigned) ? BfTypeCode_Int32 : BfTypeCode_UInt32;
  377. case BeTypeCode_Int64:
  378. return (isSigned) ? BfTypeCode_Int64 : BfTypeCode_UInt64;
  379. case BeTypeCode_Float:
  380. return BfTypeCode_Single;
  381. case BeTypeCode_Double:
  382. return BfTypeCode_Double;
  383. default:
  384. return BfTypeCode_None;
  385. }
  386. }
  387. void BeIRCodeGen::SetResult(int id, BeValue* value)
  388. {
  389. BeIRCodeGenEntry entry;
  390. entry.mKind = BeIRCodeGenEntryKind_Value;
  391. entry.mBeValue = value;
  392. mResults.TryAdd(id, entry);
  393. }
  394. void BeIRCodeGen::SetResult(int id, BeType* type)
  395. {
  396. BeIRCodeGenEntry entry;
  397. entry.mKind = BeIRCodeGenEntryKind_Type;
  398. entry.mBeType = type;
  399. mResults.TryAdd(id, entry);
  400. }
  401. void BeIRCodeGen::SetResult(int id, BeBlock* value)
  402. {
  403. BeIRCodeGenEntry entry;
  404. entry.mKind = BeIRCodeGenEntryKind_Block;
  405. entry.mBeBlock = value;
  406. mResults.TryAdd(id, entry);
  407. }
  408. void BeIRCodeGen::SetResult(int id, BeMDNode* md)
  409. {
  410. BeIRCodeGenEntry entry;
  411. entry.mKind = BeIRCodeGenEntryKind_Metadata;
  412. entry.mBeMetadata = md;
  413. mResults.TryAdd(id, entry);
  414. }
  415. int64 BeIRCodeGen::ReadSLEB128()
  416. {
  417. int64 val = 0;
  418. int64 shift = 0;
  419. uint8 byteVal;
  420. do
  421. {
  422. byteVal = mStream->Read();
  423. val |= ((int64)(byteVal & 0x7f)) << shift;
  424. shift += 7;
  425. } while (byteVal >= 128);
  426. // Sign extend negative numbers.
  427. if ((byteVal & 0x40) && (shift < 64))
  428. val |= (-1ULL) << shift;
  429. return val;
  430. }
  431. void BeIRCodeGen::Read(StringImpl& str)
  432. {
  433. BE_MEM_START;
  434. int len = (int)ReadSLEB128();
  435. str.Append('?', len);
  436. mStream->Read((void*)str.c_str(), len);
  437. BE_MEM_END("String");
  438. }
  439. void BeIRCodeGen::Read(int& i)
  440. {
  441. BE_MEM_START;
  442. i = (int)ReadSLEB128();
  443. BE_MEM_END("int");
  444. }
  445. void BeIRCodeGen::Read(int64& i)
  446. {
  447. BE_MEM_START;
  448. i = ReadSLEB128();
  449. BE_MEM_END("int64");
  450. }
  451. void BeIRCodeGen::Read(bool& val)
  452. {
  453. BE_MEM_START;
  454. val = mStream->Read() != 0;
  455. BE_MEM_END("bool");
  456. }
  457. void BeIRCodeGen::Read(BeIRTypeEntry*& type)
  458. {
  459. BE_MEM_START;
  460. int typeId = (int)ReadSLEB128();
  461. type = &GetTypeEntry(typeId);
  462. BE_MEM_END("BeIRTypeEntry");
  463. }
  464. void BeIRCodeGen::Read(BeType*& beType)
  465. {
  466. BE_MEM_START;
  467. BfIRType::TypeKind typeKind = (BfIRType::TypeKind)mStream->Read();
  468. if (typeKind == BfIRType::TypeKind::TypeKind_None)
  469. {
  470. beType = NULL;
  471. BE_MEM_END("BeType");
  472. return;
  473. }
  474. if (typeKind == BfIRType::TypeKind::TypeKind_Stream)
  475. {
  476. int streamId = (int)ReadSLEB128();
  477. if (streamId == -1)
  478. {
  479. beType = NULL;
  480. BE_MEM_END("BeType");
  481. return;
  482. }
  483. auto& result = mResults[streamId];
  484. BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Type);
  485. beType = result.mBeType;
  486. BE_MEM_END("BeType");
  487. return;
  488. }
  489. int typeId = (int)ReadSLEB128();
  490. auto& typeEntry = GetTypeEntry(typeId);
  491. if (typeKind == BfIRType::TypeKind::TypeKind_TypeId)
  492. beType = typeEntry.mBeType;
  493. else if (typeKind == BfIRType::TypeKind::TypeKind_TypeInstId)
  494. beType = typeEntry.mInstBeType;
  495. else if (typeKind == BfIRType::TypeKind::TypeKind_TypeInstPtrId)
  496. beType = mBeContext->GetPointerTo(typeEntry.mInstBeType);
  497. BE_MEM_END("BeType");
  498. }
  499. void BeIRCodeGen::Read(BeFunctionType*& beType)
  500. {
  501. BE_MEM_START;
  502. int streamId = (int)ReadSLEB128();
  503. auto& result = mResults[streamId];
  504. BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Type);
  505. beType = (BeFunctionType*)result.mBeType;
  506. BE_MEM_END("BeFunctionType");
  507. }
  508. void BeIRCodeGen::Read(BeValue*& beValue)
  509. {
  510. BE_MEM_START;
  511. BfIRParamType paramType = (BfIRParamType)mStream->Read();
  512. if (paramType == BfIRParamType_None)
  513. {
  514. beValue = NULL;
  515. BE_MEM_END("ParamType_None");
  516. }
  517. else if (paramType == BfIRParamType_Const)
  518. {
  519. BfTypeCode typeCode = (BfTypeCode)mStream->Read();
  520. BfConstType constType = (BfConstType)typeCode;
  521. if (constType == BfConstType_GlobalVar)
  522. {
  523. CMD_PARAM(int, streamId);
  524. if (streamId == -1)
  525. {
  526. int streamId = mCmdCount++;
  527. CMD_PARAM(BeType*, varType);
  528. CMD_PARAM(bool, isConstant);
  529. BfIRLinkageType linkageType = (BfIRLinkageType)mStream->Read();
  530. CMD_PARAM(BeConstant*, initializer);
  531. CMD_PARAM(String, name);
  532. CMD_PARAM(bool, isTLS);
  533. auto globalVariable = mBeModule->mGlobalVariables.Alloc();
  534. globalVariable->mModule = mBeModule;
  535. globalVariable->mType = varType;
  536. globalVariable->mIsConstant = isConstant;
  537. globalVariable->mLinkageType = linkageType;
  538. globalVariable->mInitializer = initializer;
  539. globalVariable->mName = name;
  540. globalVariable->mIsTLS = isTLS;
  541. globalVariable->mAlign = varType->mAlign;
  542. globalVariable->mUnnamedAddr = false;
  543. BF_ASSERT(varType->mAlign > 0);
  544. SetResult(streamId, globalVariable);
  545. beValue = globalVariable;
  546. }
  547. else
  548. beValue = GetBeValue(streamId);
  549. BE_MEM_END("ParamType_Const_GlobalVar");
  550. return;
  551. }
  552. else if ((constType == BfConstType_BitCast) || (constType == BfConstType_BitCastNull))
  553. {
  554. CMD_PARAM(BeConstant*, target);
  555. CMD_PARAM(BeType*, toType);
  556. auto castedVal = mBeModule->mAlloc.Alloc<BeCastConstant>();
  557. castedVal->mInt64 = target->mInt64;
  558. castedVal->mType = toType;
  559. castedVal->mTarget = target;
  560. BF_ASSERT(target->GetType() != NULL);
  561. BF_ASSERT(!target->GetType()->IsComposite());
  562. beValue = castedVal;
  563. BE_MEM_END("ParamType_Const_BitCast");
  564. return;
  565. }
  566. else if (constType == BfConstType_GEP32_2)
  567. {
  568. CMD_PARAM(BeConstant*, target);
  569. CMD_PARAM(int, idx0);
  570. CMD_PARAM(int, idx1);
  571. BF_ASSERT(target->GetType()->IsPointer());
  572. auto gepConstant = mBeModule->mAlloc.Alloc<BeGEPConstant>();
  573. gepConstant->mTarget = target;
  574. gepConstant->mIdx0 = idx0;
  575. gepConstant->mIdx1 = idx1;
  576. beValue = gepConstant;
  577. BE_MEM_END("ParamType_Const_GEP32_2");
  578. return;
  579. }
  580. else if (constType == BfConstType_PtrToInt)
  581. {
  582. CMD_PARAM(BeConstant*, target);
  583. BfTypeCode toTypeCode = (BfTypeCode)mStream->Read();
  584. BF_ASSERT(target->GetType()->IsPointer());
  585. bool isSigned = false;
  586. BeType* toType = GetBeType(toTypeCode, isSigned);
  587. auto castedVal = mBeModule->mAlloc.Alloc<BeCastConstant>();
  588. castedVal->mInt64 = target->mInt64;
  589. castedVal->mType = toType;
  590. castedVal->mTarget = target;
  591. BF_ASSERT(target->GetType() != NULL);
  592. beValue = castedVal;
  593. BE_MEM_END("ParamType_Const_PtrToInt");
  594. return;
  595. }
  596. else if (constType == BfConstType_AggZero)
  597. {
  598. CMD_PARAM(BeType*, type);
  599. auto beConst = mBeModule->mAlloc.Alloc<BeConstant>();
  600. beConst->mType = type;
  601. beValue = beConst;
  602. BE_MEM_END("ParamType_Const_AggZero");
  603. return;
  604. }
  605. else if (constType == BfConstType_Array)
  606. {
  607. CMD_PARAM(BeType*, type);
  608. CMD_PARAM(CmdParamVec<BeConstant*>, values);
  609. auto arrayType = (BeSizedArrayType*)type;
  610. int fillCount = (int)(arrayType->mLength - values.size());
  611. if (fillCount > 0)
  612. {
  613. auto lastValue = values.back();
  614. for (int i = 0; i < fillCount; i++)
  615. values.push_back(lastValue);
  616. }
  617. auto constStruct = mBeModule->mOwnedValues.Alloc<BeStructConstant>();
  618. constStruct->mType = type;
  619. for (auto val : values)
  620. constStruct->mMemberValues.push_back(BeValueDynCast<BeConstant>(val));
  621. beValue = constStruct;
  622. BE_MEM_END("ParamType_Const_Array");
  623. return;
  624. }
  625. bool isSigned = false;
  626. BeType* llvmConstType = GetBeType(typeCode, isSigned);
  627. if (typeCode == BfTypeCode_Single)
  628. {
  629. float f;
  630. mStream->Read(&f, sizeof(float));
  631. beValue = mBeModule->GetConstant(llvmConstType, f);
  632. BE_MEM_END("ParamType_Single");
  633. }
  634. else if (typeCode == BfTypeCode_Double)
  635. {
  636. double d;
  637. mStream->Read(&d, sizeof(double));
  638. beValue = mBeModule->GetConstant(llvmConstType, d);
  639. BE_MEM_END("ParamType_Const_Double");
  640. }
  641. else if (typeCode == BfTypeCode_Boolean)
  642. {
  643. CMD_PARAM(bool, boolVal);
  644. beValue = mBeModule->GetConstant(llvmConstType, boolVal);
  645. BE_MEM_END("ParamType_Const_Boolean");
  646. }
  647. else if (typeCode == BfTypeCode_None)
  648. {
  649. beValue = NULL;
  650. BE_MEM_END("ParamType_Const_None");
  651. }
  652. else if (typeCode == BfTypeCode_NullPtr)
  653. {
  654. CMD_PARAM(BeType*, nullType);
  655. beValue = mBeModule->GetConstantNull((BePointerType*)nullType);
  656. BE_MEM_END("ParamType_Const_NullPtr");
  657. }
  658. else if (BfIRBuilder::IsInt(typeCode))
  659. {
  660. int64 intVal = ReadSLEB128();
  661. auto constVal = mBeModule->GetConstant(llvmConstType, intVal);
  662. beValue = constVal;
  663. BE_MEM_END("ParamType_Const_Int");
  664. }
  665. else
  666. {
  667. BF_FATAL("Unhandled");
  668. }
  669. }
  670. else if (paramType == BfIRParamType_Arg)
  671. {
  672. int argIdx = mStream->Read();
  673. beValue = mBeModule->GetArgument(argIdx);
  674. BE_MEM_END("ParamType_Arg");
  675. }
  676. else if (paramType == BfIRParamType_StreamId_Abs8)
  677. {
  678. int cmdId = mStream->Read();
  679. auto& result = mResults[cmdId];
  680. BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Value);
  681. beValue = result.mBeValue;
  682. BE_MEM_END("ParamType_StreamId");
  683. }
  684. else if (paramType == BfIRParamType_StreamId_Rel)
  685. {
  686. int cmdId = mCmdCount - (int)ReadSLEB128();
  687. auto& result = mResults[cmdId];
  688. BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Value);
  689. beValue = result.mBeValue;
  690. BE_MEM_END("ParamType_StreamId");
  691. }
  692. else
  693. {
  694. int cmdId = mCmdCount - (paramType - BfIRParamType_StreamId_Back1) - 1;
  695. auto& result = mResults[cmdId];
  696. BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Value);
  697. beValue = result.mBeValue;
  698. BE_MEM_END("ParamType_StreamId");
  699. }
  700. }
  701. void BeIRCodeGen::Read(BeConstant*& llvmConstant)
  702. {
  703. BE_MEM_START;
  704. BeValue* value;
  705. Read(value);
  706. if (value == NULL)
  707. {
  708. llvmConstant = NULL;
  709. }
  710. else
  711. {
  712. BF_ASSERT(BeValueDynCast<BeConstant>(value));
  713. llvmConstant = (BeConstant*)value;
  714. }
  715. BE_MEM_END("BeConstant");
  716. }
  717. void BeIRCodeGen::Read(BeFunction*& beFunc)
  718. {
  719. BE_MEM_START;
  720. int streamId = (int)ReadSLEB128();
  721. if (streamId == -1)
  722. {
  723. beFunc = NULL;
  724. return;
  725. }
  726. auto& result = mResults[streamId];
  727. BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Value);
  728. BF_ASSERT(BeValueDynCast<BeFunction>(result.mBeValue));
  729. beFunc = (BeFunction*)result.mBeValue;
  730. BE_MEM_END("BeFunction");
  731. }
  732. void BeIRCodeGen::Read(BeBlock*& beBlock)
  733. {
  734. BE_MEM_START;
  735. int streamId = (int)ReadSLEB128();
  736. auto& result = mResults[streamId];
  737. BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Block);
  738. beBlock = (BeBlock*)result.mBeType;
  739. BE_MEM_END("BeBlock");
  740. }
  741. void BeIRCodeGen::Read(BeMDNode*& llvmMD)
  742. {
  743. BE_MEM_START;
  744. int streamId = (int)ReadSLEB128();
  745. if (streamId == -1)
  746. {
  747. llvmMD = NULL;
  748. return;
  749. }
  750. auto& result = mResults[streamId];
  751. BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Metadata);
  752. llvmMD = result.mBeMetadata;
  753. BE_MEM_END("BeMDNode");
  754. }
  755. void BeIRCodeGen::HandleNextCmd()
  756. {
  757. int curId = mCmdCount;
  758. BfIRCmd cmd = (BfIRCmd)mStream->Read();
  759. mCmdCount++;
  760. #ifdef CODEGEN_TRACK
  761. gBEMemReporter.BeginSection(gIRCmdNames[cmd]);
  762. gBEMemReporter.Add(1);
  763. #endif
  764. switch (cmd)
  765. {
  766. case BfIRCmd_Module_Start:
  767. {
  768. CMD_PARAM(String, moduleName);
  769. CMD_PARAM(int, ptrSize);
  770. CMD_PARAM(bool, isOptimized);
  771. BF_ASSERT(mBeModule == NULL);
  772. mPtrSize = ptrSize;
  773. mIsOptimized = isOptimized;
  774. mBeContext = new BeContext();
  775. mBeModule = new BeModule(moduleName, mBeContext);
  776. mBeModule->mBeIRCodeGen = this;
  777. for (auto constInt : mConfigConsts)
  778. {
  779. auto constVal = mBeModule->mAlloc.Alloc<BeConstant>();
  780. constVal->mType = mBeContext->GetPrimitiveType(BeTypeCode_Int32);
  781. constVal->mInt64 = constInt;
  782. mBeModule->mConfigConsts32.Add(constVal);
  783. constVal = mBeModule->mAlloc.Alloc<BeConstant>();
  784. constVal->mType = mBeContext->GetPrimitiveType(BeTypeCode_Int64);
  785. constVal->mInt64 = constInt;
  786. mBeModule->mConfigConsts64.Add(constVal);
  787. }
  788. }
  789. break;
  790. case BfIRCmd_Module_SetTargetTriple:
  791. {
  792. CMD_PARAM(String, targetTriple);
  793. mBeModule->mTargetTriple = targetTriple;
  794. }
  795. break;
  796. case BfIRCmd_Module_AddModuleFlag:
  797. {
  798. CMD_PARAM(String, flag);
  799. CMD_PARAM(int, val);
  800. //mBeModule->addModuleFlag(BeModule::Warning, flag, val);
  801. }
  802. break;
  803. case BfIRCmd_WriteIR:
  804. {
  805. /*CMD_PARAM(String, fileName);
  806. std::error_code ec;
  807. Beraw_fd_ostream outStream(fileName.c_str(), ec, Besys::fs::OpenFlags::F_Text);
  808. if (ec)
  809. {
  810. Fail("Failed writing IR '" + fileName + "': " + ec.message());
  811. }
  812. else
  813. mBeModule->print(outStream, NULL);*/
  814. }
  815. break;
  816. case BfIRCmd_SetType:
  817. {
  818. CMD_PARAM(int, typeId);
  819. CMD_PARAM(BeType*, type);
  820. GetTypeEntry(typeId).mBeType = type;
  821. }
  822. break;
  823. case BfIRCmd_SetInstType:
  824. {
  825. CMD_PARAM(int, typeId);
  826. CMD_PARAM(BeType*, type);
  827. GetTypeEntry(typeId).mInstBeType = type;
  828. }
  829. break;
  830. case BfIRCmd_PrimitiveType:
  831. {
  832. BfTypeCode typeCode = (BfTypeCode)mStream->Read();
  833. bool isSigned = false;
  834. SetResult(curId, GetBeType(typeCode, isSigned));
  835. }
  836. break;
  837. case BfIRCmd_CreateStruct:
  838. {
  839. CMD_PARAM(String, typeName);
  840. SetResult(curId, mBeContext->CreateStruct(typeName));
  841. }
  842. break;
  843. case BfIRCmd_StructSetBody:
  844. {
  845. CMD_PARAM(BeType*, type);
  846. CMD_PARAM(CmdParamVec<BeType*>, members);
  847. CMD_PARAM(bool, isPacked);
  848. BF_ASSERT(type->mTypeCode == BeTypeCode_Struct);
  849. auto structType = (BeStructType*)type;
  850. mBeContext->SetStructBody(structType, members, isPacked);
  851. }
  852. break;
  853. case BfIRCmd_Type:
  854. {
  855. CMD_PARAM(BeIRTypeEntry*, typeEntry);
  856. auto type = typeEntry->mBeType;
  857. SetResult(curId, type);
  858. }
  859. break;
  860. case BfIRCmd_TypeInst:
  861. {
  862. CMD_PARAM(BeIRTypeEntry*, typeEntry);
  863. SetResult(curId, typeEntry->mInstBeType);
  864. }
  865. break;
  866. case BfIRCmd_TypeInstPtr:
  867. {
  868. CMD_PARAM(BeIRTypeEntry*, typeEntry);
  869. SetResult(curId, mBeContext->GetPointerTo(typeEntry->mInstBeType));
  870. }
  871. break;
  872. case BfIRCmd_GetType:
  873. {
  874. CMD_PARAM(BeValue*, value);
  875. auto type = value->GetType();
  876. SetResult(curId, type);
  877. }
  878. break;
  879. case BfIRCmd_GetPointerToFuncType:
  880. {
  881. CMD_PARAM(BeFunctionType*, funcType);
  882. SetResult(curId, mBeContext->GetPointerTo(funcType));
  883. }
  884. break;
  885. case BfIRCmd_GetPointerToType:
  886. {
  887. CMD_PARAM(BeType*, type);
  888. SetResult(curId, mBeContext->GetPointerTo(type));
  889. }
  890. break;
  891. case BfIRCmd_GetSizedArrayType:
  892. {
  893. CMD_PARAM(BeType*, elementType);
  894. CMD_PARAM(int, length);
  895. SetResult(curId, mBeContext->CreateSizedArrayType(elementType, length));
  896. }
  897. break;
  898. case BfIRCmd_CreateConstStruct:
  899. {
  900. CMD_PARAM(BeType*, type);
  901. CMD_PARAM(CmdParamVec<BeValue*>, values)
  902. auto constStruct = mBeModule->mOwnedValues.Alloc<BeStructConstant>();
  903. constStruct->mType = type;
  904. BF_ASSERT(type->IsStruct());
  905. BF_ASSERT(((BeStructType*)type)->mMembers.size() == values.size());
  906. for (int i = 0; i < (int)values.size(); i++)
  907. {
  908. auto val = values[i];
  909. BF_ASSERT(((BeStructType*)type)->mMembers[i].mType == val->GetType());
  910. constStruct->mMemberValues.push_back(BeValueDynCast<BeConstant>(val));
  911. }
  912. SetResult(curId, constStruct);
  913. }
  914. break;
  915. case BfIRCmd_CreateConstStructZero:
  916. {
  917. CMD_PARAM(BeType*, type);
  918. auto beConst = mBeModule->mAlloc.Alloc<BeConstant>();
  919. beConst->mType = type;
  920. SetResult(curId, beConst);
  921. }
  922. break;
  923. case BfIRCmd_CreateConstArray:
  924. {
  925. CMD_PARAM(BeType*, type);
  926. CMD_PARAM(CmdParamVec<BeConstant*>, values);
  927. auto constStruct = mBeModule->mOwnedValues.Alloc<BeStructConstant>();
  928. constStruct->mType = type;
  929. for (auto val : values)
  930. constStruct->mMemberValues.push_back(BeValueDynCast<BeConstant>(val));
  931. SetResult(curId, constStruct);
  932. }
  933. break;
  934. case BfIRCmd_CreateConstString:
  935. {
  936. CMD_PARAM(String, str);
  937. auto constStruct = mBeModule->mOwnedValues.Alloc<BeStringConstant>();
  938. constStruct->mString = str;
  939. auto charType = mBeContext->GetPrimitiveType(BeTypeCode_Int8);
  940. constStruct->mType = mBeContext->CreateSizedArrayType(charType, str.length() + 1);
  941. SetResult(curId, constStruct);
  942. }
  943. break;
  944. case BfIRCmd_ConfigConst:
  945. {
  946. CMD_PARAM(int, constIdx);
  947. BfTypeCode typeCode = (BfTypeCode)mStream->Read();
  948. if (typeCode == BfTypeCode_IntPtr)
  949. typeCode = (mPtrSize == 4) ? BfTypeCode_Int32 : BfTypeCode_Int64;
  950. BeConstant* constVal = (typeCode == BfTypeCode_Int32) ?
  951. mBeModule->mConfigConsts32[constIdx] :
  952. mBeModule->mConfigConsts64[constIdx];
  953. SetResult(curId, constVal);
  954. }
  955. break;
  956. case BfIRCmd_SetName:
  957. {
  958. CMD_PARAM(BeValue*, val);
  959. CMD_PARAM(String, name);
  960. val->SetName(name);
  961. }
  962. break;
  963. case BfIRCmd_CreateUndefValue:
  964. {
  965. CMD_PARAM(BeType*, type);
  966. SetResult(curId, mBeModule->CreateUndefValue(type));
  967. }
  968. break;
  969. case BfIRCmd_NumericCast:
  970. {
  971. CMD_PARAM(BeValue*, val);
  972. CMD_PARAM(bool, valIsSigned);
  973. BfTypeCode typeCode = (BfTypeCode)mStream->Read();
  974. BfTypeCode valTypeCode = GetTypeCode(val->GetType(), valIsSigned);
  975. bool toSigned = false;
  976. auto toBeType = GetBeType(typeCode, toSigned);
  977. BeValue* retVal = mBeModule->CreateNumericCast(val, toBeType, valIsSigned, toSigned);
  978. SetResult(curId, retVal);
  979. }
  980. break;
  981. case BfIRCmd_CmpEQ:
  982. {
  983. CMD_PARAM(BeValue*, lhs);
  984. CMD_PARAM(BeValue*, rhs);
  985. SetResult(curId, mBeModule->CreateCmp(BeCmpKind_EQ, lhs, rhs));
  986. }
  987. break;
  988. case BfIRCmd_CmpNE:
  989. {
  990. CMD_PARAM(BeValue*, lhs);
  991. CMD_PARAM(BeValue*, rhs);
  992. SetResult(curId, mBeModule->CreateCmp(BeCmpKind_NE, lhs, rhs));
  993. }
  994. break;
  995. case BfIRCmd_CmpSLT:
  996. {
  997. CMD_PARAM(BeValue*, lhs);
  998. CMD_PARAM(BeValue*, rhs);
  999. SetResult(curId, mBeModule->CreateCmp(BeCmpKind_SLT, lhs, rhs));
  1000. }
  1001. break;
  1002. case BfIRCmd_CmpULT:
  1003. {
  1004. CMD_PARAM(BeValue*, lhs);
  1005. CMD_PARAM(BeValue*, rhs);
  1006. SetResult(curId, mBeModule->CreateCmp(BeCmpKind_ULT, lhs, rhs));
  1007. }
  1008. break;
  1009. case BfIRCmd_CmpSLE:
  1010. {
  1011. CMD_PARAM(BeValue*, lhs);
  1012. CMD_PARAM(BeValue*, rhs);
  1013. SetResult(curId, mBeModule->CreateCmp(BeCmpKind_SLE, lhs, rhs));
  1014. }
  1015. break;
  1016. case BfIRCmd_CmpULE:
  1017. {
  1018. CMD_PARAM(BeValue*, lhs);
  1019. CMD_PARAM(BeValue*, rhs);
  1020. SetResult(curId, mBeModule->CreateCmp(BeCmpKind_ULE, lhs, rhs));
  1021. }
  1022. break;
  1023. case BfIRCmd_CmpSGT:
  1024. {
  1025. CMD_PARAM(BeValue*, lhs);
  1026. CMD_PARAM(BeValue*, rhs);
  1027. SetResult(curId, mBeModule->CreateCmp(BeCmpKind_SGT, lhs, rhs));
  1028. }
  1029. break;
  1030. case BfIRCmd_CmpUGT:
  1031. {
  1032. CMD_PARAM(BeValue*, lhs);
  1033. CMD_PARAM(BeValue*, rhs);
  1034. SetResult(curId, mBeModule->CreateCmp(BeCmpKind_UGT, lhs, rhs));
  1035. }
  1036. break;
  1037. case BfIRCmd_CmpSGE:
  1038. {
  1039. CMD_PARAM(BeValue*, lhs);
  1040. CMD_PARAM(BeValue*, rhs);
  1041. SetResult(curId, mBeModule->CreateCmp(BeCmpKind_SGE, lhs, rhs));
  1042. }
  1043. break;
  1044. case BfIRCmd_CmpUGE:
  1045. {
  1046. CMD_PARAM(BeValue*, lhs);
  1047. CMD_PARAM(BeValue*, rhs);
  1048. SetResult(curId, mBeModule->CreateCmp(BeCmpKind_UGE, lhs, rhs));
  1049. }
  1050. break;
  1051. case BfIRCmd_Add:
  1052. {
  1053. CMD_PARAM(BeValue*, lhs);
  1054. CMD_PARAM(BeValue*, rhs);
  1055. SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_Add, lhs, rhs));
  1056. }
  1057. break;
  1058. case BfIRCmd_Sub:
  1059. {
  1060. CMD_PARAM(BeValue*, lhs);
  1061. CMD_PARAM(BeValue*, rhs);
  1062. SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_Subtract, lhs, rhs));
  1063. }
  1064. break;
  1065. case BfIRCmd_Mul:
  1066. {
  1067. CMD_PARAM(BeValue*, lhs);
  1068. CMD_PARAM(BeValue*, rhs);
  1069. SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_Multiply, lhs, rhs));
  1070. }
  1071. break;
  1072. case BfIRCmd_SDiv:
  1073. {
  1074. CMD_PARAM(BeValue*, lhs);
  1075. CMD_PARAM(BeValue*, rhs);
  1076. SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_SDivide, lhs, rhs));
  1077. }
  1078. break;
  1079. case BfIRCmd_UDiv:
  1080. {
  1081. CMD_PARAM(BeValue*, lhs);
  1082. CMD_PARAM(BeValue*, rhs);
  1083. SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_UDivide, lhs, rhs));
  1084. }
  1085. break;
  1086. case BfIRCmd_SRem:
  1087. {
  1088. CMD_PARAM(BeValue*, lhs);
  1089. CMD_PARAM(BeValue*, rhs);
  1090. SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_SModulus, lhs, rhs));
  1091. }
  1092. break;
  1093. case BfIRCmd_URem:
  1094. {
  1095. CMD_PARAM(BeValue*, lhs);
  1096. CMD_PARAM(BeValue*, rhs);
  1097. SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_UModulus, lhs, rhs));
  1098. }
  1099. break;
  1100. case BfIRCmd_And:
  1101. {
  1102. CMD_PARAM(BeValue*, lhs);
  1103. CMD_PARAM(BeValue*, rhs);
  1104. SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_BitwiseAnd, lhs, rhs));
  1105. }
  1106. break;
  1107. case BfIRCmd_Or:
  1108. {
  1109. CMD_PARAM(BeValue*, lhs);
  1110. CMD_PARAM(BeValue*, rhs);
  1111. SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_BitwiseOr, lhs, rhs));
  1112. }
  1113. break;
  1114. case BfIRCmd_Xor:
  1115. {
  1116. CMD_PARAM(BeValue*, lhs);
  1117. CMD_PARAM(BeValue*, rhs);
  1118. SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_ExclusiveOr, lhs, rhs));
  1119. }
  1120. break;
  1121. case BfIRCmd_Shl:
  1122. {
  1123. CMD_PARAM(BeValue*, lhs);
  1124. CMD_PARAM(BeValue*, rhs);
  1125. SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_LeftShift, lhs, rhs));
  1126. }
  1127. break;
  1128. case BfIRCmd_AShr:
  1129. {
  1130. CMD_PARAM(BeValue*, lhs);
  1131. CMD_PARAM(BeValue*, rhs);
  1132. SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_ARightShift, lhs, rhs));
  1133. }
  1134. break;
  1135. case BfIRCmd_LShr:
  1136. {
  1137. CMD_PARAM(BeValue*, lhs);
  1138. CMD_PARAM(BeValue*, rhs);
  1139. SetResult(curId, mBeModule->CreateBinaryOp(BeBinaryOpKind_RightShift, lhs, rhs));
  1140. }
  1141. break;
  1142. case BfIRCmd_Neg:
  1143. {
  1144. CMD_PARAM(BeValue*, val);
  1145. auto negInst = mBeModule->AllocInst<BeNegInst>();
  1146. negInst->mValue = val;
  1147. SetResult(curId, negInst);
  1148. }
  1149. break;
  1150. case BfIRCmd_Not:
  1151. {
  1152. CMD_PARAM(BeValue*, val);
  1153. auto negInst = mBeModule->AllocInst<BeNotInst>();
  1154. negInst->mValue = val;
  1155. SetResult(curId, negInst);
  1156. }
  1157. break;
  1158. case BfIRCmd_BitCast:
  1159. {
  1160. CMD_PARAM(BeValue*, val);
  1161. CMD_PARAM(BeType*, toType);
  1162. if (auto funcVal = BeValueDynCast<BeFunction>(val))
  1163. {
  1164. auto beConst = mBeModule->mAlloc.Alloc<BeConstant>();
  1165. beConst->mTarget = funcVal;
  1166. BF_ASSERT(funcVal->mType != NULL);
  1167. beConst->mType = toType;
  1168. SetResult(curId, beConst);
  1169. break;
  1170. }
  1171. SetResult(curId, mBeModule->CreateBitCast(val, toType));
  1172. }
  1173. break;
  1174. case BfIRCmd_PtrToInt:
  1175. {
  1176. CMD_PARAM(BeValue*, val);
  1177. auto typeCode = (BfTypeCode)mStream->Read();
  1178. bool isSigned = false;
  1179. auto beType = GetBeType(typeCode, isSigned);
  1180. BF_ASSERT(beType != NULL);
  1181. auto numericCastInst = mBeModule->AllocInst<BeNumericCastInst>();
  1182. numericCastInst->mValue = val;
  1183. numericCastInst->mValSigned = false;
  1184. numericCastInst->mToType = beType;
  1185. numericCastInst->mToSigned = isSigned;
  1186. SetResult(curId, numericCastInst);
  1187. }
  1188. break;
  1189. case BfIRCmd_IntToPtr:
  1190. {
  1191. CMD_PARAM(BeValue*, val);
  1192. CMD_PARAM(BeType*, toType);
  1193. auto bitcastInst = mBeModule->AllocInst<BeBitCastInst>();
  1194. bitcastInst->mValue = val;
  1195. bitcastInst->mToType = toType;
  1196. SetResult(curId, bitcastInst);
  1197. }
  1198. break;
  1199. case BfIRCmd_InboundsGEP1_32:
  1200. {
  1201. CMD_PARAM(BeValue*, val);
  1202. CMD_PARAM(int, idx0);
  1203. BF_ASSERT(val->GetType()->IsPointer());
  1204. BeType* int32Type = mBeContext->GetPrimitiveType(BeTypeCode_Int32);
  1205. SetResult(curId, mBeModule->CreateGEP(val, mBeModule->GetConstant(int32Type, (int64)idx0), NULL));
  1206. }
  1207. break;
  1208. case BfIRCmd_InboundsGEP2_32:
  1209. {
  1210. CMD_PARAM(BeValue*, val);
  1211. CMD_PARAM(int, idx0);
  1212. CMD_PARAM(int, idx1);
  1213. BF_ASSERT(val->GetType()->IsPointer());
  1214. BeType* int32Type = mBeContext->GetPrimitiveType(BeTypeCode_Int32);
  1215. SetResult(curId, mBeModule->CreateGEP(val, mBeModule->GetConstant(int32Type, (int64)idx0), mBeModule->GetConstant(int32Type, (int64)idx1)));
  1216. }
  1217. break;
  1218. case BfIRCmd_InBoundsGEP1:
  1219. {
  1220. CMD_PARAM(BeValue*, val);
  1221. CMD_PARAM(BeValue*, idx0);
  1222. BF_ASSERT(val->GetType()->IsPointer());
  1223. SetResult(curId, mBeModule->CreateGEP(val, idx0, NULL));
  1224. }
  1225. break;
  1226. case BfIRCmd_InBoundsGEP2:
  1227. {
  1228. CMD_PARAM(BeValue*, val);
  1229. CMD_PARAM(BeValue*, idx0);
  1230. CMD_PARAM(BeValue*, idx1);
  1231. BF_ASSERT(val->GetType()->IsPointer());
  1232. SetResult(curId, mBeModule->CreateGEP(val, idx0, idx1));
  1233. }
  1234. break;
  1235. case BfIRCmd_IsNull:
  1236. {
  1237. CMD_PARAM(BeValue*, val);
  1238. BF_ASSERT(val->GetType()->IsPointer());
  1239. SetResult(curId, mBeModule->CreateCmp(BeCmpKind_EQ, val, mBeModule->GetConstantNull((BePointerType*)val->GetType())));
  1240. }
  1241. break;
  1242. case BfIRCmd_IsNotNull:
  1243. {
  1244. CMD_PARAM(BeValue*, val);
  1245. BF_ASSERT(val->GetType()->IsPointer());
  1246. SetResult(curId, mBeModule->CreateCmp(BeCmpKind_NE, val, mBeModule->GetConstantNull((BePointerType*)val->GetType())));
  1247. }
  1248. break;
  1249. case BfIRCmd_ExtractValue:
  1250. {
  1251. CMD_PARAM(BeValue*, val);
  1252. CMD_PARAM(int, idx);
  1253. auto extractValueInst = mBeModule->AllocInst<BeExtractValueInst>();
  1254. extractValueInst->mAggVal = val;
  1255. extractValueInst->mIdx = idx;
  1256. BF_ASSERT(val->GetType()->IsComposite());
  1257. SetResult(curId, extractValueInst);
  1258. }
  1259. break;
  1260. case BfIRCmd_InsertValue:
  1261. {
  1262. CMD_PARAM(BeValue*, agg);
  1263. CMD_PARAM(BeValue*, val);
  1264. CMD_PARAM(int, idx);
  1265. auto insertValueInst = mBeModule->AllocInst<BeInsertValueInst>();
  1266. insertValueInst->mAggVal = agg;
  1267. insertValueInst->mMemberVal = val;
  1268. insertValueInst->mIdx = idx;
  1269. SetResult(curId, insertValueInst);
  1270. }
  1271. break;
  1272. case BfIRCmd_Alloca:
  1273. {
  1274. CMD_PARAM(BeType*, type);
  1275. if (type->IsStruct())
  1276. {
  1277. BF_ASSERT(!((BeStructType*)type)->mIsOpaque);
  1278. }
  1279. SetResult(curId, mBeModule->CreateAlloca(type));
  1280. }
  1281. break;
  1282. case BfIRCmd_AllocaArray:
  1283. {
  1284. CMD_PARAM(BeType*, type);
  1285. CMD_PARAM(BeValue*, arraySize);
  1286. auto allocaInst = mBeModule->AllocInst<BeAllocaInst>();
  1287. allocaInst->mType = type;
  1288. allocaInst->mArraySize = arraySize;
  1289. SetResult(curId, allocaInst);
  1290. }
  1291. break;
  1292. case BfIRCmd_SetAllocaAlignment:
  1293. {
  1294. CMD_PARAM(BeValue*, val);
  1295. CMD_PARAM(int, alignment);
  1296. auto inst = BeValueDynCast<BeAllocaInst>(val);
  1297. //TODO: Implement
  1298. /*inst->setAlignment(alignment);*/
  1299. }
  1300. break;
  1301. case BfIRCmd_AliasValue:
  1302. {
  1303. CMD_PARAM(BeValue*, val);
  1304. auto inst = mBeModule->AllocInst<BeAliasValueInst>();
  1305. inst->mPtr = val;
  1306. SetResult(curId, inst);
  1307. }
  1308. break;
  1309. case BfIRCmd_LifetimeStart:
  1310. {
  1311. CMD_PARAM(BeValue*, val);
  1312. auto inst = mBeModule->AllocInst<BeLifetimeStartInst>();
  1313. inst->mPtr = val;
  1314. SetResult(curId, inst);
  1315. }
  1316. break;
  1317. case BfIRCmd_LifetimeEnd:
  1318. {
  1319. CMD_PARAM(BeValue*, val);
  1320. auto inst = mBeModule->AllocInst<BeLifetimeEndInst>();
  1321. inst->mPtr = val;
  1322. SetResult(curId, inst);
  1323. }
  1324. break;
  1325. case BfIRCmd_LifetimeExtend:
  1326. {
  1327. CMD_PARAM(BeValue*, val);
  1328. auto inst = mBeModule->AllocInst<BeLifetimeExtendInst>();
  1329. inst->mPtr = val;
  1330. SetResult(curId, inst);
  1331. }
  1332. break;
  1333. case BfIRCmd_ValueScopeStart:
  1334. {
  1335. auto inst = mBeModule->AllocInst<BeValueScopeStartInst>();
  1336. SetResult(curId, inst);
  1337. }
  1338. break;
  1339. case BfIRCmd_ValueScopeRetain:
  1340. {
  1341. CMD_PARAM(BeValue*, val);
  1342. auto inst = mBeModule->AllocInst<BeValueScopeRetainInst>();
  1343. inst->mValue = val;
  1344. }
  1345. break;
  1346. case BfIRCmd_ValueScopeSoftEnd:
  1347. {
  1348. CMD_PARAM(BeValue*, val);
  1349. auto inst = mBeModule->AllocInst<BeValueScopeEndInst>();
  1350. inst->mScopeStart = (BeValueScopeStartInst*)val;
  1351. inst->mIsSoft = true;
  1352. //TODO: Is this always correct? This keeps there from being nops inserted on block opens ( { )
  1353. inst->mDbgLoc = NULL;
  1354. }
  1355. break;
  1356. case BfIRCmd_ValueScopeHardEnd:
  1357. {
  1358. CMD_PARAM(BeValue*, val);
  1359. auto inst = mBeModule->AllocInst<BeValueScopeEndInst>();
  1360. inst->mScopeStart = (BeValueScopeStartInst*)val;
  1361. inst->mIsSoft = false;
  1362. //TODO: Is this always correct? This keeps there from being nops inserted on block opens ( { )
  1363. inst->mDbgLoc = NULL;
  1364. }
  1365. break;
  1366. case BfIRCmd_SetAllocaNoChkStkHint:
  1367. {
  1368. CMD_PARAM(BeValue*, val);
  1369. auto inst = BeValueDynCast<BeAllocaInst>(val);
  1370. inst->mNoChkStk = true;
  1371. }
  1372. break;
  1373. case BfIRCmd_SetAllocaForceMem:
  1374. {
  1375. CMD_PARAM(BeValue*, val);
  1376. auto inst = BeValueDynCast<BeAllocaInst>(val);
  1377. inst->mForceMem = true;
  1378. }
  1379. break;
  1380. case BfIRCmd_Load:
  1381. {
  1382. CMD_PARAM(BeValue*, val);
  1383. #ifdef _DEBUG
  1384. auto ptrType = val->GetType();
  1385. BF_ASSERT(ptrType->IsPointer());
  1386. // We call via a function pointer so there's never a reason to allow loading of a funcPtr
  1387. BF_ASSERT(((BePointerType*)ptrType)->mElementType->mTypeCode != BeTypeCode_Function);
  1388. #endif
  1389. CMD_PARAM(bool, isVolatile);
  1390. SetResult(curId, mBeModule->CreateLoad(val, isVolatile));
  1391. }
  1392. break;
  1393. case BfIRCmd_AlignedLoad:
  1394. {
  1395. CMD_PARAM(BeValue*, val);
  1396. CMD_PARAM(int, alignment);
  1397. CMD_PARAM(bool, isVolatile);
  1398. #ifdef _DEBUG
  1399. auto ptrType = val->GetType();
  1400. BF_ASSERT(ptrType->IsPointer());
  1401. #endif
  1402. SetResult(curId, mBeModule->CreateAlignedLoad(val, alignment, isVolatile));
  1403. }
  1404. break;
  1405. case BfIRCmd_Store:
  1406. {
  1407. CMD_PARAM(BeValue*, val);
  1408. CMD_PARAM(BeValue*, ptr);
  1409. #ifdef _DEBUG
  1410. auto ptrType = ptr->GetType();
  1411. auto valType = val->GetType();
  1412. BF_ASSERT(ptrType->IsPointer());
  1413. BF_ASSERT(mBeContext->AreTypesEqual(((BePointerType*)ptrType)->mElementType, valType));
  1414. #endif
  1415. CMD_PARAM(bool, isVolatile);
  1416. SetResult(curId, mBeModule->CreateStore(val, ptr, isVolatile));
  1417. }
  1418. break;
  1419. case BfIRCmd_AlignedStore:
  1420. {
  1421. CMD_PARAM(BeValue*, val);
  1422. CMD_PARAM(BeValue*, ptr);
  1423. CMD_PARAM(int, alignment);
  1424. CMD_PARAM(bool, isVolatile);
  1425. #ifdef _DEBUG
  1426. auto ptrType = ptr->GetType();
  1427. auto valType = val->GetType();
  1428. BF_ASSERT(ptrType->IsPointer());
  1429. BF_ASSERT(mBeContext->AreTypesEqual(((BePointerType*)ptrType)->mElementType, valType));
  1430. #endif
  1431. SetResult(curId, mBeModule->CreateAlignedStore(val, ptr, alignment, isVolatile));
  1432. }
  1433. break;
  1434. case BfIRCmd_MemSet:
  1435. {
  1436. auto inst = mBeModule->AllocInst<BeMemSetInst>();
  1437. Read(inst->mAddr);
  1438. Read(inst->mVal);
  1439. Read(inst->mSize);
  1440. Read(inst->mAlignment);
  1441. SetResult(curId, inst);
  1442. }
  1443. break;
  1444. case BfIRCmd_Fence:
  1445. {
  1446. BfIRFenceType fenceType = (BfIRFenceType)mStream->Read();
  1447. mBeModule->AllocInst<BeFenceInst>();
  1448. }
  1449. break;
  1450. case BfIRCmd_StackSave:
  1451. {
  1452. SetResult(curId, mBeModule->AllocInst<BeStackSaveInst>());
  1453. }
  1454. break;
  1455. case BfIRCmd_StackRestore:
  1456. {
  1457. CMD_PARAM(BeValue*, stackVal);
  1458. auto stackRestoreInst = mBeModule->AllocInst<BeStackRestoreInst>();
  1459. stackRestoreInst->mStackVal = stackVal;
  1460. SetResult(curId, stackRestoreInst);
  1461. }
  1462. break;
  1463. case BfIRCmd_GlobalVariable:
  1464. {
  1465. CMD_PARAM(BeType*, varType);
  1466. CMD_PARAM(bool, isConstant);
  1467. BfIRLinkageType linkageType = (BfIRLinkageType)mStream->Read();
  1468. CMD_PARAM(BeConstant*, initializer);
  1469. CMD_PARAM(String, name);
  1470. CMD_PARAM(bool, isTLS);
  1471. auto globalVariable = mBeModule->mGlobalVariables.Alloc();
  1472. globalVariable->mModule = mBeModule;
  1473. globalVariable->mType = varType;
  1474. globalVariable->mIsConstant = isConstant;
  1475. globalVariable->mLinkageType = linkageType;
  1476. globalVariable->mInitializer = initializer;
  1477. globalVariable->mName = name;
  1478. globalVariable->mIsTLS = isTLS;
  1479. globalVariable->mUnnamedAddr = false;
  1480. if (initializer != NULL)
  1481. {
  1482. globalVariable->mAlign = varType->mAlign;
  1483. BF_ASSERT(varType->mAlign > 0);
  1484. }
  1485. else
  1486. globalVariable->mAlign = -1;
  1487. SetResult(curId, globalVariable);
  1488. }
  1489. break;
  1490. case BfIRCmd_GlobalVar_SetUnnamedAddr:
  1491. {
  1492. CMD_PARAM(BeValue*, val);
  1493. CMD_PARAM(bool, unnamedAddr);
  1494. ((BeGlobalVariable*)val)->mUnnamedAddr = true;
  1495. }
  1496. break;
  1497. case BfIRCmd_GlobalVar_SetInitializer:
  1498. {
  1499. CMD_PARAM(BeValue*, val);
  1500. CMD_PARAM(BeConstant*, initializer);
  1501. auto globalVariable = (BeGlobalVariable*)val;
  1502. globalVariable->mInitializer = initializer;
  1503. if (globalVariable->mInitializer != NULL)
  1504. {
  1505. globalVariable->mAlign = globalVariable->mType->mAlign;
  1506. BF_ASSERT(globalVariable->mAlign != -1);
  1507. }
  1508. }
  1509. break;
  1510. case BfIRCmd_GlobalVar_SetAlignment:
  1511. {
  1512. CMD_PARAM(BeValue*, val);
  1513. CMD_PARAM(int, alignment);
  1514. auto globalVariable = (BeGlobalVariable*)val;
  1515. globalVariable->mAlign = alignment;
  1516. BF_ASSERT(alignment > 0);
  1517. if (globalVariable->mInitializer != NULL)
  1518. {
  1519. BF_ASSERT(globalVariable->mAlign != -1);
  1520. }
  1521. }
  1522. break;
  1523. case BfIRCmd_GlobalStringPtr:
  1524. {
  1525. CMD_PARAM(String, str);
  1526. auto constStruct = mBeModule->mOwnedValues.Alloc<BeStringConstant>();
  1527. constStruct->mString = str;
  1528. auto charType = mBeContext->GetPrimitiveType(BeTypeCode_Int8);
  1529. constStruct->mType = mBeContext->CreateSizedArrayType(charType, str.length() + 1);
  1530. auto globalVariable = mBeModule->mGlobalVariables.Alloc();
  1531. globalVariable->mModule = mBeModule;
  1532. globalVariable->mType = constStruct->mType;
  1533. globalVariable->mIsConstant = true;
  1534. globalVariable->mLinkageType = BfIRLinkageType_Internal;
  1535. globalVariable->mInitializer = constStruct;
  1536. globalVariable->mName = StrFormat("__str%d", (int)mBeModule->mGlobalVariables.size() - 1);
  1537. globalVariable->mIsTLS = false;
  1538. globalVariable->mAlign = 1;
  1539. globalVariable->mUnnamedAddr = false;
  1540. auto castedVal = mBeModule->mAlloc.Alloc<BeCastConstant>();
  1541. castedVal->mType = mBeContext->GetPointerTo(charType);
  1542. castedVal->mTarget = globalVariable;
  1543. SetResult(curId, castedVal);
  1544. //SetResult(curId, globalVariable);
  1545. }
  1546. break;
  1547. case BfIRCmd_CreateBlock:
  1548. {
  1549. CMD_PARAM(String, name);
  1550. CMD_PARAM(bool, addNow);
  1551. auto block = mBeModule->CreateBlock(name);
  1552. if (addNow)
  1553. mBeModule->AddBlock(mActiveFunction, block);
  1554. SetResult(curId, block);
  1555. }
  1556. break;
  1557. case BfIRCmd_MaybeChainNewBlock:
  1558. {
  1559. CMD_PARAM(String, name);
  1560. auto newBlock = mBeModule->GetInsertBlock();
  1561. if (!newBlock->IsEmpty())
  1562. {
  1563. auto bb = mBeModule->CreateBlock(name);
  1564. mBeModule->CreateBr(bb);
  1565. mBeModule->AddBlock(mActiveFunction, bb);
  1566. mBeModule->SetInsertPoint(bb);
  1567. newBlock = bb;
  1568. }
  1569. SetResult(curId, newBlock);
  1570. }
  1571. break;
  1572. case BfIRCmd_AddBlock:
  1573. {
  1574. CMD_PARAM(BeBlock*, block);
  1575. mBeModule->AddBlock(mActiveFunction, block);
  1576. }
  1577. break;
  1578. case BfIRCmd_DropBlocks:
  1579. {
  1580. CMD_PARAM(BeBlock*, startingBlock);
  1581. auto& basicBlockList = mActiveFunction->mBlocks;
  1582. int postExitBlockIdx = -1;
  1583. /*auto itr = basicBlockList.begin();
  1584. while (itr != basicBlockList.end())
  1585. {
  1586. auto block = *itr;
  1587. if (block == startingBlock)
  1588. {
  1589. basicBlockList.erase(itr, basicBlockList.end());
  1590. break;
  1591. }
  1592. ++itr;
  1593. }*/
  1594. for (int i = 0; i < (int)basicBlockList.size(); i++)
  1595. {
  1596. if (basicBlockList[i] == startingBlock)
  1597. {
  1598. basicBlockList.RemoveRange(i, basicBlockList.size() - i);
  1599. break;
  1600. }
  1601. }
  1602. }
  1603. break;
  1604. case BfIRCmd_MergeBlockDown:
  1605. {
  1606. CMD_PARAM(BeBlock*, fromBlock);
  1607. CMD_PARAM(BeBlock*, intoBlock);
  1608. for (auto inst : fromBlock->mInstructions)
  1609. inst->mParentBlock = intoBlock;
  1610. if (!fromBlock->mInstructions.IsEmpty())
  1611. intoBlock->mInstructions.Insert(0, &fromBlock->mInstructions[0], fromBlock->mInstructions.size());
  1612. mBeModule->RemoveBlock(mActiveFunction, fromBlock);
  1613. }
  1614. break;
  1615. case BfIRCmd_SetInsertPoint:
  1616. {
  1617. CMD_PARAM(BeBlock*, block);
  1618. mBeModule->SetInsertPoint(block);
  1619. }
  1620. break;
  1621. case BfIRCmd_SetInsertPointAtStart:
  1622. {
  1623. CMD_PARAM(BeBlock*, block);
  1624. mBeModule->SetInsertPointAtStart(block);
  1625. }
  1626. break;
  1627. case BfIRCmd_EraseFromParent:
  1628. {
  1629. CMD_PARAM(BeBlock*, block);
  1630. mBeModule->RemoveBlock(mActiveFunction, block);
  1631. }
  1632. break;
  1633. case BfIRCmd_DeleteBlock:
  1634. {
  1635. CMD_PARAM(BeBlock*, block);
  1636. }
  1637. break;
  1638. case BfIRCmd_EraseInstFromParent:
  1639. {
  1640. CMD_PARAM(BeValue*, instVal);
  1641. BeInst* inst = (BeInst*)instVal;
  1642. auto itr = std::find(inst->mParentBlock->mInstructions.begin(), inst->mParentBlock->mInstructions.end(), inst);
  1643. inst->mParentBlock->mInstructions.erase(itr);
  1644. }
  1645. break;
  1646. case BfIRCmd_CreateBr:
  1647. {
  1648. CMD_PARAM(BeBlock*, block);
  1649. mBeModule->CreateBr(block);
  1650. }
  1651. break;
  1652. case BfIRCmd_CreateBr_Fake:
  1653. {
  1654. CMD_PARAM(BeBlock*, block);
  1655. auto inst = mBeModule->CreateBr(block);
  1656. inst->mIsFake = true;
  1657. }
  1658. break;
  1659. case BfIRCmd_CreateBr_NoCollapse:
  1660. {
  1661. CMD_PARAM(BeBlock*, block);
  1662. auto inst = mBeModule->CreateBr(block);
  1663. inst->mNoCollapse = true;
  1664. }
  1665. break;
  1666. case BfIRCmd_CreateCondBr:
  1667. {
  1668. CMD_PARAM(BeValue*, condVal);
  1669. CMD_PARAM(BeBlock*, trueBlock);
  1670. CMD_PARAM(BeBlock*, falseBlock);
  1671. mBeModule->CreateCondBr(condVal, trueBlock, falseBlock);
  1672. }
  1673. break;
  1674. case BfIRCmd_MoveBlockToEnd:
  1675. {
  1676. CMD_PARAM(BeBlock*, block);
  1677. mBeModule->RemoveBlock(mActiveFunction, block);
  1678. mBeModule->AddBlock(mActiveFunction, block);
  1679. }
  1680. break;
  1681. case BfIRCmd_CreateSwitch:
  1682. {
  1683. CMD_PARAM(BeValue*, val);
  1684. CMD_PARAM(BeBlock*, dest);
  1685. CMD_PARAM(int, numCases);
  1686. auto switchInst = mBeModule->AllocInstOwned<BeSwitchInst>();
  1687. switchInst->mValue = val;
  1688. switchInst->mDefaultBlock = dest;
  1689. switchInst->mCases.Reserve(numCases);
  1690. SetResult(curId, switchInst);
  1691. }
  1692. break;
  1693. case BfIRCmd_AddSwitchCase:
  1694. {
  1695. CMD_PARAM(BeValue*, switchVal);
  1696. CMD_PARAM(BeValue*, caseVal);
  1697. CMD_PARAM(BeBlock*, caseBlock);
  1698. BeSwitchCase switchCase;
  1699. switchCase.mValue = (BeConstant*)caseVal;
  1700. switchCase.mBlock = caseBlock;
  1701. ((BeSwitchInst*)switchVal)->mCases.push_back(switchCase);
  1702. }
  1703. break;
  1704. case BfIRCmd_SetSwitchDefaultDest:
  1705. {
  1706. CMD_PARAM(BeValue*, switchVal);
  1707. CMD_PARAM(BeBlock*, caseBlock);
  1708. ((BeSwitchInst*)switchVal)->mDefaultBlock = caseBlock;
  1709. }
  1710. break;
  1711. case BfIRCmd_CreatePhi:
  1712. {
  1713. CMD_PARAM(BeType*, type);
  1714. CMD_PARAM(int, incomingCount);
  1715. auto phiInst = mBeModule->AllocInstOwned<BePhiInst>();
  1716. phiInst->mType = type;
  1717. SetResult(curId, phiInst);
  1718. }
  1719. break;
  1720. case BfIRCmd_AddPhiIncoming:
  1721. {
  1722. CMD_PARAM(BeValue*, phiValue);
  1723. CMD_PARAM(BeValue*, value);
  1724. CMD_PARAM(BeBlock*, comingFrom);
  1725. auto phiIncoming = mBeModule->mAlloc.Alloc<BePhiIncoming>();
  1726. phiIncoming->mBlock = comingFrom;
  1727. phiIncoming->mValue = value;
  1728. ((BePhiInst*)phiValue)->mIncoming.push_back(phiIncoming);
  1729. }
  1730. break;
  1731. case BfIRCmd_GetIntrinsic:
  1732. {
  1733. CMD_PARAM(int, intrinId);
  1734. CMD_PARAM(CmdParamVec<BeType*>, paramTypes);
  1735. auto intrin = mBeModule->mAlloc.Alloc<BeIntrinsic>();
  1736. intrin->mKind = (BfIRIntrinsic)intrinId;
  1737. switch (intrin->mKind)
  1738. {
  1739. case BfIRIntrinsic_AtomicAdd:
  1740. case BfIRIntrinsic_AtomicAnd:
  1741. case BfIRIntrinsic_AtomicCmpXChg:
  1742. case BfIRIntrinsic_AtomicLoad:
  1743. case BfIRIntrinsic_AtomicMax:
  1744. case BfIRIntrinsic_AtomicMin:
  1745. case BfIRIntrinsic_AtomicNAnd:
  1746. case BfIRIntrinsic_AtomicOr:
  1747. case BfIRIntrinsic_AtomicSub:
  1748. case BfIRIntrinsic_AtomicUMax:
  1749. case BfIRIntrinsic_AtomicUMin:
  1750. case BfIRIntrinsic_AtomicXChg:
  1751. case BfIRIntrinsic_AtomicXor:
  1752. if (!paramTypes.IsEmpty())
  1753. {
  1754. BF_ASSERT(paramTypes[0]->IsPointer());
  1755. if (paramTypes[0]->IsPointer())
  1756. intrin->mReturnType = ((BePointerType*)paramTypes[0])->mElementType;
  1757. }
  1758. else
  1759. intrin->mReturnType = mBeContext->GetPrimitiveType(BeTypeCode_None);
  1760. break;
  1761. case BfIRIntrinsic_AtomicCmpStore:
  1762. case BfIRIntrinsic_AtomicCmpStore_Weak:
  1763. intrin->mReturnType = mBeContext->GetPrimitiveType(BeTypeCode_Boolean);
  1764. break;
  1765. }
  1766. SetResult(curId, intrin);
  1767. }
  1768. break;
  1769. case BfIRCmd_CreateFunctionType:
  1770. {
  1771. CMD_PARAM(BeType*, resultType);
  1772. CMD_PARAM(CmdParamVec<BeType*>, paramTypes);
  1773. CMD_PARAM(bool, isVarArg);
  1774. auto functionType = mBeContext->CreateFunctionType(resultType, paramTypes, isVarArg);
  1775. SetResult(curId, functionType);
  1776. }
  1777. break;
  1778. case BfIRCmd_CreateFunction:
  1779. {
  1780. CMD_PARAM(BeFunctionType*, type);
  1781. BfIRLinkageType linkageType = (BfIRLinkageType)mStream->Read();
  1782. CMD_PARAM(String, name);
  1783. SetResult(curId, mBeModule->CreateFunction(type, linkageType, name));
  1784. }
  1785. break;
  1786. case BfIRCmd_EnsureFunctionPatchable:
  1787. {
  1788. }
  1789. break;
  1790. case BfIRCmd_RemapBindFunction:
  1791. {
  1792. CMD_PARAM(BeValue*, func);
  1793. // We need to store this value to a data segment so we get a symbol we can remap during hot swap
  1794. // We actually do this to ensure that we don't bind to the NEW method but rather the old one- so
  1795. // delegate equality checks still work
  1796. BeFunction* beFunc = BeValueDynCast<BeFunction>(func);
  1797. if (beFunc != NULL)
  1798. {
  1799. if (beFunc->mRemapBindVar == NULL)
  1800. {
  1801. auto globalVariable = mBeModule->mGlobalVariables.Alloc();
  1802. globalVariable->mModule = mBeModule;
  1803. globalVariable->mType = beFunc->mType;
  1804. globalVariable->mIsConstant = true;
  1805. globalVariable->mLinkageType = BfIRLinkageType_External;
  1806. globalVariable->mInitializer = beFunc;
  1807. globalVariable->mName = StrFormat("bf_hs_preserve@%s_%s", beFunc->mName.c_str(), mBeModule->mModuleName.c_str());
  1808. globalVariable->mIsTLS = false;
  1809. globalVariable->mAlign = 8;
  1810. globalVariable->mUnnamedAddr = false;
  1811. beFunc->mRemapBindVar = globalVariable;
  1812. /*if (mBeModule->mDbgModule != NULL)
  1813. {
  1814. auto dbgGlobalVariable = mBeModule->mDbgModule->mGlobalVariables.Alloc();
  1815. dbgGlobalVariable->mContext = mBeContext;
  1816. dbgGlobalVariable->mName = name;
  1817. dbgGlobalVariable->mLinkageName = globalVariable->mName;
  1818. dbgGlobalVariable->mFile = (BeDbgFile*)file;
  1819. dbgGlobalVariable->mLineNum = lineNum;
  1820. dbgGlobalVariable->mType = (BeDbgType*)type;
  1821. dbgGlobalVariable->mIsLocalToUnit = isLocalToUnit;
  1822. dbgGlobalVariable->mValue = val;
  1823. dbgGlobalVariable->mDecl = decl;
  1824. }*/
  1825. }
  1826. SetResult(curId, mBeModule->CreateLoad(beFunc->mRemapBindVar, false));
  1827. }
  1828. else
  1829. SetResult(curId, func);
  1830. }
  1831. break;
  1832. case BfIRCmd_SetActiveFunction:
  1833. {
  1834. CMD_PARAM(BeFunction*, func);
  1835. mActiveFunction = func;
  1836. mBeModule->mActiveFunction = func;
  1837. }
  1838. break;
  1839. case BfIRCmd_CreateCall:
  1840. {
  1841. CMD_PARAM(BeValue*, func);
  1842. CMD_PARAM(CmdParamVec<BeValue*>, args);
  1843. #ifdef _DEBUG
  1844. auto funcPtrType = func->GetType();
  1845. if (funcPtrType != NULL)
  1846. {
  1847. BF_ASSERT(funcPtrType->IsPointer());
  1848. auto funcType = (BeFunctionType*)((BePointerType*)funcPtrType)->mElementType;
  1849. BF_ASSERT(funcType->mTypeCode == BeTypeCode_Function);
  1850. if (!funcType->mIsVarArg)
  1851. {
  1852. BF_ASSERT(funcType->mParams.size() == args.size());
  1853. int argIdx = 0;
  1854. for (int argIdx = 0; argIdx < (int)args.size(); argIdx++)
  1855. {
  1856. BF_ASSERT(funcType->mParams[argIdx].mType == args[argIdx]->GetType());
  1857. }
  1858. }
  1859. }
  1860. else
  1861. {
  1862. BF_ASSERT(func->GetTypeId() == BeIntrinsic::TypeId);
  1863. }
  1864. #endif
  1865. SetResult(curId, mBeModule->CreateCall(func, args));
  1866. }
  1867. break;
  1868. case BfIRCmd_SetCallCallingConv:
  1869. {
  1870. CMD_PARAM(BeValue*, callInst);
  1871. BfIRCallingConv callingConv = (BfIRCallingConv)mStream->Read();
  1872. ((BeCallInst*)callInst)->mCallingConv = callingConv;
  1873. }
  1874. break;
  1875. case BfIRCmd_SetFuncCallingConv:
  1876. {
  1877. CMD_PARAM(BeFunction*, func);
  1878. BfIRCallingConv callingConv = (BfIRCallingConv)mStream->Read();
  1879. func->mCallingConv = callingConv;
  1880. }
  1881. break;
  1882. case BfIRCmd_SetTailCall:
  1883. {
  1884. CMD_PARAM(BeValue*, callInstVal);
  1885. BeCallInst* callInst = (BeCallInst*)callInstVal;
  1886. callInst->mTailCall = true;
  1887. }
  1888. break;
  1889. case BfIRCmd_SetCallAttribute:
  1890. {
  1891. CMD_PARAM(BeValue*, callInstVal);
  1892. CMD_PARAM(int, paramIdx);
  1893. BfIRAttribute attribute = (BfIRAttribute)mStream->Read();
  1894. BeCallInst* callInst = (BeCallInst*)callInstVal;
  1895. if (attribute == BfIRAttribute_NoReturn)
  1896. callInst->mNoReturn = true;
  1897. }
  1898. break;
  1899. case BfIRCmd_CreateRet:
  1900. {
  1901. CMD_PARAM(BeValue*, val);
  1902. SetResult(curId, mBeModule->CreateRet(val));
  1903. }
  1904. break;
  1905. case BfIRCmd_CreateRetVoid:
  1906. {
  1907. mBeModule->CreateRetVoid();
  1908. }
  1909. break;
  1910. case BfIRCmd_CreateUnreachable:
  1911. {
  1912. mBeModule->AllocInst<BeUnreachableInst>();
  1913. }
  1914. break;
  1915. case BfIRCmd_Call_AddAttribute:
  1916. {
  1917. CMD_PARAM(BeValue*, callInstVal);
  1918. CMD_PARAM(int, argIdx);
  1919. BfIRAttribute attribute = (BfIRAttribute)mStream->Read();
  1920. BeCallInst* callInst = (BeCallInst*)callInstVal;
  1921. if (argIdx > 0)
  1922. {
  1923. if (attribute == BfIRAttribute_StructRet)
  1924. {
  1925. BF_ASSERT(argIdx == 1);
  1926. callInst->mArgs[argIdx - 1].mStructRet = true;
  1927. }
  1928. else if (attribute == BfIRAttribute_ZExt)
  1929. callInst->mArgs[argIdx - 1].mZExt = true;
  1930. else if (attribute == BfIRAttribute_NoAlias)
  1931. callInst->mArgs[argIdx - 1].mNoAlias = true;
  1932. else if (attribute == BfIRAttribute_NoCapture)
  1933. callInst->mArgs[argIdx - 1].mNoCapture = true;
  1934. else
  1935. BF_FATAL("Unhandled");
  1936. }
  1937. else
  1938. {
  1939. if (attribute == BfIRAttribute_NoReturn)
  1940. callInst->mNoReturn = true;
  1941. else
  1942. BF_FATAL("Unhandled");
  1943. }
  1944. }
  1945. break;
  1946. case BfIRCmd_Call_AddAttribute1:
  1947. {
  1948. CMD_PARAM(BeValue*, inst);
  1949. CMD_PARAM(int, argIdx);
  1950. BfIRAttribute attribute = (BfIRAttribute)mStream->Read();
  1951. CMD_PARAM(int, arg);
  1952. BeCallInst* callInst = BeValueDynCast<BeCallInst>(inst);
  1953. if (callInst != NULL)
  1954. {
  1955. if (argIdx > 0)
  1956. {
  1957. if (attribute == BfIRAttribute_Dereferencable)
  1958. callInst->mArgs[argIdx - 1].mDereferenceableSize = arg;
  1959. else
  1960. BF_FATAL("Unhandled");
  1961. }
  1962. else
  1963. {
  1964. BF_FATAL("Unhandled");
  1965. }
  1966. }
  1967. }
  1968. break;
  1969. case BfIRCmd_Func_AddAttribute:
  1970. {
  1971. CMD_PARAM(BeFunction*, func);
  1972. CMD_PARAM(int, argIdx);
  1973. BfIRAttribute attribute = (BfIRAttribute)mStream->Read();
  1974. if (argIdx > 0)
  1975. {
  1976. if (attribute == BfIRAttribute_StructRet)
  1977. func->mParams[argIdx - 1].mStructRet = true;
  1978. else if (attribute == BfIRAttribute_NoAlias)
  1979. func->mParams[argIdx - 1].mNoAlias = true;
  1980. else if (attribute == BfIRAttribute_NoCapture)
  1981. func->mParams[argIdx - 1].mNoCapture = true;
  1982. else if (attribute == BfIRAttribute_ZExt)
  1983. func->mParams[argIdx - 1].mZExt = true;
  1984. else
  1985. BF_FATAL("Unhandled");
  1986. }
  1987. else
  1988. {
  1989. if (attribute == BFIRAttribute_AlwaysInline)
  1990. func->mAlwaysInline = true;
  1991. else if (attribute == BFIRAttribute_NoUnwind)
  1992. func->mNoUnwind = true;
  1993. else if (attribute == BFIRAttribute_UWTable)
  1994. func->mUWTable = true;
  1995. else if (attribute == BfIRAttribute_NoReturn)
  1996. func->mNoReturn = true;
  1997. else if (attribute == BFIRAttribute_NoFramePointerElim)
  1998. func->mNoFramePointerElim = true;
  1999. else if (attribute == BFIRAttribute_DllExport)
  2000. func->mIsDLLExport = true;
  2001. else if (attribute == BFIRAttribute_DllImport)
  2002. func->mIsDLLImport = true;
  2003. else
  2004. BF_FATAL("Unhandled");
  2005. }
  2006. }
  2007. break;
  2008. case BfIRCmd_Func_AddAttribute1:
  2009. {
  2010. CMD_PARAM(BeFunction*, func);
  2011. CMD_PARAM(int, argIdx);
  2012. BfIRAttribute attribute = (BfIRAttribute)mStream->Read();
  2013. CMD_PARAM(int, arg);
  2014. // This is for adding things like Dereferencable, which we don't use
  2015. if (argIdx > 0)
  2016. {
  2017. if (attribute == BfIRAttribute_Dereferencable)
  2018. func->mParams[argIdx - 1].mDereferenceableSize = arg;
  2019. else
  2020. BF_FATAL("Unhandled");
  2021. }
  2022. else
  2023. BF_FATAL("Unhandled");
  2024. }
  2025. break;
  2026. case BfIRCmd_Func_SetParamName:
  2027. {
  2028. CMD_PARAM(BeFunction*, func);
  2029. CMD_PARAM(int, argIdx);
  2030. CMD_PARAM(String, name);
  2031. if (argIdx > 0)
  2032. func->mParams[argIdx - 1].mName = name;
  2033. }
  2034. break;
  2035. case BfIRCmd_Func_DeleteBody:
  2036. {
  2037. CMD_PARAM(BeFunction*, func);
  2038. func->mBlocks.Clear();
  2039. }
  2040. break;
  2041. case BfIRCmd_Func_SetLinkage:
  2042. {
  2043. CMD_PARAM(BeFunction*, func);
  2044. BfIRLinkageType linkageType = (BfIRLinkageType)mStream->Read();
  2045. func->mLinkageType = linkageType;
  2046. }
  2047. break;
  2048. case BfIRCmd_SaveDebugLocation:
  2049. {
  2050. mSavedDebugLocs.push_back(mBeModule->GetCurrentDebugLocation());
  2051. }
  2052. break;
  2053. case BfIRCmd_RestoreDebugLocation:
  2054. {
  2055. mBeModule->SetCurrentDebugLocation(mSavedDebugLocs[mSavedDebugLocs.size() - 1]);
  2056. mSavedDebugLocs.pop_back();
  2057. }
  2058. break;
  2059. case BfIRCmd_ClearDebugLocation:
  2060. {
  2061. mBeModule->SetCurrentDebugLocation(NULL);
  2062. }
  2063. break;
  2064. case BfIRCmd_ClearDebugLocationInst:
  2065. {
  2066. CMD_PARAM(BeValue*, instValue);
  2067. auto inst = (BeInst*)instValue;
  2068. inst->mDbgLoc = NULL;
  2069. }
  2070. break;
  2071. case BfIRCmd_ClearDebugLocationInstLast:
  2072. {
  2073. if ((mBeModule->mActiveBlock != NULL) && (!mBeModule->mActiveBlock->mInstructions.IsEmpty()))
  2074. {
  2075. auto inst = mBeModule->mActiveBlock->mInstructions.back();
  2076. inst->mDbgLoc = NULL;
  2077. }
  2078. }
  2079. break;
  2080. case BfIRCmd_UpdateDebugLocation:
  2081. {
  2082. CMD_PARAM(BeValue*, instValue);
  2083. auto inst = (BeInst*)instValue;
  2084. inst->mDbgLoc = mBeModule->mCurDbgLoc;
  2085. }
  2086. break;
  2087. case BfIRCmd_SetCurrentDebugLocation:
  2088. {
  2089. CMD_PARAM(int, line);
  2090. CMD_PARAM(int, column);
  2091. CMD_PARAM(BeMDNode*, diScope);
  2092. CMD_PARAM(BeMDNode*, diInlinedAt);
  2093. mBeModule->SetCurrentDebugLocation(line - 1, column - 1, diScope, (BeDbgLoc*)diInlinedAt);
  2094. }
  2095. break;
  2096. case BfIRCmd_Nop:
  2097. {
  2098. mBeModule->CreateNop();
  2099. }
  2100. break;
  2101. case BfIRCmd_EnsureInstructionAt:
  2102. {
  2103. mBeModule->AllocInst<BeEnsureInstructionAtInst>();
  2104. }
  2105. break;
  2106. case BfIRCmd_StatementStart:
  2107. {
  2108. }
  2109. break;
  2110. case BfIRCmd_ObjectAccessCheck:
  2111. {
  2112. CMD_PARAM(BeValue*, val);
  2113. CMD_PARAM(bool, useAsm);
  2114. auto inst = mBeModule->AllocInst<BeObjectAccessCheckInst>();
  2115. inst->mValue = val;
  2116. SetResult(curId, mBeModule->GetInsertBlock());
  2117. }
  2118. break;
  2119. case BfIRCmd_DbgInit:
  2120. {
  2121. /*mDIBuilder = new BeDIBuilder(*mBeModule); */
  2122. mBeModule->mDbgModule = new BeDbgModule();
  2123. mBeModule->mDbgModule->mBeModule = mBeModule;
  2124. }
  2125. break;
  2126. case BfIRCmd_DbgFinalize:
  2127. {
  2128. /*for (auto typeEntryPair : mTypes)
  2129. {
  2130. auto& typeEntry = typeEntryPair.second;
  2131. if (typeEntry.mInstDIType != NULL)
  2132. typeEntry.mInstDIType->resolveCycles();
  2133. }
  2134. mDIBuilder->finalize();*/
  2135. }
  2136. break;
  2137. case BfIRCmd_DbgCreateCompileUnit:
  2138. {
  2139. CMD_PARAM(int, lang);
  2140. CMD_PARAM(String, fileName);
  2141. CMD_PARAM(String, directory);
  2142. CMD_PARAM(String, producer);
  2143. CMD_PARAM(bool, isOptimized);
  2144. CMD_PARAM(String, flags);
  2145. CMD_PARAM(int, runtimeVer);
  2146. CMD_PARAM(bool, linesOnly);
  2147. mBeModule->mDbgModule->mFileName = fileName;
  2148. mBeModule->mDbgModule->mDirectory = directory;
  2149. mBeModule->mDbgModule->mProducer = producer;
  2150. //mDebugging = fileName == "TestToots";
  2151. SetResult(curId, mBeModule->mDbgModule);
  2152. }
  2153. break;
  2154. case BfIRCmd_DbgCreateFile:
  2155. {
  2156. CMD_PARAM(String, fileName);
  2157. CMD_PARAM(String, directory);
  2158. auto dbgFile = mBeModule->mDbgModule->mFiles.Alloc();
  2159. dbgFile->mFileName = fileName;
  2160. dbgFile->mDirectory = directory;
  2161. dbgFile->mIdx = (int)mBeModule->mDbgModule->mFiles.size() - 1;
  2162. SetResult(curId, dbgFile);
  2163. }
  2164. break;
  2165. case BfIRCmd_DbgGetCurrentLocation:
  2166. {
  2167. SetResult(curId, mBeModule->mCurDbgLoc);
  2168. }
  2169. break;
  2170. case BfIRCmd_DbgSetType:
  2171. {
  2172. CMD_PARAM(int, typeId);
  2173. CMD_PARAM(BeMDNode*, type);
  2174. GetTypeEntry(typeId).mDIType = (BeDbgType*)type;
  2175. }
  2176. break;
  2177. case BfIRCmd_DbgSetInstType:
  2178. {
  2179. CMD_PARAM(int, typeId);
  2180. CMD_PARAM(BeMDNode*, type);
  2181. GetTypeEntry(typeId).mInstDIType = (BeDbgType*)type;
  2182. }
  2183. break;
  2184. case BfIRCmd_DbgGetType:
  2185. {
  2186. CMD_PARAM(int, typeId);
  2187. SetResult(curId, GetTypeEntry(typeId).mDIType);
  2188. }
  2189. break;
  2190. case BfIRCmd_DbgGetTypeInst:
  2191. {
  2192. CMD_PARAM(int, typeId);
  2193. SetResult(curId, GetTypeEntry(typeId).mInstDIType);
  2194. }
  2195. break;
  2196. case BfIRCmd_DbgTrackDITypes:
  2197. {
  2198. CMD_PARAM(int, typeId);
  2199. auto& typeEntry = GetTypeEntry(typeId);
  2200. /*if (typeEntry.mDIType != NULL)
  2201. BeMetadataTracking::track(*(BeMetadata**)&typeEntry.mDIType);
  2202. if (typeEntry.mInstDIType != NULL)
  2203. BeMetadataTracking::track(*(BeMetadata**)&typeEntry.mInstDIType);*/
  2204. //NotImpl();
  2205. }
  2206. break;
  2207. case BfIRCmd_DbgCreateNamespace:
  2208. {
  2209. CMD_PARAM(BeMDNode*, scope);
  2210. CMD_PARAM(String, name);
  2211. CMD_PARAM(BeMDNode*, file);
  2212. CMD_PARAM(int, lineNum);
  2213. auto dbgNamespace = mBeModule->mOwnedValues.Alloc<BeDbgNamespace>();
  2214. dbgNamespace->mScope = scope;
  2215. dbgNamespace->mName = name;
  2216. SetResult(curId, dbgNamespace);
  2217. }
  2218. break;
  2219. case BfIRCmd_DbgCreateImportedModule:
  2220. {
  2221. CMD_PARAM(BeMDNode*, context);
  2222. CMD_PARAM(BeMDNode*, namespaceNode);
  2223. CMD_PARAM(int, lineNum);
  2224. /*SetResult(curId, mDIBuilder->createImportedModule((BeDIScope*)context, (BeDINamespace*)namespaceNode, lineNum));*/
  2225. //NotImpl();
  2226. }
  2227. break;
  2228. case BfIRCmd_DbgCreateBasicType:
  2229. {
  2230. CMD_PARAM(String, name);
  2231. CMD_PARAM(int64, sizeInBits);
  2232. CMD_PARAM(int64, alignInBits);
  2233. CMD_PARAM(int, encoding);
  2234. auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgBasicType>();
  2235. dbgType->mName = name;
  2236. dbgType->mSize = (int)(sizeInBits / 8);
  2237. dbgType->mAlign = (int)(alignInBits / 8);
  2238. dbgType->mEncoding = encoding;
  2239. SetResult(curId, dbgType);
  2240. }
  2241. break;
  2242. case BfIRCmd_DbgCreateStructType:
  2243. {
  2244. CMD_PARAM(BeMDNode*, context);
  2245. CMD_PARAM(String, name);
  2246. CMD_PARAM(BeMDNode*, file);
  2247. CMD_PARAM(int, lineNum);
  2248. CMD_PARAM(int64, sizeInBits);
  2249. CMD_PARAM(int64, alignInBits);
  2250. CMD_PARAM(int, flags);
  2251. CMD_PARAM(BeMDNode*, derivedFrom);
  2252. CMD_PARAM(CmdParamVec<BeMDNode*>, members);
  2253. auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgStructType>();
  2254. dbgType->mScope = context;
  2255. dbgType->mName = name;
  2256. dbgType->mSize = (int)(sizeInBits / 8);
  2257. dbgType->mAlign = (int)(alignInBits / 8);
  2258. dbgType->mDerivedFrom = (BeDbgType*)derivedFrom;
  2259. dbgType->mDefFile = (BeDbgFile*)file;
  2260. dbgType->mDefLine = lineNum - 1;
  2261. dbgType->mIsFullyDefined = true;
  2262. dbgType->SetMembers(members);
  2263. SetResult(curId, dbgType);
  2264. }
  2265. break;
  2266. case BfIRCmd_DbgCreateEnumerationType:
  2267. {
  2268. CMD_PARAM(BeMDNode*, context);
  2269. CMD_PARAM(String, name);
  2270. CMD_PARAM(BeMDNode*, file);
  2271. CMD_PARAM(int, lineNum);
  2272. CMD_PARAM(int64, sizeInBits);
  2273. CMD_PARAM(int64, alignInBits);
  2274. CMD_PARAM(CmdParamVec<BeMDNode*>, members);
  2275. CMD_PARAM(BeMDNode*, underlyingType);
  2276. auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgEnumType>();
  2277. dbgType->mScope = context;
  2278. dbgType->mName = name;
  2279. dbgType->mSize = (int)(sizeInBits / 8);
  2280. dbgType->mAlign = (int)(alignInBits / 8);
  2281. dbgType->mIsFullyDefined = true;
  2282. dbgType->mElementType = (BeDbgType*)underlyingType;
  2283. for (auto member : members)
  2284. {
  2285. if (auto enumMember = BeValueDynCast<BeDbgEnumMember>(member))
  2286. {
  2287. dbgType->mMembers.push_back(enumMember);
  2288. }
  2289. else
  2290. NotImpl();
  2291. }
  2292. //dbgType->mDefFile = (BeDbgFile*)file;
  2293. //dbgType->mDefLine = line - 1;
  2294. SetResult(curId, dbgType);
  2295. }
  2296. break;
  2297. case BfIRCmd_DbgCreatePointerType:
  2298. {
  2299. CMD_PARAM(BeMDNode*, elementTypeNode);
  2300. BeDbgType* elementType = (BeDbgType*)elementTypeNode;
  2301. BeDbgType* useType = elementType->FindDerivedType(BeDbgPointerType::TypeId);
  2302. if (useType == NULL)
  2303. {
  2304. auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgPointerType>();
  2305. dbgType->mElement = elementType;
  2306. dbgType->mSize = mPtrSize;
  2307. dbgType->mAlign = mPtrSize;
  2308. elementType->mDerivedTypes.PushFront(dbgType, &mBeModule->mAlloc);
  2309. useType = dbgType;
  2310. }
  2311. SetResult(curId, useType);
  2312. }
  2313. break;
  2314. case BfIRCmd_DbgCreateReferenceType:
  2315. {
  2316. CMD_PARAM(BeMDNode*, elementTypeNode);
  2317. auto useType = mBeModule->mDbgModule->CreateReferenceType((BeDbgType*)elementTypeNode);
  2318. SetResult(curId, useType);
  2319. }
  2320. break;
  2321. case BfIRCmd_DbgCreateConstType:
  2322. {
  2323. CMD_PARAM(BeMDNode*, elementTypeNode);
  2324. BeDbgType* elementType = (BeDbgType*)elementTypeNode;
  2325. BeDbgType* useType = elementType->FindDerivedType(BeDbgConstType::TypeId);
  2326. if (useType == NULL)
  2327. {
  2328. auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgConstType>();
  2329. dbgType->mElement = elementType;
  2330. elementType->mDerivedTypes.PushFront(dbgType, &mBeModule->mAlloc);
  2331. useType = dbgType;
  2332. }
  2333. SetResult(curId, useType);
  2334. }
  2335. break;
  2336. case BfIRCmd_DbgCreateArtificialType:
  2337. {
  2338. CMD_PARAM(BeMDNode*, diType);
  2339. //auto dbgType = mBeModule->mOwnedValues.Alloc<BeDbgArtificialType>();
  2340. //dbgType->mElement = (BeDbgType*)diType;
  2341. // Does the artificial thing do anything for us actually?
  2342. auto dbgType = diType;
  2343. SetResult(curId, dbgType);
  2344. }
  2345. break;
  2346. case BfIRCmd_DbgCreateArrayType:
  2347. {
  2348. CMD_PARAM(int64, sizeInBits);
  2349. CMD_PARAM(int64, alignInBits);
  2350. CMD_PARAM(BeMDNode*, elementType);
  2351. CMD_PARAM(int64, numElements);
  2352. auto dbgArray = mBeModule->mDbgModule->mTypes.Alloc<BeDbgArrayType>();
  2353. dbgArray->mSize = (int)(sizeInBits / 8);
  2354. dbgArray->mAlign = (int)(alignInBits / 8);
  2355. dbgArray->mElement = (BeDbgType*)elementType;
  2356. dbgArray->mNumElements = numElements;
  2357. SetResult(curId, dbgArray);
  2358. }
  2359. break;
  2360. case BfIRCmd_DbgCreateReplaceableCompositeType:
  2361. {
  2362. CMD_PARAM(int, tag);
  2363. CMD_PARAM(String, name);
  2364. CMD_PARAM(BeMDNode*, scope);
  2365. CMD_PARAM(BeMDNode*, file);
  2366. CMD_PARAM(int, line);
  2367. CMD_PARAM(int64, sizeInBits);
  2368. CMD_PARAM(int64, alignInBits);
  2369. CMD_PARAM(int, flags);
  2370. if (tag == llvm::dwarf::DW_TAG_structure_type)
  2371. {
  2372. auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgStructType>();
  2373. dbgType->mScope = scope;
  2374. dbgType->mName = name;
  2375. dbgType->mSize = (int)(sizeInBits / 8);
  2376. dbgType->mAlign = (int)(alignInBits / 8);
  2377. dbgType->mDefFile = (BeDbgFile*)file;
  2378. dbgType->mDefLine = line - 1;
  2379. SetResult(curId, dbgType);
  2380. }
  2381. else if (tag == llvm::dwarf::DW_TAG_enumeration_type)
  2382. {
  2383. auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgEnumType>();
  2384. dbgType->mScope = scope;
  2385. dbgType->mName = name;
  2386. dbgType->mSize = (int)(sizeInBits / 8);
  2387. dbgType->mAlign = (int)(alignInBits / 8);
  2388. //dbgType->mDefFile = (BeDbgFile*)file;
  2389. //dbgType->mDefLine = line - 1;
  2390. SetResult(curId, dbgType);
  2391. }
  2392. else
  2393. NotImpl();
  2394. }
  2395. break;
  2396. case BfIRCmd_DbgCreateForwardDecl:
  2397. {
  2398. CMD_PARAM(int, tag);
  2399. CMD_PARAM(String, name);
  2400. CMD_PARAM(BeMDNode*, scope);
  2401. CMD_PARAM(BeMDNode*, file);
  2402. CMD_PARAM(int, line);
  2403. if (tag == llvm::dwarf::DW_TAG_structure_type)
  2404. {
  2405. auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgStructType>();
  2406. dbgType->mScope = scope;
  2407. dbgType->mName = name;
  2408. dbgType->mDefFile = (BeDbgFile*)file;
  2409. dbgType->mDefLine = line;
  2410. SetResult(curId, dbgType);
  2411. }
  2412. else if (tag == llvm::dwarf::DW_TAG_enumeration_type)
  2413. {
  2414. auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgEnumType>();
  2415. dbgType->mScope = scope;
  2416. dbgType->mName = name;
  2417. SetResult(curId, dbgType);
  2418. }
  2419. else
  2420. NotImpl();
  2421. }
  2422. break;
  2423. case BfIRCmd_DbgCreateSizedForwardDecl:
  2424. {
  2425. CMD_PARAM(int, tag);
  2426. CMD_PARAM(String, name);
  2427. CMD_PARAM(BeMDNode*, scope);
  2428. CMD_PARAM(BeMDNode*, file);
  2429. CMD_PARAM(int, line);
  2430. CMD_PARAM(int64, sizeInBits);
  2431. CMD_PARAM(int64, alignInBits);
  2432. if (tag == llvm::dwarf::DW_TAG_structure_type)
  2433. {
  2434. auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgStructType>();
  2435. dbgType->mDefFile = (BeDbgFile*)file;
  2436. dbgType->mDefLine = line;
  2437. dbgType->mScope = scope;
  2438. dbgType->mName = name;
  2439. dbgType->mSize = (int)(sizeInBits / 8);
  2440. dbgType->mAlign = (int)(alignInBits / 8);
  2441. SetResult(curId, dbgType);
  2442. }
  2443. else if (tag == llvm::dwarf::DW_TAG_enumeration_type)
  2444. {
  2445. auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgEnumType>();
  2446. dbgType->mScope = scope;
  2447. dbgType->mName = name;
  2448. dbgType->mSize = (int)(sizeInBits / 8);
  2449. dbgType->mAlign = (int)(alignInBits / 8);
  2450. SetResult(curId, dbgType);
  2451. }
  2452. else
  2453. NotImpl();
  2454. }
  2455. break;
  2456. case BeIRCmd_DbgSetTypeSize:
  2457. {
  2458. CMD_PARAM(BeMDNode*, mdType);
  2459. CMD_PARAM(int64, sizeInBits);
  2460. CMD_PARAM(int64, alignInBits);
  2461. auto dbgType = (BeDbgType*)mdType;
  2462. dbgType->mSize = (int)(sizeInBits / 8);
  2463. dbgType->mAlign = (int)(alignInBits / 8);
  2464. }
  2465. break;
  2466. case BfIRCmd_DbgReplaceAllUses:
  2467. {
  2468. CMD_PARAM(BeMDNode*, diPrevNode);
  2469. CMD_PARAM(BeMDNode*, diNewNode);
  2470. /*diPrevNode->replaceAllUsesWith(diNewNode); */
  2471. NotImpl();
  2472. }
  2473. break;
  2474. case BfIRCmd_DbgDeleteTemporary:
  2475. {
  2476. CMD_PARAM(BeMDNode*, diNode);
  2477. /*BeMDNode::deleteTemporary(diNode);*/
  2478. NotImpl();
  2479. }
  2480. break;
  2481. case BfIRCmd_DbgMakePermanent:
  2482. {
  2483. CMD_PARAM(BeMDNode*, diNode);
  2484. CMD_PARAM(BeMDNode*, diBaseType);
  2485. CMD_PARAM(CmdParamVec<BeMDNode*>, members);
  2486. if (auto dbgType = BeValueDynCast<BeDbgStructType>(diNode))
  2487. {
  2488. dbgType->SetMembers(members);
  2489. }
  2490. else if (auto dbgType = BeValueDynCast<BeDbgEnumType>(diNode))
  2491. {
  2492. dbgType->mElementType = (BeDbgType*)diBaseType;
  2493. dbgType->SetMembers(members);
  2494. }
  2495. else
  2496. NotImpl();
  2497. SetResult(curId, diNode);
  2498. break;
  2499. }
  2500. case BfIRCmd_CreateEnumerator:
  2501. {
  2502. CMD_PARAM(String, name);
  2503. CMD_PARAM(int64, val);
  2504. auto enumValue = mBeModule->mOwnedValues.Alloc<BeDbgEnumMember>();
  2505. enumValue->mName = name;
  2506. enumValue->mValue = val;
  2507. SetResult(curId, enumValue);
  2508. }
  2509. break;
  2510. case BfIRCmd_DbgCreateMemberType:
  2511. {
  2512. CMD_PARAM(BeMDNode*, scope);
  2513. CMD_PARAM(String, name);
  2514. CMD_PARAM(BeMDNode*, file);
  2515. CMD_PARAM(int, lineNumber);
  2516. CMD_PARAM(int64, sizeInBits);
  2517. CMD_PARAM(int64, alignInBits);
  2518. CMD_PARAM(int64, offsetInBits);
  2519. CMD_PARAM(int, flags);
  2520. CMD_PARAM(BeMDNode*, type);
  2521. auto dbgMember = mBeModule->mOwnedValues.Alloc<BeDbgStructMember>();
  2522. dbgMember->mName = name;
  2523. dbgMember->mType = (BeDbgType*)type;
  2524. dbgMember->mOffset = (int)(offsetInBits / 8);
  2525. dbgMember->mFlags = flags;
  2526. SetResult(curId, dbgMember);
  2527. }
  2528. break;
  2529. case BfIRCmd_DbgStaticCreateMemberType:
  2530. {
  2531. CMD_PARAM(BeMDNode*, scope);
  2532. CMD_PARAM(String, name);
  2533. CMD_PARAM(BeMDNode*, file);
  2534. CMD_PARAM(int, lineNumber);
  2535. CMD_PARAM(BeMDNode*, type);
  2536. CMD_PARAM(int, flags);
  2537. CMD_PARAM(BeConstant*, val);
  2538. auto dbgMember = mBeModule->mOwnedValues.Alloc<BeDbgStructMember>();
  2539. dbgMember->mName = name;
  2540. dbgMember->mType = (BeDbgType*)type;
  2541. dbgMember->mOffset = -1;
  2542. dbgMember->mStaticValue = val;
  2543. dbgMember->mFlags = flags;
  2544. dbgMember->mIsStatic = true;
  2545. SetResult(curId, dbgMember);
  2546. }
  2547. break;
  2548. case BfIRCmd_DbgCreateInheritance:
  2549. {
  2550. CMD_PARAM(BeMDNode*, type);
  2551. CMD_PARAM(BeMDNode*, baseType);
  2552. CMD_PARAM(int64, baseOffset);
  2553. CMD_PARAM(int, flags);
  2554. auto dbgInheritance = mBeModule->mAlloc.Alloc<BeDbgInheritance>();
  2555. dbgInheritance->mBaseType = (BeDbgType*)baseType;
  2556. SetResult(curId, dbgInheritance);
  2557. }
  2558. break;
  2559. case BfIRCmd_DbgCreateMethod:
  2560. {
  2561. CMD_PARAM(BeMDNode*, context);
  2562. CMD_PARAM(String, name);
  2563. CMD_PARAM(String, linkageName);
  2564. CMD_PARAM(BeMDNode*, file);
  2565. CMD_PARAM(int, lineNum);
  2566. CMD_PARAM(BeMDNode*, type);
  2567. CMD_PARAM(bool, isLocalToUnit);
  2568. CMD_PARAM(bool, isDefinition);
  2569. CMD_PARAM(int, vk);
  2570. CMD_PARAM(int, vIndex);
  2571. CMD_PARAM(BeMDNode*, vTableHolder);
  2572. CMD_PARAM(int, flags);
  2573. CMD_PARAM(bool, isOptimized);
  2574. CMD_PARAM(BeValue*, fn);
  2575. CMD_PARAM(CmdParamVec<BeMDNode*>, genericArgs);
  2576. CMD_PARAM(CmdParamVec<BeConstant*>, genericConstValueArgs);
  2577. auto dbgFunc = mBeModule->mOwnedValues.Alloc<BeDbgFunction>();
  2578. dbgFunc->mScope = context;
  2579. dbgFunc->mFile = (BeDbgFile*)file;
  2580. dbgFunc->mLine = lineNum - 1;
  2581. dbgFunc->mType = (BeDbgFunctionType*)type;
  2582. dbgFunc->mName = name;
  2583. dbgFunc->mLinkageName = linkageName;
  2584. dbgFunc->mValue = (BeFunction*)fn;
  2585. dbgFunc->mIsLocalToUnit = isLocalToUnit;
  2586. dbgFunc->mVK = vk;
  2587. dbgFunc->mVIndex = vIndex;
  2588. dbgFunc->mIsStaticMethod = (flags & llvm::DINode::FlagStaticMember) != 0;
  2589. dbgFunc->mFlags = flags;
  2590. for (auto arg : genericArgs)
  2591. {
  2592. BF_ASSERT(arg != NULL);
  2593. dbgFunc->mGenericArgs.Add((BeDbgType*)arg);
  2594. }
  2595. for (auto genericConstValue : genericConstValueArgs)
  2596. dbgFunc->mGenericConstValueArgs.Add(genericConstValue);
  2597. if (dbgFunc->mValue != NULL)
  2598. dbgFunc->mValue->mDbgFunction = dbgFunc;
  2599. mBeModule->mDbgModule->mFuncs.push_back(dbgFunc);
  2600. SetResult(curId, dbgFunc);
  2601. }
  2602. break;
  2603. case BfIRCmd_DbgCreateFunction:
  2604. {
  2605. CMD_PARAM(BeMDNode*, context);
  2606. CMD_PARAM(String, name);
  2607. CMD_PARAM(String, linkageName);
  2608. CMD_PARAM(BeMDNode*, file);
  2609. CMD_PARAM(int, lineNum);
  2610. CMD_PARAM(BeMDNode*, type);
  2611. CMD_PARAM(bool, isLocalToUnit);
  2612. CMD_PARAM(bool, isDefinition);
  2613. CMD_PARAM(int, scopeLine);
  2614. CMD_PARAM(int, flags);
  2615. CMD_PARAM(bool, isOptimized);
  2616. CMD_PARAM(BeValue*, fn);
  2617. auto dbgFunc = mBeModule->mOwnedValues.Alloc<BeDbgFunction>();
  2618. dbgFunc->mScope = context;
  2619. dbgFunc->mFile = (BeDbgFile*)file;
  2620. dbgFunc->mLine = lineNum - 1;
  2621. dbgFunc->mType = (BeDbgFunctionType*)type;
  2622. dbgFunc->mName = name;
  2623. dbgFunc->mLinkageName = linkageName;
  2624. dbgFunc->mValue = (BeFunction*)fn;
  2625. dbgFunc->mIsLocalToUnit = isLocalToUnit;
  2626. dbgFunc->mFlags = flags;
  2627. /*if (auto dbgStructType = BeValueDynCast<BeDbgStructType>(context))
  2628. {
  2629. // This will get added to the struct later
  2630. }
  2631. else
  2632. {
  2633. }*/
  2634. if (dbgFunc->mValue != NULL)
  2635. dbgFunc->mValue->mDbgFunction = dbgFunc;
  2636. mBeModule->mDbgModule->mFuncs.push_back(dbgFunc);
  2637. SetResult(curId, dbgFunc);
  2638. }
  2639. break;
  2640. case BfIRCmd_DbgCreateParameterVariable:
  2641. {
  2642. CMD_PARAM(BeMDNode*, scope);
  2643. CMD_PARAM(String, name);
  2644. CMD_PARAM(int, argNo);
  2645. CMD_PARAM(BeMDNode*, file);
  2646. CMD_PARAM(int, lineNum);
  2647. CMD_PARAM(BeMDNode*, type);
  2648. CMD_PARAM(bool, alwaysPreserve);
  2649. CMD_PARAM(int, flags);
  2650. auto dbgFunc = (BeDbgFunction*)scope;
  2651. auto dbgVar = mBeModule->mOwnedValues.Alloc<BeDbgVariable>();
  2652. dbgVar->mName = name;
  2653. dbgVar->mType = (BeDbgType*)type;
  2654. dbgVar->mParamNum = argNo - 1;
  2655. int argIdx = argNo - 1;
  2656. //for (int i = ; i <= argNo - 1; i++)
  2657. while (argIdx >= (int)dbgFunc->mVariables.size())
  2658. dbgFunc->mVariables.push_back(NULL);
  2659. if (dbgFunc->mVariables[argIdx] == NULL)
  2660. dbgFunc->mVariables[argIdx] = dbgVar;
  2661. else
  2662. {
  2663. BF_ASSERT(dbgFunc->mVariables[argIdx]->mParamNum == -1);
  2664. dbgFunc->mVariables.Insert(argIdx, dbgVar);
  2665. }
  2666. //mActiveFunction->mDbgFunction->mVariables.push_back(dbgVar);
  2667. //dbgVar->mValue = mBeModule->GetArgument(argNo - 1);
  2668. SetResult(curId, dbgVar);
  2669. }
  2670. break;
  2671. case BfIRCmd_DbgCreateSubroutineType:
  2672. {
  2673. CMD_PARAM(CmdParamVec<BeMDNode*>, elements);
  2674. auto dbgFuncType = mBeModule->mOwnedValues.Alloc<BeDbgFunctionType>();
  2675. if (!elements.empty())
  2676. {
  2677. dbgFuncType->mReturnType = (BeDbgType*)elements[0];
  2678. for (int i = 1; i < (int)elements.size(); i++)
  2679. dbgFuncType->mParams.push_back((BeDbgType*)elements[i]);
  2680. }
  2681. SetResult(curId, dbgFuncType);
  2682. }
  2683. break;
  2684. case BfIRCmd_DbgCreateAutoVariable:
  2685. {
  2686. CMD_PARAM(BeMDNode*, scope);
  2687. CMD_PARAM(String, name);
  2688. CMD_PARAM(BeMDNode*, file);
  2689. CMD_PARAM(int, lineNo);
  2690. CMD_PARAM(BeMDNode*, type);
  2691. CMD_PARAM(int, initType);
  2692. auto dbgVar = mBeModule->mOwnedValues.Alloc<BeDbgVariable>();
  2693. dbgVar->mName = name;
  2694. dbgVar->mType = (BeDbgType*)type;
  2695. dbgVar->mScope = scope;
  2696. dbgVar->mInitType = (BfIRInitType)initType;
  2697. mActiveFunction->mDbgFunction->mVariables.push_back(dbgVar);
  2698. BF_ASSERT(name != "__CRASH_AUTOVARIABLE__");
  2699. SetResult(curId, dbgVar);
  2700. }
  2701. break;
  2702. case BfIRCmd_DbgInsertValueIntrinsic:
  2703. {
  2704. CMD_PARAM(BeValue*, val);
  2705. CMD_PARAM(BeMDNode*, varInfo);
  2706. auto dbgVar = BeValueDynCast<BeDbgVariable>(varInfo);
  2707. //dbgVar->mValue = val;
  2708. //dbgVar->mDeclDbgLoc = mBeModule->mCurDbgLoc;
  2709. if (val == NULL)
  2710. {
  2711. val = mBeModule->GetConstant(mBeContext->GetPrimitiveType(BeTypeCode_Int32), (int64)0);
  2712. }
  2713. auto inst = mBeModule->AllocInst<BeDbgDeclareInst>();
  2714. inst->mValue = val;
  2715. inst->mDbgVar = dbgVar;
  2716. inst->mIsValue = true;
  2717. SetResult(curId, inst);
  2718. }
  2719. break;
  2720. case BfIRCmd_DbgInsertDeclare:
  2721. {
  2722. CMD_PARAM(BeValue*, val);
  2723. CMD_PARAM(BeMDNode*, varInfo);
  2724. CMD_PARAM(BeValue*, insertBefore);
  2725. auto dbgVar = BeValueDynCast<BeDbgVariable>(varInfo);
  2726. //dbgVar->mValue = val;
  2727. //dbgVar->mDeclDbgLoc = mBeModule->mCurDbgLoc;
  2728. auto inst = mBeModule->mAlloc.Alloc<BeDbgDeclareInst>();
  2729. inst->mValue = val;
  2730. inst->mDbgVar = dbgVar;
  2731. inst->mIsValue = false;
  2732. if (insertBefore == NULL)
  2733. mBeModule->AddInst(inst);
  2734. else
  2735. NotImpl();
  2736. SetResult(curId, inst);
  2737. }
  2738. break;
  2739. case BfIRCmd_DbgLifetimeEnd:
  2740. {
  2741. CMD_PARAM(BeMDNode*, varInfo);
  2742. auto inst = mBeModule->AllocInst<BeLifetimeEndInst>();
  2743. inst->mPtr = varInfo;
  2744. }
  2745. break;
  2746. case BfIRCmd_DbgCreateGlobalVariable:
  2747. {
  2748. CMD_PARAM(BeMDNode*, context);
  2749. CMD_PARAM(String, name);
  2750. CMD_PARAM(String, linkageName);
  2751. CMD_PARAM(BeMDNode*, file);
  2752. CMD_PARAM(int, lineNum);
  2753. CMD_PARAM(BeMDNode*, type);
  2754. CMD_PARAM(bool, isLocalToUnit);
  2755. CMD_PARAM(BeConstant*, val);
  2756. CMD_PARAM(BeMDNode*, decl);
  2757. auto dbgGlobalVariable = mBeModule->mDbgModule->mGlobalVariables.Alloc();
  2758. dbgGlobalVariable->mContext = context;
  2759. dbgGlobalVariable->mName = name;
  2760. dbgGlobalVariable->mLinkageName = linkageName;
  2761. dbgGlobalVariable->mFile = (BeDbgFile*)file;
  2762. dbgGlobalVariable->mLineNum = lineNum;
  2763. dbgGlobalVariable->mType = (BeDbgType*)type;
  2764. dbgGlobalVariable->mIsLocalToUnit = isLocalToUnit;
  2765. dbgGlobalVariable->mValue = val;
  2766. dbgGlobalVariable->mDecl = decl;
  2767. SetResult(curId, dbgGlobalVariable);
  2768. }
  2769. break;
  2770. case BfIRCmd_DbgCreateLexicalBlock:
  2771. {
  2772. CMD_PARAM(BeMDNode*, scope);
  2773. CMD_PARAM(BeMDNode*, file);
  2774. CMD_PARAM(int, lineNum);
  2775. CMD_PARAM(int, col);
  2776. auto dbgLexicalBlock = mBeModule->mOwnedValues.Alloc<BeDbgLexicalBlock>();
  2777. BF_ASSERT(BeValueDynCast<BeDbgFile>(file) != NULL);
  2778. dbgLexicalBlock->mFile = (BeDbgFile*)file;
  2779. dbgLexicalBlock->mScope = scope;
  2780. dbgLexicalBlock->mId = mBeModule->mCurLexBlockId++;
  2781. SetResult(curId, dbgLexicalBlock);
  2782. }
  2783. break;
  2784. case BfIRCmd_DbgCreateAnnotation:
  2785. {
  2786. CMD_PARAM(BeMDNode*, scope);
  2787. CMD_PARAM(String, name);
  2788. CMD_PARAM(BeValue*, value);
  2789. if (auto dbgFunc = BeValueDynCast<BeDbgFunction>(scope))
  2790. {
  2791. auto beType = value->GetType();
  2792. BeDbgType** dbgTypePtr;
  2793. if (mOnDemandTypeMap.TryAdd(beType, NULL, &dbgTypePtr))
  2794. {
  2795. auto dbgType = mBeModule->mDbgModule->mTypes.Alloc<BeDbgBasicType>();
  2796. dbgType->mSize = beType->mSize;
  2797. dbgType->mAlign = beType->mAlign;
  2798. dbgType->mEncoding = llvm::dwarf::DW_ATE_signed;
  2799. *dbgTypePtr = dbgType;
  2800. }
  2801. auto dbgVar = mBeModule->mOwnedValues.Alloc<BeDbgVariable>();
  2802. dbgVar->mName = "#" + name;
  2803. dbgVar->mType = *dbgTypePtr;
  2804. dbgVar->mValue = value;
  2805. dbgVar->mScope = scope;
  2806. dbgFunc->mVariables.Add(dbgVar);
  2807. auto inst = mBeModule->AllocInst<BeDbgDeclareInst>();
  2808. inst->mValue = value;
  2809. inst->mDbgVar = dbgVar;
  2810. inst->mIsValue = true;
  2811. }
  2812. else
  2813. NotImpl();
  2814. }
  2815. break;
  2816. default:
  2817. BF_FATAL("Unhandled");
  2818. break;
  2819. }
  2820. #ifdef CODEGEN_TRACK
  2821. gBEMemReporter.EndSection();
  2822. gBEMemReporterSize += mStream->GetReadPos() - curId;
  2823. #endif
  2824. }
  2825. void BeIRCodeGen::SetConfigConst(int idx, int value)
  2826. {
  2827. BF_ASSERT(idx == (int)mConfigConsts.size());
  2828. mConfigConsts.Add(value);
  2829. }
  2830. BeValue* BeIRCodeGen::GetBeValue(int id)
  2831. {
  2832. auto& result = mResults[id];
  2833. BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Value);
  2834. return result.mBeValue;
  2835. }
  2836. BeType* BeIRCodeGen::GetBeType(int id)
  2837. {
  2838. auto& result = mResults[id];
  2839. BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Type);
  2840. return result.mBeType;
  2841. }
  2842. BeBlock* BeIRCodeGen::GetBeBlock(int id)
  2843. {
  2844. auto& result = mResults[id];
  2845. BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Block);
  2846. return result.mBeBlock;
  2847. }
  2848. BeMDNode* BeIRCodeGen::GetBeMetadata(int id)
  2849. {
  2850. auto& result = mResults[id];
  2851. BF_ASSERT(result.mKind == BeIRCodeGenEntryKind_Metadata);
  2852. return result.mBeMetadata;
  2853. }
  2854. BeType* BeIRCodeGen::GetBeTypeById(int id)
  2855. {
  2856. return GetTypeEntry(id).mBeType;
  2857. }