BeIRCodeGen.cpp 79 KB

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