BfIRBuilder.h 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435
  1. #pragma once
  2. //#define BFIR_RENTRY_CHECK
  3. #include "BeefySysLib/Common.h"
  4. #include "BeefySysLib/util/BumpAllocator.h"
  5. #include "BeefySysLib/util/ChunkedDataBuffer.h"
  6. #include "BeefySysLib/util/Dictionary.h"
  7. #include "BfAstAllocator.h"
  8. #include <unordered_map>
  9. //#include "BfSystem.h"
  10. #pragma warning(push)
  11. #pragma warning(disable:4141)
  12. #pragma warning(disable:4146)
  13. #pragma warning(disable:4291)
  14. #pragma warning(disable:4244)
  15. #pragma warning(disable:4267)
  16. #pragma warning(disable:4624)
  17. #pragma warning(disable:4800)
  18. #pragma warning(disable:4996)
  19. #include "llvm/IR/DebugLoc.h"
  20. #include "llvm/IR/IRBuilder.h"
  21. #pragma warning(pop)
  22. NS_BF_BEGIN
  23. typedef int addr_ce;
  24. class BfModule;
  25. class BfType;
  26. class BfTypeInstance;
  27. class BfIRConstHolder;
  28. class BfIRCodeGen;
  29. class BeIRCodeGen;
  30. class BfMethodInstance;
  31. class BfFieldInstance;
  32. class BfMethodRef;
  33. class BfFileInstance;
  34. class BfParser;
  35. class BfParserData;
  36. class Val128;
  37. class BfFilePosition
  38. {
  39. public:
  40. BfFileInstance* mFileInstance;
  41. int mCurSrcPos;
  42. int mCurLine;
  43. int mCurColumn;
  44. public:
  45. BfFilePosition()
  46. {
  47. mFileInstance = NULL;
  48. mCurSrcPos = 0;
  49. mCurLine = 0;
  50. mCurColumn = 0;
  51. }
  52. };
  53. enum BfTypeCode : uint8
  54. {
  55. BfTypeCode_None,
  56. BfTypeCode_CharPtr,
  57. BfTypeCode_StringId,
  58. BfTypeCode_Pointer,
  59. BfTypeCode_NullPtr,
  60. BfTypeCode_Self,
  61. BfTypeCode_Dot,
  62. BfTypeCode_Var,
  63. BfTypeCode_Let,
  64. BfTypeCode_Boolean,
  65. BfTypeCode_Int8,
  66. BfTypeCode_UInt8,
  67. BfTypeCode_Int16,
  68. BfTypeCode_UInt16,
  69. BfTypeCode_Int24,
  70. BfTypeCode_UInt24,
  71. BfTypeCode_Int32,
  72. BfTypeCode_UInt32,
  73. BfTypeCode_Int40,
  74. BfTypeCode_UInt40,
  75. BfTypeCode_Int48,
  76. BfTypeCode_UInt48,
  77. BfTypeCode_Int56,
  78. BfTypeCode_UInt56,
  79. BfTypeCode_Int64,
  80. BfTypeCode_UInt64,
  81. BfTypeCode_Int128,
  82. BfTypeCode_UInt128,
  83. BfTypeCode_IntPtr,
  84. BfTypeCode_UIntPtr,
  85. BfTypeCode_IntUnknown,
  86. BfTypeCode_UIntUnknown,
  87. BfTypeCode_Char8,
  88. BfTypeCode_Char16,
  89. BfTypeCode_Char32,
  90. BfTypeCode_Float,
  91. BfTypeCode_Double,
  92. BfTypeCode_Float2,
  93. BfTypeCode_Object,
  94. BfTypeCode_Interface,
  95. BfTypeCode_Struct,
  96. BfTypeCode_Enum,
  97. BfTypeCode_TypeAlias,
  98. BfTypeCode_Extension,
  99. BfTypeCode_FloatX2,
  100. BfTypeCode_FloatX3,
  101. BfTypeCode_FloatX4,
  102. BfTypeCode_DoubleX2,
  103. BfTypeCode_DoubleX3,
  104. BfTypeCode_DoubleX4,
  105. BfTypeCode_Int64X2,
  106. BfTypeCode_Int64X3,
  107. BfTypeCode_Int64X4,
  108. BfTypeCode_Length,
  109. BfTypeCode_Inferred
  110. };
  111. enum BfConstType
  112. {
  113. BfConstType_GlobalVar = BfTypeCode_Length,
  114. BfConstType_BitCast,
  115. BfConstType_BitCastNull,
  116. BfConstType_GEP32_1,
  117. BfConstType_GEP32_2,
  118. BfConstType_ExtractValue,
  119. BfConstType_PtrToInt,
  120. BfConstType_IntToPtr,
  121. BfConstType_TypeOf,
  122. BfConstType_TypeOf_WithData,
  123. BfConstType_TypeOf_Comptime,
  124. BfConstType_AggZero,
  125. BfConstType_Agg,
  126. BfConstType_AggCE,
  127. BfConstType_ArrayZero,
  128. BfConstType_ArrayZero8,
  129. BfConstType_Undef,
  130. BfConstType_SizedArrayType,
  131. BfConstType_Box
  132. };
  133. enum BfIRValueFlags : uint8
  134. {
  135. BfIRValueFlags_None,
  136. BfIRValueFlags_Value = 1,
  137. BfIRValueFlags_Arg = 2,
  138. BfIRValueFlags_Const = 4,
  139. BfIRValueFlags_FromLLVM = 8,
  140. BfIRValueFlags_Block = 16,
  141. BfIRValueFlags_Func = 32
  142. };
  143. enum BfIRCmd : uint8
  144. {
  145. BfIRCmd_Module_Start,
  146. BfIRCmd_Module_SetTargetTriple,
  147. BfIRCmd_Module_AddModuleFlag,
  148. BfIRCmd_WriteIR,
  149. BfIRCmd_Abort,
  150. BfIRCmd_SetType,
  151. BfIRCmd_SetInstType,
  152. BfIRCmd_PrimitiveType,
  153. BfIRCmd_CreateAnonymousStruct,
  154. BfIRCmd_CreateStruct,
  155. BfIRCmd_StructSetBody,
  156. BfIRCmd_Type,
  157. BfIRCmd_TypeInst,
  158. BfIRCmd_TypeInstPtr,
  159. BfIRCmd_GetType,
  160. BfIRCmd_GetPointerToFuncType,
  161. BfIRCmd_GetPointerToType,
  162. BfIRCmd_GetSizedArrayType,
  163. BfIRCmd_GetVectorType,
  164. BfIRCmd_CreateConstStructZero,
  165. BfIRCmd_CreateConstAgg,
  166. BfIRCmd_CreateConstArrayZero,
  167. BfIRCmd_CreateConstString,
  168. BfIRCmd_ConfigConst,
  169. BfIRCmd_SetName,
  170. BfIRCmd_CreateUndefValue,
  171. BfIRCmd_NumericCast,
  172. BfIRCmd_CmpEQ,
  173. BfIRCmd_CmpNE,
  174. BfIRCmd_CmpSLT,
  175. BfIRCmd_CmpULT,
  176. BfIRCmd_CmpSLE,
  177. BfIRCmd_CmpULE,
  178. BfIRCmd_CmpSGT,
  179. BfIRCmd_CmpUGT,
  180. BfIRCmd_CmpSGE,
  181. BfIRCmd_CmpUGE,
  182. BfIRCmd_Add,
  183. BfIRCmd_Sub,
  184. BfIRCmd_Mul,
  185. BfIRCmd_SDiv,
  186. BfIRCmd_UDiv,
  187. BfIRCmd_SRem,
  188. BfIRCmd_URem,
  189. BfIRCmd_And,
  190. BfIRCmd_Or,
  191. BfIRCmd_Xor,
  192. BfIRCmd_Shl,
  193. BfIRCmd_AShr,
  194. BfIRCmd_LShr,
  195. BfIRCmd_Neg,
  196. BfIRCmd_Not,
  197. BfIRCmd_BitCast,
  198. BfIRCmd_PtrToInt,
  199. BfIRCmd_IntToPtr,
  200. BfIRCmd_InboundsGEP1_32,
  201. BfIRCmd_InboundsGEP2_32,
  202. BfIRCmd_InBoundsGEP1,
  203. BfIRCmd_InBoundsGEP2,
  204. BfIRCmd_IsNull,
  205. BfIRCmd_IsNotNull,
  206. BfIRCmd_ExtractValue,
  207. BfIRCmd_InsertValue,
  208. BfIRCmd_Alloca,
  209. BfIRCmd_AllocaArray,
  210. BfIRCmd_SetAllocaAlignment,
  211. BfIRCmd_SetAllocaNoChkStkHint,
  212. BfIRCmd_SetAllocaForceMem,
  213. BfIRCmd_AliasValue,
  214. BfIRCmd_LifetimeStart,
  215. BfIRCmd_LifetimeEnd,
  216. BfIRCmd_LifetimeSoftEnd,
  217. BfIRCmd_LifetimeExtend,
  218. BfIRCmd_ValueScopeStart,
  219. BfIRCmd_ValueScopeRetain,
  220. BfIRCmd_ValueScopeSoftEnd,
  221. BfIRCmd_ValueScopeHardEnd,
  222. BfIRCmd_Load,
  223. BfIRCmd_AlignedLoad,
  224. BfIRCmd_Store,
  225. BfIRCmd_AlignedStore,
  226. BfIRCmd_MemSet,
  227. BfIRCmd_Fence,
  228. BfIRCmd_StackSave,
  229. BfIRCmd_StackRestore,
  230. BfIRCmd_GlobalVariable,
  231. BfIRCmd_GlobalVar_SetUnnamedAddr,
  232. BfIRCmd_GlobalVar_SetInitializer,
  233. BfIRCmd_GlobalVar_SetAlignment,
  234. BfIRCmd_GlobalVar_SetStorageKind,
  235. BfIRCmd_GlobalStringPtr,
  236. BfIRCmd_SetReflectTypeData,
  237. BfIRCmd_CreateBlock,
  238. BfIRCmd_MaybeChainNewBlock,
  239. BfIRCmd_AddBlock,
  240. BfIRCmd_DropBlocks,
  241. BfIRCmd_MergeBlockDown,
  242. BfIRCmd_GetInsertBlock,
  243. BfIRCmd_SetInsertPoint,
  244. BfIRCmd_SetInsertPointAtStart,
  245. BfIRCmd_EraseFromParent,
  246. BfIRCmd_DeleteBlock,
  247. BfIRCmd_EraseInstFromParent,
  248. BfIRCmd_CreateBr,
  249. BfIRCmd_CreateBr_Fake,
  250. BfIRCmd_CreateBr_NoCollapse,
  251. BfIRCmd_CreateCondBr,
  252. BfIRCmd_MoveBlockToEnd,
  253. BfIRCmd_CreateSwitch,
  254. BfIRCmd_AddSwitchCase,
  255. BfIRCmd_SetSwitchDefaultDest,
  256. BfIRCmd_CreatePhi,
  257. BfIRCmd_AddPhiIncoming,
  258. BfIRCmd_GetIntrinsic,
  259. BfIRCmd_CreateFunctionType,
  260. BfIRCmd_CreateFunction,
  261. BfIRCmd_SetFunctionName,
  262. BfIRCmd_EnsureFunctionPatchable,
  263. BfIRCmd_RemapBindFunction,
  264. BfIRCmd_SetActiveFunction,
  265. BfIRCmd_CreateCall,
  266. BfIRCmd_SetCallCallingConv,
  267. BfIRCmd_SetFuncCallingConv,
  268. BfIRCmd_SetTailCall,
  269. BfIRCmd_SetCallAttribute,
  270. BfIRCmd_CreateRet,
  271. BfIRCmd_CreateSetRet,
  272. BfIRCmd_CreateRetVoid,
  273. BfIRCmd_CreateUnreachable,
  274. BfIRCmd_Call_AddAttribute,
  275. BfIRCmd_Call_AddAttribute1,
  276. BfIRCmd_Func_AddAttribute,
  277. BfIRCmd_Func_AddAttribute1,
  278. BfIRCmd_Func_SetParamName,
  279. BfIRCmd_Func_DeleteBody,
  280. BfIRCmd_Func_SafeRename,
  281. BfIRCmd_Func_SafeRenameFrom,
  282. BfIRCmd_Func_SetLinkage,
  283. BfIRCmd_Comptime_Error,
  284. BfIRCmd_Comptime_GetBfType,
  285. BfIRCmd_Comptime_GetReflectType,
  286. BfIRCmd_Comptime_DynamicCastCheck,
  287. BfIRCmd_Comptime_GetVirtualFunc,
  288. BfIRCmd_Comptime_GetInterfaceFunc,
  289. BfIRCmd_SaveDebugLocation,
  290. BfIRCmd_RestoreDebugLocation,
  291. BfIRCmd_DupDebugLocation,
  292. BfIRCmd_ClearDebugLocation,
  293. BfIRCmd_ClearDebugLocationInst,
  294. BfIRCmd_ClearDebugLocationInstLast,
  295. BfIRCmd_UpdateDebugLocation,
  296. BfIRCmd_SetCurrentDebugLocation,
  297. BfIRCmd_Nop,
  298. BfIRCmd_EnsureInstructionAt,
  299. BfIRCmd_StatementStart,
  300. BfIRCmd_ObjectAccessCheck,
  301. BfIRCmd_DbgInit,
  302. BfIRCmd_DbgFinalize,
  303. BfIRCmd_DbgCreateCompileUnit,
  304. BfIRCmd_DbgCreateFile,
  305. BfIRCmd_ConstValueI64,
  306. BfIRCmd_DbgGetCurrentLocation,
  307. BfIRCmd_DbgSetType,
  308. BfIRCmd_DbgSetInstType,
  309. BfIRCmd_DbgGetType,
  310. BfIRCmd_DbgGetTypeInst,
  311. BfIRCmd_DbgTrackDITypes,
  312. BfIRCmd_DbgCreateNamespace,
  313. BfIRCmd_DbgCreateImportedModule,
  314. BfIRCmd_DbgCreateBasicType,
  315. BfIRCmd_DbgCreateStructType,
  316. BfIRCmd_DbgCreateEnumerationType,
  317. BfIRCmd_DbgCreatePointerType,
  318. BfIRCmd_DbgCreateReferenceType,
  319. BfIRCmd_DbgCreateConstType,
  320. BfIRCmd_DbgCreateArtificialType,
  321. BfIRCmd_DbgCreateArrayType,
  322. BfIRCmd_DbgCreateReplaceableCompositeType,
  323. BfIRCmd_DbgCreateForwardDecl,
  324. BfIRCmd_DbgCreateSizedForwardDecl,
  325. BeIRCmd_DbgSetTypeSize,
  326. BfIRCmd_DbgReplaceAllUses,
  327. BfIRCmd_DbgDeleteTemporary,
  328. BfIRCmd_DbgMakePermanent,
  329. BfIRCmd_CreateEnumerator,
  330. BfIRCmd_DbgCreateMemberType,
  331. BfIRCmd_DbgStaticCreateMemberType,
  332. BfIRCmd_DbgCreateInheritance,
  333. BfIRCmd_DbgCreateMethod,
  334. BfIRCmd_DbgCreateFunction,
  335. BfIRCmd_DbgCreateParameterVariable,
  336. BfIRCmd_DbgCreateSubroutineType,
  337. BfIRCmd_DbgCreateAutoVariable,
  338. BfIRCmd_DbgInsertValueIntrinsic,
  339. BfIRCmd_DbgInsertDeclare,
  340. BfIRCmd_DbgLifetimeEnd,
  341. BfIRCmd_DbgCreateGlobalVariable,
  342. BfIRCmd_DbgCreateLexicalBlock,
  343. BfIRCmd_DbgCreateLexicalBlockFile,
  344. BfIRCmd_DbgCreateAnnotation,
  345. BfIRCmd_COUNT
  346. };
  347. enum BfIRCallingConv
  348. {
  349. BfIRCallingConv_ThisCall,
  350. BfIRCallingConv_StdCall,
  351. BfIRCallingConv_CDecl,
  352. BfIRCallingConv_FastCall
  353. };
  354. enum BfIRParamType : uint8
  355. {
  356. BfIRParamType_None,
  357. BfIRParamType_Const,
  358. BfIRParamType_Arg,
  359. BfIRParamType_StreamId_Abs8,
  360. BfIRParamType_StreamId_Rel,
  361. BfIRParamType_StreamId_Back1,
  362. BfIRParamType_StreamId_Back_LAST = 0xFF, // Use remaining encoding
  363. };
  364. enum BfIRLinkageType : uint8
  365. {
  366. BfIRLinkageType_External,
  367. BfIRLinkageType_Internal
  368. };
  369. enum BfIRInitType : uint8
  370. {
  371. BfIRInitType_NotSet, // Not specified
  372. BfIRInitType_NotNeeded, // Explicitly disable
  373. BfIRInitType_NotNeeded_AliveOnDecl, // Explicitly disable, treat variable as alive at declaration
  374. BfIRInitType_Uninitialized, // May set to 0xCC for debug
  375. BfIRInitType_Zero, // Must be zero
  376. };
  377. enum BfIRFenceType : uint8
  378. {
  379. BfIRFenceType_AcquireRelease
  380. };
  381. enum BfIRConfigConst : uint8
  382. {
  383. BfIRConfigConst_VirtualMethodOfs,
  384. BfIRConfigConst_DynSlotOfs
  385. };
  386. enum BfIRIntrinsic : uint8
  387. {
  388. BfIRIntrinsic__PLATFORM,
  389. BfIRIntrinsic_Abs,
  390. BfIRIntrinsic_Add,
  391. BfIRIntrinsic_And,
  392. BfIRIntrinsic_AtomicAdd,
  393. BfIRIntrinsic_AtomicAnd,
  394. BfIRIntrinsic_AtomicCmpStore,
  395. BfIRIntrinsic_AtomicCmpStore_Weak,
  396. BfIRIntrinsic_AtomicCmpXChg,
  397. BfIRIntrinsic_AtomicFence,
  398. BfIRIntrinsic_AtomicLoad,
  399. BfIRIntrinsic_AtomicMax,
  400. BfIRIntrinsic_AtomicMin,
  401. BfIRIntrinsic_AtomicNAnd,
  402. BfIRIntrinsic_AtomicOr,
  403. BfIRIntrinsic_AtomicStore,
  404. BfIRIntrinsic_AtomicSub,
  405. BfIRIntrinsic_AtomicUMax,
  406. BfIRIntrinsic_AtomicUMin,
  407. BfIRIntrinsic_AtomicXChg,
  408. BfIRIntrinsic_AtomicXor,
  409. BfIRIntrinsic_BSwap,
  410. BfIRIntrinsic_Cast,
  411. BfIRIntrinsic_Cos,
  412. BfIRIntrinsic_Cpuid,
  413. BfIRIntrinsic_DebugTrap,
  414. BfIRIntrinsic_Div,
  415. BfIRIntrinsic_Eq,
  416. BfIRIntrinsic_Floor,
  417. BfIRIntrinsic_Free,
  418. BfIRIntrinsic_Gt,
  419. BfIRIntrinsic_GtE,
  420. BfIRIntrinsic_Index,
  421. BfIRIntrinsic_Log,
  422. BfIRIntrinsic_Log10,
  423. BfIRIntrinsic_Log2,
  424. BfIRIntrinsic_Lt,
  425. BfIRIntrinsic_LtE,
  426. BfIRIntrinsic_Malloc,
  427. BfIRIntrinsic_Max,
  428. BfIRIntrinsic_MemCpy,
  429. BfIRIntrinsic_MemMove,
  430. BfIRIntrinsic_MemSet,
  431. BfIRIntrinsic_Min,
  432. BfIRIntrinsic_Mod,
  433. BfIRIntrinsic_Mul,
  434. BfIRIntrinsic_Neq,
  435. BfIRIntrinsic_Not,
  436. BfIRIntrinsic_Or,
  437. BfIRIntrinsic_Pow,
  438. BfIRIntrinsic_PowI,
  439. BfIRIntrinsic_ReturnAddress,
  440. BfIRIntrinsic_Round,
  441. BfIRIntrinsic_SAR,
  442. BfIRIntrinsic_SHL,
  443. BfIRIntrinsic_SHR,
  444. BfIRIntrinsic_Shuffle,
  445. BfIRIntrinsic_Sin,
  446. BfIRIntrinsic_Sqrt,
  447. BfIRIntrinsic_Sub,
  448. BfIRIntrinsic_VAArg,
  449. BfIRIntrinsic_VAEnd,
  450. BfIRIntrinsic_VAStart,
  451. BfIRIntrinsic_Xgetbv,
  452. BfIRIntrinsic_Xor,
  453. BfIRIntrinsic_COUNT,
  454. BfIRIntrinsic_Atomic_FIRST = BfIRIntrinsic_AtomicAdd,
  455. BfIRIntrinsic_Atomic_LAST = BfIRIntrinsic_AtomicXor
  456. };
  457. enum BfIRAtomicOrdering : uint8
  458. {
  459. BfIRAtomicOrdering_Unordered,
  460. BfIRAtomicOrdering_Relaxed,
  461. BfIRAtomicOrdering_Acquire,
  462. BfIRAtomicOrdering_Release,
  463. BfIRAtomicOrdering_AcqRel,
  464. BfIRAtomicOrdering_SeqCst,
  465. BfIRAtomicOrdering_ORDERMASK = 7,
  466. BfIRAtomicOrdering_Volatile = 8,
  467. BfIRAtomicOrdering_ReturnModified = 0x10 // Generally atomic instructions return original value, this overrides that
  468. };
  469. enum BfIRStorageKind : uint8
  470. {
  471. BfIRStorageKind_Normal,
  472. BfIRStorageKind_Import,
  473. BfIRStorageKind_Export
  474. };
  475. //#define CHECK_CONSTHOLDER
  476. struct BfIRRawValue
  477. {
  478. public:
  479. int mId;
  480. BfIRRawValue()
  481. {
  482. mId = -1;
  483. }
  484. operator bool() const
  485. {
  486. return mId != -1;
  487. }
  488. bool IsFake() const
  489. {
  490. return mId < -1;
  491. }
  492. };
  493. struct BfIRValue
  494. {
  495. public:
  496. // Reserved 'fake' Ids
  497. enum
  498. {
  499. ID_IMPLICIT = -3
  500. };
  501. public:
  502. int mId;
  503. BfIRValueFlags mFlags;
  504. static BfIRValue sValueless;
  505. #ifdef CHECK_CONSTHOLDER
  506. BfIRConstHolder* mHolder;
  507. #endif
  508. public:
  509. BfIRValue()
  510. {
  511. mId = -1;
  512. mFlags = BfIRValueFlags_None;
  513. #ifdef CHECK_CONSTHOLDER
  514. mHolder = NULL;
  515. #endif
  516. }
  517. BfIRValue(const BfIRValue& from)
  518. {
  519. mFlags = from.mFlags;
  520. mId = from.mId;
  521. #ifdef CHECK_CONSTHOLDER
  522. mHolder = from.mHolder;
  523. #endif
  524. }
  525. BfIRValue(BfIRValueFlags flags, int id)
  526. {
  527. mFlags = flags;
  528. mId = id;
  529. }
  530. operator bool() const
  531. {
  532. return mFlags != BfIRValueFlags_None;
  533. }
  534. bool IsFake() const;
  535. bool IsConst() const;
  536. bool IsArg() const;
  537. bool IsFromLLVM() const;
  538. bool operator==(const BfIRValue& rhs) const
  539. {
  540. if (mFlags != rhs.mFlags)
  541. return false;
  542. if (mId != rhs.mId)
  543. return false;
  544. return true;
  545. }
  546. bool operator!=(const BfIRValue& rhs) const
  547. {
  548. if (mFlags != rhs.mFlags)
  549. return true;
  550. if (mId != rhs.mId)
  551. return true;
  552. return false;
  553. }
  554. };
  555. struct BfIRTypeData
  556. {
  557. enum TypeKind
  558. {
  559. TypeKind_None,
  560. TypeKind_TypeId,
  561. TypeKind_TypeCode,
  562. TypeKind_TypeInstId,
  563. TypeKind_TypeInstPtrId,
  564. TypeKind_Stream,
  565. TypeKind_SizedArray
  566. };
  567. TypeKind mKind;
  568. int mId;
  569. operator bool()
  570. {
  571. return (mId != -1);
  572. }
  573. };
  574. struct BfIRType : public BfIRTypeData
  575. {
  576. public:
  577. BfIRType()
  578. {
  579. mKind = TypeKind_None;
  580. mId = -1;
  581. }
  582. BfIRType(BfIRTypeData typeData)
  583. {
  584. mKind = typeData.mKind;
  585. mId = typeData.mId;
  586. }
  587. BfIRType(const BfIRValue& val) { mKind = TypeKind_Stream; mId = val.mId; }
  588. };
  589. struct BfIRBlock : public BfIRValue
  590. {
  591. public:
  592. BfIRBlock();
  593. BfIRBlock(const BfIRValue& fromVal) : BfIRValue(fromVal) {}
  594. };
  595. enum BfIRAttribute
  596. {
  597. BfIRAttribute_NoReturn,
  598. BfIRAttribute_NoAlias,
  599. BfIRAttribute_NoCapture,
  600. BfIRAttribute_StructRet,
  601. BfIRAttribute_VarRet,
  602. BfIRAttribute_ZExt,
  603. BfIRAttribute_ByVal,
  604. BfIRAttribute_Dereferencable,
  605. BFIRAttribute_NoUnwind,
  606. BFIRAttribute_UWTable,
  607. BFIRAttribute_AlwaysInline,
  608. BFIRAttribute_NoFramePointerElim,
  609. BFIRAttribute_DllImport,
  610. BFIRAttribute_DllExport,
  611. BFIRAttribute_NoRecurse,
  612. BFIRAttribute_Constructor,
  613. BFIRAttribute_Destructor,
  614. };
  615. struct BfIRFunctionType
  616. {
  617. public:
  618. int mId;
  619. public:
  620. BfIRFunctionType();
  621. BfIRFunctionType(const BfIRValue& val) { mId = val.mId; }
  622. operator bool() const
  623. {
  624. return mId != -1;
  625. }
  626. };
  627. struct BfIRFunction : public BfIRRawValue
  628. {
  629. public:
  630. BfIRFunction();
  631. BfIRFunction(const BfIRValue& val)
  632. {
  633. BF_ASSERT((val.mFlags == BfIRValueFlags_None) || (val.mFlags == BfIRValueFlags_Value));
  634. mId = val.mId;
  635. }
  636. //: BfIRValue(val) {}
  637. bool operator==(const BfIRFunction& rhs) const
  638. {
  639. if (mId != rhs.mId)
  640. return false;
  641. return true;
  642. }
  643. bool operator!=(const BfIRFunction& rhs) const
  644. {
  645. if (mId == rhs.mId)
  646. return false;
  647. return true;
  648. }
  649. operator bool() const
  650. {
  651. return mId != -1;
  652. }
  653. operator BfIRValue() const
  654. {
  655. return BfIRValue((mId == -1) ? BfIRValueFlags_None : BfIRValueFlags_Value, mId);
  656. }
  657. };
  658. struct BfIRMDNode
  659. {
  660. public:
  661. int mId;
  662. BfIRMDNode()
  663. {
  664. mId = -1;
  665. }
  666. BfIRMDNode(const BfIRValue& val)
  667. {
  668. mId = val.mId;
  669. }
  670. operator bool() const
  671. {
  672. return mId != -1;
  673. }
  674. bool operator==(const BfIRMDNode& rhs) const
  675. {
  676. if (mId != rhs.mId)
  677. return false;
  678. return true;
  679. }
  680. bool operator!=(const BfIRMDNode& rhs) const
  681. {
  682. if (mId == rhs.mId)
  683. return false;
  684. return true;
  685. }
  686. };
  687. class BfFileInstance
  688. {
  689. public:
  690. BfParserData* mParser;
  691. BfIRMDNode mDIFile;
  692. BfFilePosition mPrevPosition;
  693. public:
  694. BfFileInstance()
  695. {
  696. mParser = NULL;
  697. }
  698. };
  699. struct BfGlobalVar
  700. {
  701. BfConstType mConstType;
  702. const char* mName;
  703. BfIRType mType;
  704. bool mIsConst;
  705. BfIRLinkageType mLinkageType;
  706. int mStreamId;
  707. BfIRValue mInitializer;
  708. bool mIsTLS;
  709. int mAlignment;
  710. };
  711. struct BfGlobalVar_TypeInst
  712. {
  713. BfConstType mConstType;
  714. const char* mName;
  715. BfTypeInstance* mTypeInst;
  716. bool mIsConst;
  717. BfIRLinkageType mLinkageType;
  718. int mStreamId;
  719. BfIRValue mInitializer;
  720. bool mIsTLS;
  721. };
  722. struct BfTypeOf_Const
  723. {
  724. BfConstType mConstType;
  725. BfType* mType;
  726. BfIRValue mTypeData;
  727. };
  728. struct BfTypeOf_WithData_Const
  729. {
  730. BfConstType mConstType;
  731. BfType* mType;
  732. BfIRValue mTypeData;
  733. };
  734. struct BfConstant
  735. {
  736. public:
  737. union
  738. {
  739. BfTypeCode mTypeCode;
  740. BfConstType mConstType;
  741. };
  742. union
  743. {
  744. bool mBool;
  745. int64 mInt64;
  746. int32 mInt32;
  747. int16 mInt16;
  748. int8 mInt8;
  749. uint64 mUInt64;
  750. uint32 mUInt32;
  751. uint16 mUInt16;
  752. uint8 mUInt8;
  753. uint8 mChar;
  754. uint32 mChar32;
  755. double mDouble;
  756. BfIRTypeData mIRType;
  757. };
  758. bool IsNull()
  759. {
  760. if (mTypeCode == BfTypeCode_NullPtr)
  761. return true;
  762. if (mConstType == BfConstType_BitCastNull)
  763. return true;
  764. return false;
  765. }
  766. };
  767. struct BfConstantSizedArrayType
  768. {
  769. BfConstType mConstType;
  770. BfIRType mType;
  771. intptr mLength;
  772. };
  773. struct BfConstantUndef
  774. {
  775. BfConstType mConstType;
  776. BfIRType mType;
  777. };
  778. struct BfConstantBitCast
  779. {
  780. BfConstType mConstType;
  781. int mTarget;
  782. BfIRType mToType;
  783. };
  784. struct BfConstantBox
  785. {
  786. BfConstType mConstType;
  787. int mTarget;
  788. BfIRType mToType;
  789. };
  790. struct BfConstantPtrToInt
  791. {
  792. BfConstType mConstType;
  793. int mTarget;
  794. BfTypeCode mToTypeCode;
  795. };
  796. struct BfConstantIntToPtr
  797. {
  798. BfConstType mConstType;
  799. int mTarget;
  800. BfIRType mToType;
  801. };
  802. struct BfConstantGEP32_1
  803. {
  804. BfConstType mConstType;
  805. int mTarget;
  806. int mIdx0;
  807. };
  808. struct BfConstantGEP32_2
  809. {
  810. BfConstType mConstType;
  811. int mTarget;
  812. int mIdx0;
  813. int mIdx1;
  814. };
  815. struct BfConstantExtractValue
  816. {
  817. BfConstType mConstType;
  818. int mTarget;
  819. int mIdx0;
  820. };
  821. struct BfConstantAgg
  822. {
  823. BfConstType mConstType;
  824. BfIRType mType;
  825. BfSizedArray<BfIRValue> mValues;
  826. };
  827. struct BfConstantAggCE
  828. {
  829. BfConstType mConstType;
  830. BfIRType mType;
  831. addr_ce mCEAddr;
  832. };
  833. struct BfConstantArrayZero
  834. {
  835. BfConstType mConstType;
  836. BfIRType mType;
  837. int mCount;
  838. };
  839. class BfIRConstHolder
  840. {
  841. public:
  842. BumpAllocatorT<256> mTempAlloc;
  843. BfModule* mModule;
  844. Dictionary<String, BfIRValue> mGlobalVarMap;
  845. public:
  846. void FixTypeCode(BfTypeCode& typeCode);
  847. int GetSize(BfTypeCode typeCode);
  848. static int GetSize(BfTypeCode typeCode, int ptrSize);
  849. static bool IsInt(BfTypeCode typeCode);
  850. static bool IsChar(BfTypeCode typeCode);
  851. static bool IsIntable(BfTypeCode typeCode);
  852. static bool IsSigned(BfTypeCode typeCode);
  853. static bool IsFloat(BfTypeCode typeCode);
  854. const char* AllocStr(const StringImpl& str);
  855. public:
  856. BfIRConstHolder(BfModule* module);
  857. virtual ~BfIRConstHolder();
  858. String ToString(BfIRValue irValue);
  859. String ToString(BfIRType irType);
  860. void pv(const BfIRValue& irValue);
  861. BfConstant* GetConstantById(int id);
  862. BfConstant* GetConstant(BfIRValue id);
  863. bool TryGetBool(BfIRValue id, bool& boolVal);
  864. int IsZero(BfIRValue val);
  865. bool IsConstValue(BfIRValue val);
  866. int CheckConstEquality(BfIRValue lhs, BfIRValue rhs); // -1 = fail, 0 = false, 1 = true
  867. //void WriteConstant(void* data, BeConstant* constVal);
  868. BfIRType GetSizedArrayType(BfIRType elementType, int length);
  869. BfIRValue CreateConst(BfTypeCode typeCode, uint64 val);
  870. BfIRValue CreateConst(BfTypeCode typeCode, int val);
  871. BfIRValue CreateConst(BfTypeCode typeCode, double val);
  872. BfIRValue CreateConst(BfConstant* fromConst, BfIRConstHolder* fromHolder);
  873. BfIRValue CreateConstNull();
  874. BfIRValue CreateConstNull(BfIRType nullType);
  875. BfIRValue CreateConstAggZero(BfIRType aggType);
  876. BfIRValue CreateConstAgg(BfIRType type, const BfSizedArray<BfIRValue>& values);
  877. BfIRValue CreateConstAggCE(BfIRType type, addr_ce ptr);
  878. BfIRValue CreateConstArrayZero(BfIRType type, int count);
  879. BfIRValue CreateConstArrayZero(int count);
  880. BfIRValue CreateConstBitCast(BfIRValue val, BfIRType type);
  881. BfIRValue CreateConstBox(BfIRValue val, BfIRType type);
  882. BfIRValue CreateTypeOf(BfType* type);
  883. BfIRValue CreateTypeOfComptime(BfType* type);
  884. BfIRValue CreateTypeOf(BfType* type, BfIRValue typeData);
  885. BfIRValue GetUndefConstValue(BfIRType type);
  886. BfIRValue CreateGlobalVariableConstant(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS = false);
  887. bool WriteConstant(BfIRValue val, void* ptr, BfType* type);
  888. BfIRValue ReadConstant(void* ptr, BfType* type);
  889. };
  890. enum BfIRPopulateType
  891. {
  892. BfIRPopulateType_Identity,
  893. BfIRPopulateType_Declaration,
  894. BfIRPopulateType_Eventually_Full,
  895. BfIRPopulateType_Full,
  896. BfIRPopulateType_Full_ForceDefinition
  897. };
  898. struct BfIRState
  899. {
  900. BfIRBlock mActualInsertBlock; // Only when not ignoring writes
  901. BfIRBlock mInsertBlock;
  902. BfIRFunction mActiveFunction;
  903. bool mActiveFunctionHasBody;
  904. Array<BfFilePosition> mSavedDebugLocs;
  905. };
  906. enum BfOverflowCheckKind : int8
  907. {
  908. BfOverflowCheckKind_None = 0,
  909. BfOverflowCheckKind_Signed = 1,
  910. BfOverflowCheckKind_Unsigned = 2,
  911. BfOverflowCheckKind_Flag_UseAsm = 4
  912. };
  913. class BfIRBuilder : public BfIRConstHolder
  914. {
  915. public:
  916. bool mIgnoreWrites;
  917. bool mDbgVerifyCodeGen;
  918. int mCurFakeId;
  919. bool mHasGlobalDefs;
  920. bool mIsBeefBackend;
  921. int mNumFunctionsWithBodies;
  922. int mBlockCount;
  923. bool mHasStarted;
  924. int mCmdCount;
  925. ChunkedDataBuffer mStream;
  926. BfIRBlock mActualInsertBlock; // Only when not ignoring writes
  927. BfIRBlock mInsertBlock;
  928. bool mHasDebugLoc;
  929. bool mHasDebugInfo;
  930. bool mHasDebugLineInfo;
  931. Dictionary<BfMethodRef, BfIRFunctionType> mMethodTypeMap;
  932. Dictionary<StringView, BfIRFunction> mFunctionMap;
  933. Dictionary<BfType*, BfIRPopulateType> mTypeMap;
  934. Dictionary<int, BfIRValue> mConstMemMap;
  935. Array<BfTypeInstance*> mDITemporaryTypes;
  936. BfIRFunction mActiveFunction;
  937. bool mActiveFunctionHasBody;
  938. Array<BfFilePosition> mSavedDebugLocs;
  939. Array<BfType*> mDeferredDbgTypeDefs;
  940. BfIRCodeGenBase* mIRCodeGen;
  941. BfIRCodeGen* mBfIRCodeGen;
  942. BeIRCodeGen* mBeIRCodeGen;
  943. #ifdef BFIR_RENTRY_CHECK
  944. std::set<BfType*> mDeclReentrySet;
  945. std::set<BfType*> mDefReentrySet;
  946. #endif
  947. bool mOpFailed;
  948. public:
  949. ~BfIRBuilder();
  950. void WriteSLEB128(int64 val);
  951. void WriteSLEB128(int32 val);
  952. void Write(uint8 val);
  953. void Write(bool val);
  954. void Write(int val);
  955. void Write(int64 val);
  956. void Write(Val128 val);
  957. void Write(const StringImpl& str);
  958. void Write(const BfIRValue& irValue);
  959. void Write(BfTypeCode typeCode);
  960. void Write(const BfIRTypeData& type);
  961. void Write(BfIRFunctionType func);
  962. void Write(BfIRFunction funcType);
  963. void Write(BfIRBlock block);
  964. void Write(BfIRMDNode node);
  965. template <typename T>
  966. void Write(const BfSizedArray<T>& sizedArray)
  967. {
  968. WriteSLEB128(sizedArray.mSize);
  969. for (int i = 0; i < sizedArray.mSize; i++)
  970. Write(sizedArray.mVals[i]);
  971. }
  972. BfIRValue WriteCmd(BfIRCmd cmd);
  973. template <typename T>
  974. void WriteArg(const T& first)
  975. {
  976. Write(first);
  977. }
  978. template <typename T, typename... Args>
  979. void WriteArg(const T& first, const Args&... args)
  980. {
  981. Write(first);
  982. WriteArg(args...);
  983. }
  984. template <typename... Args>
  985. BfIRValue WriteCmd(BfIRCmd cmd, const Args&... args)
  986. {
  987. if (mIgnoreWrites)
  988. return GetFakeVal();
  989. //int dataPos = mStream.GetSize();
  990. auto result = WriteCmd(cmd);
  991. WriteArg(args...);
  992. return result;
  993. //return BfIRValue(BfIRValueFlags_Value, dataPos);
  994. }
  995. public:
  996. void NewCmdInserted();
  997. BfIRMDNode CreateNamespaceScope(BfType* type, BfIRMDNode fileDIScope);
  998. String GetDebugTypeName(BfTypeInstance* typeInstance, bool includeOuterTypeName);
  999. void CreateDbgTypeDefinition(BfType* type);
  1000. bool WantsDbgDefinition(BfType * type);
  1001. void CreateTypeDeclaration(BfType* type, bool forceDbgDefine);
  1002. void CreateTypeDefinition_Data(BfModule* populateModule, BfTypeInstance* typeInstance, bool forceDbgDefine);
  1003. void CreateTypeDefinition(BfType* type, bool forceDbgDefine);
  1004. void ReplaceDITemporaryTypes();
  1005. void PushDbgLoc(BfTypeInstance* typeInst);
  1006. BfIRPopulateType GetPopulateTypeState(BfType* type);
  1007. void PopulateType(BfType* type, BfIRPopulateType populateType = BfIRPopulateType_Full);
  1008. void SetType(BfType* type, BfIRType irType);
  1009. void SetInstType(BfType* type, BfIRType irType);
  1010. int GetFakeId();
  1011. BfIRValue GetFakeVal();
  1012. BfIRValue GetFakeConst();
  1013. BfIRType GetFakeType();
  1014. BfIRType GetFakeBlock();
  1015. BfIRFunctionType GetFakeFunctionType();
  1016. BfIRFunction GetFakeFunction();
  1017. public:
  1018. void OpFailed();
  1019. uint8 CheckedAdd(uint8 a, uint8 b);
  1020. uint16 CheckedAdd(uint16 a, uint16 b);
  1021. uint32 CheckedAdd(uint32 a, uint32 b);
  1022. uint64 CheckedAdd(uint64 a, uint64 b);
  1023. int8 CheckedAdd(int8 a, int8 b);
  1024. int16 CheckedAdd(int16 a, int16 b);
  1025. int32 CheckedAdd(int32 a, int32 b);
  1026. int64 CheckedAdd(int64 a, int64 b);
  1027. float CheckedAdd(float a, float b) { return a + b; }
  1028. double CheckedAdd(double a, double b) { return a + b; }
  1029. uint8 CheckedSub(uint8 a, uint8 b);
  1030. uint16 CheckedSub(uint16 a, uint16 b);
  1031. uint32 CheckedSub(uint32 a, uint32 b);
  1032. uint64 CheckedSub(uint64 a, uint64 b);
  1033. int8 CheckedSub(int8 a, int8 b);
  1034. int16 CheckedSub(int16 a, int16 b);
  1035. int32 CheckedSub(int32 a, int32 b);
  1036. int64 CheckedSub(int64 a, int64 b);
  1037. float CheckedSub(float a, float b) { return a - b; }
  1038. double CheckedSub(double a, double b) { return a - b; }
  1039. uint8 CheckedMul(uint8 a, uint8 b);
  1040. uint16 CheckedMul(uint16 a, uint16 b);
  1041. uint32 CheckedMul(uint32 a, uint32 b);
  1042. uint64 CheckedMul(uint64 a, uint64 b);
  1043. int8 CheckedMul(int8 a, int8 b);
  1044. int16 CheckedMul(int16 a, int16 b);
  1045. int32 CheckedMul(int32 a, int32 b);
  1046. int64 CheckedMul(int64 a, int64 b);
  1047. float CheckedMul(float a, float b) { return a * b; }
  1048. double CheckedMul(double a, double b) { return a * b; }
  1049. uint8 CheckedShl(uint8 a, uint8 b);
  1050. uint16 CheckedShl(uint16 a, uint16 b);
  1051. uint32 CheckedShl(uint32 a, uint32 b);
  1052. uint64 CheckedShl(uint64 a, uint64 b);
  1053. int8 CheckedShl(int8 a, int8 b);
  1054. int16 CheckedShl(int16 a, int16 b);
  1055. int32 CheckedShl(int32 a, int32 b);
  1056. int64 CheckedShl(int64 a, int64 b);
  1057. public:
  1058. BfIRBuilder(BfModule* module);
  1059. bool HasExports(); // Contains non-empty functions and/or non-empty globals
  1060. String ToString(BfIRValue irValue);
  1061. String ToString(BfIRType irType);
  1062. String ToString(BfIRFunction irFunc);
  1063. String ToString(BfIRFunctionType irType);
  1064. String ToString(BfIRMDNode irMDNode);
  1065. String ActiveFuncToString();
  1066. void PrintActiveFunc();
  1067. void pv(const BfIRValue& irValue);
  1068. void pt(const BfIRType& irType);
  1069. void pbft(BfType* type);
  1070. void pt(const BfIRFunction& irFun);
  1071. void pft(const BfIRFunctionType& irType);
  1072. void pmd(const BfIRMDNode& irMDNode);
  1073. void GetBufferData(Array<uint8>& outBuffer);
  1074. void ClearConstData();
  1075. void ClearNonConstData();
  1076. void Start(const StringImpl& moduleName, int ptrSize, bool isOptimized);
  1077. void SetBackend(bool isBeefBackend);
  1078. void RemoveIRCodeGen();
  1079. void WriteIR(const StringImpl& fileName);
  1080. void AbortStream();
  1081. void Module_SetTargetTriple(const StringImpl& targetTriple, const StringImpl& targetCPU);
  1082. void Module_AddModuleFlag(const StringImpl& flag, int val);
  1083. BfIRType GetPrimitiveType(BfTypeCode typeCode);
  1084. BfIRType CreateStructType(const StringImpl& name);
  1085. BfIRType CreateStructType(const BfSizedArray<BfIRType>& memberTypes);
  1086. void StructSetBody(BfIRType type, const BfSizedArray<BfIRType>& memberTypes, int size, int align, bool isPacked);
  1087. BfIRType MapType(BfType* type, BfIRPopulateType populateType = BfIRPopulateType_Declaration);
  1088. BfIRType MapTypeInst(BfTypeInstance* typeInst, BfIRPopulateType populateType = BfIRPopulateType_Declaration);
  1089. BfIRType MapTypeInstPtr(BfTypeInstance* typeInst);
  1090. BfIRType GetType(BfIRValue val);
  1091. BfIRType GetPointerTo(BfIRFunctionType funcType);
  1092. BfIRType GetPointerTo(BfIRType type);
  1093. BfIRType GetSizedArrayType(BfIRType elementType, int length);
  1094. BfIRType GetVectorType(BfIRType elementType, int length);
  1095. BfIRValue CreateConstAgg_Value(BfIRType type, const BfSizedArray<BfIRValue>& values);
  1096. BfIRValue CreateConstString(const StringImpl& string);
  1097. BfIRValue ConstToMemory(BfIRValue constVal);
  1098. BfIRValue GetConfigConst(BfIRConfigConst constType, BfTypeCode typeCode);
  1099. BfIRValue GetArgument(int argIdx);
  1100. void SetName(BfIRValue val, const StringImpl& name);
  1101. BfIRValue CreateUndefValue(BfIRType type);
  1102. BfIRValue CreateNumericCast(BfIRValue val, bool valIsSigned, BfTypeCode typeCode);
  1103. BfIRValue CreateCmpEQ(BfIRValue lhs, BfIRValue rhs);
  1104. BfIRValue CreateCmpNE(BfIRValue lhs, BfIRValue rhs);
  1105. BfIRValue CreateCmpLT(BfIRValue lhs, BfIRValue rhs, bool isSigned);
  1106. BfIRValue CreateCmpLTE(BfIRValue lhs, BfIRValue rhs, bool isSigned);
  1107. BfIRValue CreateCmpGT(BfIRValue lhs, BfIRValue rhs, bool isSigned);
  1108. BfIRValue CreateCmpGTE(BfIRValue lhs, BfIRValue rhs, bool isSigned);
  1109. BfIRValue CreateAdd(BfIRValue lhs, BfIRValue rhs, BfOverflowCheckKind overflowCheckKind = BfOverflowCheckKind_None);
  1110. BfIRValue CreateSub(BfIRValue lhs, BfIRValue rhs, BfOverflowCheckKind overflowCheckKind = BfOverflowCheckKind_None);
  1111. BfIRValue CreateMul(BfIRValue lhs, BfIRValue rhs, BfOverflowCheckKind overflowCheckKind = BfOverflowCheckKind_None);
  1112. BfIRValue CreateDiv(BfIRValue lhs, BfIRValue rhs, bool isSigned);
  1113. BfIRValue CreateRem(BfIRValue lhs, BfIRValue rhs, bool isSigned);
  1114. BfIRValue CreateAnd(BfIRValue lhs, BfIRValue rhs);
  1115. BfIRValue CreateOr(BfIRValue lhs, BfIRValue rhs);
  1116. BfIRValue CreateXor(BfIRValue lhs, BfIRValue rhs);
  1117. BfIRValue CreateShl(BfIRValue lhs, BfIRValue rhs);
  1118. BfIRValue CreateShr(BfIRValue lhs, BfIRValue rhs, bool isSigned);
  1119. BfIRValue CreateNeg(BfIRValue val);
  1120. BfIRValue CreateNot(BfIRValue val);
  1121. BfIRValue CreateBitCast(BfIRValue val, BfIRType type);
  1122. BfIRValue CreatePtrToInt(BfIRValue val, BfTypeCode typeCode);
  1123. BfIRValue CreateIntToPtr(BfIRValue val, BfIRType type);
  1124. BfIRValue CreateIntToPtr(uint64 val, BfIRType type);
  1125. BfIRValue CreateInBoundsGEP(BfIRValue val, int idx0);
  1126. BfIRValue CreateInBoundsGEP(BfIRValue val, int idx0, int idx1);
  1127. BfIRValue CreateInBoundsGEP(BfIRValue val, BfIRValue idx0);
  1128. BfIRValue CreateInBoundsGEP(BfIRValue val, BfIRValue idx0, BfIRValue idx1);
  1129. BfIRValue CreateIsNull(BfIRValue val);
  1130. BfIRValue CreateIsNotNull(BfIRValue val);
  1131. BfIRValue CreateExtractValue(BfIRValue val, int idx);
  1132. BfIRValue CreateExtractValue(BfIRValue val, BfIRValue idx);
  1133. BfIRValue CreateInsertValue(BfIRValue agg, BfIRValue val, int idx);
  1134. BfIRValue CreateAlloca(BfIRType type);
  1135. BfIRValue CreateAlloca(BfIRType type, BfIRValue arraySize);
  1136. void SetAllocaAlignment(BfIRValue val, int alignment);
  1137. // When we do a dynamic alloca where we know the memory access patterns will not cause a page fault, we can omit the __chkstk call
  1138. // Generally, this is when we allocate less than 4k and we know there will be a write on this memory before the next alloca
  1139. void SetAllocaNoChkStkHint(BfIRValue val);
  1140. void SetAllocaForceMem(BfIRValue val);
  1141. BfIRValue CreateAliasValue(BfIRValue val);
  1142. BfIRValue CreateLifetimeStart(BfIRValue val);
  1143. BfIRValue CreateLifetimeEnd(BfIRValue val);
  1144. BfIRValue CreateLifetimeSoftEnd(BfIRValue val);
  1145. BfIRValue CreateLifetimeExtend(BfIRValue val);
  1146. BfIRValue CreateValueScopeStart();
  1147. void CreateValueScopeRetain(BfIRValue val); // When a value is held by a variable -- don't release until we have a HardValueScopeEnd
  1148. void CreateValueScopeSoftEnd(BfIRValue scopeStart);
  1149. void CreateValueScopeHardEnd(BfIRValue scopeStart);
  1150. BfIRValue CreateLoad(BfIRValue val, bool isVolatile = false);
  1151. BfIRValue CreateAlignedLoad(BfIRValue val, int align, bool isVolatile = false);
  1152. BfIRValue CreateStore(BfIRValue val, BfIRValue ptr, bool isVolatile = false);
  1153. BfIRValue CreateAlignedStore(BfIRValue val, BfIRValue ptr, int align, bool isVolatile = false);
  1154. BfIRValue CreateMemSet(BfIRValue addr, BfIRValue val, BfIRValue size, int align);
  1155. void CreateFence(BfIRFenceType fenceType);
  1156. BfIRValue CreateStackSave();
  1157. BfIRValue CreateStackRestore(BfIRValue stackVal);
  1158. BfIRValue CreateGlobalVariable(BfIRType varType, bool isConstant, BfIRLinkageType linkageType, BfIRValue initializer, const StringImpl& name, bool isTLS = false);
  1159. void CreateGlobalVariable(BfIRValue irValue);
  1160. void GlobalVar_SetUnnamedAddr(BfIRValue val, bool unnamedAddr);
  1161. void GlobalVar_SetInitializer(BfIRValue globalVar, BfIRValue initVal);
  1162. void GlobalVar_SetAlignment(BfIRValue globalVar, int alignment);
  1163. void GlobalVar_SetStorageKind(BfIRValue globalVar, BfIRStorageKind storageKind);
  1164. BfIRValue CreateGlobalStringPtr(const StringImpl& str);
  1165. void SetReflectTypeData(BfIRType type, BfIRValue globalVar);
  1166. BfIRBlock CreateBlock(const StringImpl& name, bool addNow = false);
  1167. BfIRBlock MaybeChainNewBlock(const StringImpl& name); // Creates new block if current block isn't empty
  1168. void AddBlock(BfIRBlock block);
  1169. void DropBlocks(BfIRBlock block);
  1170. void MergeBlockDown(BfIRBlock fromBlock, BfIRBlock intoBlock);
  1171. void SetInsertPoint(BfIRValue value);
  1172. void SetInsertPoint(BfIRBlock block);
  1173. void SetInsertPointAtStart(BfIRBlock block);
  1174. void EraseFromParent(BfIRBlock block);
  1175. void DeleteBlock(BfIRBlock block);
  1176. void EraseInstFromParent(BfIRValue val);
  1177. BfIRValue CreateBr(BfIRBlock block);
  1178. BfIRValue CreateBr_Fake(BfIRBlock block);
  1179. BfIRValue CreateBr_NoCollapse(BfIRBlock block);
  1180. void CreateCondBr(BfIRValue val, BfIRBlock trueBlock, BfIRBlock falseBlock);
  1181. BfIRBlock GetInsertBlock();
  1182. void MoveBlockToEnd(BfIRBlock block);
  1183. BfIRValue CreateSwitch(BfIRValue value, BfIRBlock dest, int numCases);
  1184. BfIRValue AddSwitchCase(BfIRValue switchVal, BfIRValue caseVal, BfIRBlock caseBlock);
  1185. void SetSwitchDefaultDest(BfIRValue switchVal, BfIRBlock caseBlock);
  1186. BfIRValue CreatePhi(BfIRType type, int incomingCount);
  1187. void AddPhiIncoming(BfIRValue phi, BfIRValue value, BfIRBlock comingFrom);
  1188. BfIRFunction GetIntrinsic(String intrinName, int intrinId, BfIRType returnType, const BfSizedArray<BfIRType>& paramTypes);
  1189. BfIRFunctionType MapMethod(BfMethodInstance* methodInstance);
  1190. BfIRFunctionType CreateFunctionType(BfIRType resultType, const BfSizedArray<BfIRType>& paramTypes, bool isVarArg = false);
  1191. BfIRFunction CreateFunction(BfIRFunctionType funcType, BfIRLinkageType linkageType, const StringImpl& name);
  1192. void SetFunctionName(BfIRValue func, const StringImpl& name);
  1193. void EnsureFunctionPatchable();
  1194. BfIRValue RemapBindFunction(BfIRValue func);
  1195. void SetActiveFunction(BfIRFunction func);
  1196. BfIRFunction GetActiveFunction();
  1197. BfIRFunction GetFunction(const StringImpl& name);
  1198. BfIRValue CreateCall(BfIRValue func, const BfSizedArray<BfIRValue>& params);
  1199. void SetCallCallingConv(BfIRValue callInst, BfIRCallingConv callingConv);
  1200. void SetFuncCallingConv(BfIRFunction func, BfIRCallingConv callingConv);
  1201. void SetTailCall(BfIRValue callInst);
  1202. void SetCallAttribute(BfIRValue callInst, int paramIdx, BfIRAttribute attribute);
  1203. BfIRValue CreateRet(BfIRValue val);
  1204. BfIRValue CreateSetRet(BfIRValue val, int returnTypeId);
  1205. void CreateRetVoid();
  1206. void CreateUnreachable();
  1207. void Call_AddAttribute(BfIRValue callInst, int argIdx, BfIRAttribute attr);
  1208. void Call_AddAttribute(BfIRValue callInst, int argIdx, BfIRAttribute attr, int arg);
  1209. void Func_AddAttribute(BfIRFunction func, int argIdx, BfIRAttribute attr);
  1210. void Func_AddAttribute(BfIRFunction func, int argIdx, BfIRAttribute attr, int arg);
  1211. void Func_SetParamName(BfIRFunction func, int argIdx, const StringImpl& name);
  1212. void Func_DeleteBody(BfIRFunction func);
  1213. void Func_SafeRename(BfIRFunction func);
  1214. void Func_SafeRenameFrom(BfIRFunction func, const StringImpl& prevName);
  1215. void Func_SetLinkage(BfIRFunction func, BfIRLinkageType linkage);
  1216. void Comptime_Error(int errorKind);
  1217. BfIRValue Comptime_GetBfType(int typeId, BfIRType resultType);
  1218. BfIRValue Comptime_GetReflectType(int typeId, BfIRType resultType);
  1219. BfIRValue Comptime_DynamicCastCheck(BfIRValue value, int typeId, BfIRType resultType);
  1220. BfIRValue Comptime_GetVirtualFunc(BfIRValue value, int virtualTableId, BfIRType resultType);
  1221. BfIRValue Comptime_GetInterfaceFunc(BfIRValue value, int typeId, int methodIdx, BfIRType resultType);
  1222. void SaveDebugLocation();
  1223. void RestoreDebugLocation();
  1224. void DupDebugLocation();
  1225. bool HasDebugLocation();
  1226. void ClearDebugLocation();
  1227. void ClearDebugLocation(BfIRValue inst);
  1228. void ClearDebugLocation_Last();
  1229. void UpdateDebugLocation(BfIRValue inst);
  1230. void SetCurrentDebugLocation(int line, int column, BfIRMDNode diScope, BfIRMDNode diInlinedAt);
  1231. void CreateNop();
  1232. void CreateEnsureInstructionAt();
  1233. void CreateStatementStart();
  1234. void CreateObjectAccessCheck(BfIRValue value, bool useAsm);
  1235. void DbgInit();
  1236. void DbgFinalize();
  1237. bool DbgHasInfo();
  1238. bool DbgHasLineInfo();
  1239. String DbgGetStaticFieldName(BfFieldInstance* fieldInstance);
  1240. void DbgAddPrefix(String& name);
  1241. BfIRMDNode DbgCreateCompileUnit(int lang, const StringImpl& filename, const StringImpl& directory, const StringImpl& producer, bool isOptimized,
  1242. const StringImpl& flags, int runtimeVer, bool linesOnly);
  1243. BfIRMDNode DbgCreateFile(const StringImpl& fileName, const StringImpl& directory, const Val128& md5Hash);
  1244. BfIRMDNode DbgGetCurrentLocation();
  1245. void DbgSetType(BfType * type, BfIRMDNode diType);
  1246. void DbgSetInstType(BfType * type, BfIRMDNode diType);
  1247. BfIRMDNode DbgCreateConstValue(int64 val);
  1248. BfIRMDNode DbgGetType(BfType* type, BfIRPopulateType populateType = BfIRPopulateType_Declaration);
  1249. BfIRMDNode DbgGetTypeInst(BfTypeInstance* typeInst, BfIRPopulateType populateType = BfIRPopulateType_Declaration);
  1250. void DbgTrackDITypes(BfType* type);
  1251. BfIRMDNode DbgCreateNameSpace(BfIRMDNode scope, const StringImpl& name, BfIRMDNode file, int lineNum);
  1252. BfIRMDNode DbgCreateImportedModule(BfIRMDNode context, BfIRMDNode namespaceNode, int line);
  1253. BfIRMDNode DbgCreateBasicType(const StringImpl& name, int64 sizeInBits, int64 alignInBits, int encoding);
  1254. BfIRMDNode DbgCreateStructType(BfIRMDNode context, const StringImpl& name, BfIRMDNode file, int lineNum, int64 sizeInBits, int64 alignInBits,
  1255. int flags, BfIRMDNode derivedFrom, const BfSizedArray<BfIRMDNode>& elements);
  1256. BfIRMDNode DbgCreateEnumerationType(BfIRMDNode scope, const StringImpl& name, BfIRMDNode file, int lineNumber, int64 SizeInBits, int64 alignInBits,
  1257. const BfSizedArray<BfIRMDNode>& elements, BfIRMDNode underlyingType);
  1258. BfIRMDNode DbgCreatePointerType(BfIRMDNode diType);
  1259. BfIRMDNode DbgCreateReferenceType(BfIRMDNode diType);
  1260. BfIRMDNode DbgCreateConstType(BfIRMDNode diType);
  1261. BfIRMDNode DbgCreateArtificialType(BfIRMDNode diType);
  1262. BfIRMDNode DbgCreateArrayType(int64 sizeInBits, int64 alignInBits, BfIRMDNode elementType, int64 numElements);
  1263. BfIRMDNode DbgCreateReplaceableCompositeType(int tag, const StringImpl& name, BfIRMDNode scope, BfIRMDNode F, int line, int64 sizeInBits = 0, int64 alignInBits = 0, int flags = 0);
  1264. void DbgSetTypeSize(BfIRMDNode diType, int64 sizeInBits, int alignInBits);
  1265. BfIRMDNode DbgCreateForwardDecl(int tag, const StringImpl& name, BfIRMDNode scope, BfIRMDNode F, int line);
  1266. BfIRMDNode DbgCreateSizedForwardDecl(int tag, const StringImpl& name, BfIRMDNode scope, BfIRMDNode F, int line, int64 sizeInBits, int64 alignInBits);
  1267. BfIRMDNode DbgReplaceAllUses(BfIRMDNode diPrevNode, BfIRMDNode diNewNode);
  1268. void DbgDeleteTemporary(BfIRMDNode diNode);
  1269. BfIRMDNode DbgMakePermanent(BfIRMDNode diNode, BfIRMDNode diBaseType, const BfSizedArray<BfIRMDNode>& elements);
  1270. BfIRMDNode DbgCreateEnumerator(const StringImpl& name, int64 val);
  1271. BfIRMDNode DbgCreateMemberType(BfIRMDNode scope, const StringImpl& name, BfIRMDNode file, int lineNumber, int64 sizeInBits, int64 alignInBits,
  1272. int64 offsetInBits, int flags, BfIRMDNode type);
  1273. BfIRMDNode DbgCreateStaticMemberType(BfIRMDNode scope, const StringImpl& name, BfIRMDNode file, int lineNumber, BfIRMDNode type, int flags, BfIRValue val);
  1274. BfIRMDNode DbgCreateInheritance(BfIRMDNode type, BfIRMDNode baseType, int64 baseOffset, int flags);
  1275. BfIRMDNode DbgCreateMethod(BfIRMDNode context, const StringImpl& name, const StringImpl& linkageName, BfIRMDNode file, int lineNum, BfIRMDNode type,
  1276. bool isLocalToUnit, bool isDefinition, int vk, int vIndex, BfIRMDNode vTableHolder, int flags, bool isOptimized, BfIRValue fn,
  1277. const BfSizedArray<BfIRMDNode>& genericArgs, const BfSizedArray<BfIRValue>& genericConstValueArgs);
  1278. BfIRMDNode DbgCreateFunction(BfIRMDNode context, const StringImpl& name, const StringImpl& linkageName, BfIRMDNode file, int lineNum, BfIRMDNode type,
  1279. bool isLocalToUnit, bool isDefinition, int scopeLine, int flags, bool isOptimized, BfIRValue fn);
  1280. BfIRMDNode DbgCreateParameterVariable(BfIRMDNode scope, const StringImpl& name, int argNo, BfIRMDNode file, int lineNum, BfIRMDNode type,
  1281. bool AlwaysPreserve = false, int flags = 0);
  1282. BfIRMDNode DbgCreateSubroutineType(BfMethodInstance* methodInstance);
  1283. BfIRMDNode DbgCreateSubroutineType(const BfSizedArray<BfIRMDNode>& elements);
  1284. BfIRMDNode DbgCreateAutoVariable(BfIRMDNode scope, const StringImpl& name, BfIRMDNode file, int lineNo, BfIRMDNode type, BfIRInitType initType = BfIRInitType_NotSet);
  1285. BfIRValue DbgInsertValueIntrinsic(BfIRValue val, BfIRMDNode varInfo);
  1286. BfIRValue DbgInsertDeclare(BfIRValue val, BfIRMDNode varInfo, BfIRValue declareBefore = BfIRValue());
  1287. BfIRValue DbgLifetimeEnd(BfIRMDNode varInfo);
  1288. void DbgCreateGlobalVariable(BfIRMDNode context, const StringImpl& name, const StringImpl& linkageName, BfIRMDNode file, int lineNumber,
  1289. BfIRMDNode type, bool isLocalToUnit, BfIRValue val, BfIRMDNode Decl = BfIRMDNode());
  1290. BfIRMDNode DbgCreateLexicalBlock(BfIRMDNode scope, BfIRMDNode file, int line, int col);
  1291. void DbgCreateAnnotation(BfIRMDNode scope, const StringImpl& name, BfIRValue value);
  1292. BfIRState GetState();
  1293. void SetState(const BfIRState& state);
  1294. };
  1295. NS_BF_END