tilde_backend.hpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #if defined(GB_SYSTEM_WINDOWS)
  2. #pragma warning(push)
  3. #pragma warning(disable: 4200)
  4. #pragma warning(disable: 4201)
  5. #define restrict gb_restrict
  6. #endif
  7. #include "tilde/tb.h"
  8. #if defined(GB_SYSTEM_WINDOWS)
  9. #pragma warning(pop)
  10. #endif
  11. #define CG_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime"
  12. #define CG_CLEANUP_RUNTIME_PROC_NAME "__$cleanup_runtime"
  13. #define CG_STARTUP_TYPE_INFO_PROC_NAME "__$startup_type_info"
  14. #define CG_TYPE_INFO_DATA_NAME "__$type_info_data"
  15. #define CG_TYPE_INFO_TYPES_NAME "__$type_info_types_data"
  16. #define CG_TYPE_INFO_NAMES_NAME "__$type_info_names_data"
  17. #define CG_TYPE_INFO_OFFSETS_NAME "__$type_info_offsets_data"
  18. #define CG_TYPE_INFO_USINGS_NAME "__$type_info_usings_data"
  19. #define CG_TYPE_INFO_TAGS_NAME "__$type_info_tags_data"
  20. struct cgModule;
  21. enum cgValueKind : u32 {
  22. cgValue_Value,
  23. cgValue_Addr,
  24. cgValue_Symbol,
  25. };
  26. struct cgValue {
  27. cgValueKind kind;
  28. Type * type;
  29. union {
  30. TB_Symbol *symbol;
  31. TB_Node * node;
  32. };
  33. };
  34. enum cgAddrKind {
  35. cgAddr_Default,
  36. cgAddr_Map,
  37. cgAddr_Context,
  38. cgAddr_SoaVariable,
  39. cgAddr_RelativePointer,
  40. cgAddr_RelativeSlice,
  41. cgAddr_Swizzle,
  42. cgAddr_SwizzleLarge,
  43. };
  44. struct cgAddr {
  45. cgAddrKind kind;
  46. cgValue addr;
  47. union {
  48. struct {
  49. cgValue key;
  50. Type *type;
  51. Type *result;
  52. } map;
  53. struct {
  54. Selection sel;
  55. } ctx;
  56. struct {
  57. cgValue index;
  58. Ast *index_expr;
  59. } soa;
  60. struct {
  61. cgValue index;
  62. Ast *node;
  63. } index_set;
  64. struct {
  65. bool deref;
  66. } relative;
  67. struct {
  68. Type *type;
  69. u8 count; // 2, 3, or 4 components
  70. u8 indices[4];
  71. } swizzle;
  72. struct {
  73. Type *type;
  74. Slice<i32> indices;
  75. } swizzle_large;
  76. };
  77. };
  78. struct cgTargetList {
  79. cgTargetList *prev;
  80. bool is_block;
  81. // control regions
  82. TB_Node * break_;
  83. TB_Node * continue_;
  84. TB_Node * fallthrough_;
  85. };
  86. struct cgBranchBlocks {
  87. Ast * label;
  88. TB_Node *break_;
  89. TB_Node *continue_;
  90. };
  91. enum cgDeferExitKind {
  92. cgDeferExit_Default,
  93. cgDeferExit_Return,
  94. cgDeferExit_Branch,
  95. };
  96. struct cgProcedure {
  97. u32 flags;
  98. u16 state_flags;
  99. cgProcedure *parent;
  100. Array<cgProcedure *> children;
  101. TB_Function *func;
  102. TB_Symbol *symbol;
  103. Entity * entity;
  104. cgModule *module;
  105. String name;
  106. Type * type;
  107. Ast * type_expr;
  108. Ast * body;
  109. u64 tags;
  110. ProcInlining inlining;
  111. bool is_foreign;
  112. bool is_export;
  113. bool is_entry_point;
  114. bool is_startup;
  115. cgValue value;
  116. Ast *curr_stmt;
  117. cgTargetList * target_list;
  118. Array<cgBranchBlocks> branch_blocks;
  119. };
  120. struct cgModule {
  121. TB_Module * mod;
  122. Checker * checker;
  123. CheckerInfo *info;
  124. RwMutex values_mutex;
  125. PtrMap<Entity *, cgValue> values;
  126. StringMap<cgValue> members;
  127. StringMap<cgProcedure *> procedures;
  128. PtrMap<TB_Function *, Entity *> procedure_values;
  129. Array<cgProcedure *> procedures_to_generate;
  130. PtrMap<uintptr, TB_FileID> file_id_map; // Key: AstFile.id (i32 cast to uintptr)
  131. std::atomic<u32> nested_type_name_guid;
  132. };
  133. #ifndef ABI_PKG_NAME_SEPARATOR
  134. #define ABI_PKG_NAME_SEPARATOR "."
  135. #endif
  136. gb_global Entity *cg_global_type_info_data_entity = {};
  137. gb_global cgAddr cg_global_type_info_member_types = {};
  138. gb_global cgAddr cg_global_type_info_member_names = {};
  139. gb_global cgAddr cg_global_type_info_member_offsets = {};
  140. gb_global cgAddr cg_global_type_info_member_usings = {};
  141. gb_global cgAddr cg_global_type_info_member_tags = {};
  142. gb_global isize cg_global_type_info_data_index = 0;
  143. gb_global isize cg_global_type_info_member_types_index = 0;
  144. gb_global isize cg_global_type_info_member_names_index = 0;
  145. gb_global isize cg_global_type_info_member_offsets_index = 0;
  146. gb_global isize cg_global_type_info_member_usings_index = 0;
  147. gb_global isize cg_global_type_info_member_tags_index = 0;
  148. gb_internal cgValue cg_value(TB_Global * g, Type *type);
  149. gb_internal cgValue cg_value(TB_External *e, Type *type);
  150. gb_internal cgValue cg_value(TB_Function *f, Type *type);
  151. gb_internal cgValue cg_value(TB_Symbol * s, Type *type);
  152. gb_internal cgValue cg_value(TB_Node * node, Type *type);
  153. gb_internal cgAddr cg_addr(cgValue const &value);
  154. gb_internal void cg_build_stmt(cgProcedure *p, Ast *stmt);
  155. gb_internal void cg_build_stmt_list(cgProcedure *p, Slice<Ast *> const &stmts);
  156. gb_internal void cg_build_when_stmt(cgProcedure *p, AstWhenStmt *ws);