tilde.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. #define TB_TYPE_F16 TB_DataType{ { TB_INT, 0, 16 } }
  9. #define TB_TYPE_I128 TB_DataType{ { TB_INT, 0, 128 } }
  10. #define TB_TYPE_INT TB_TYPE_INTN(cast(u16)(8*build_context.int_size))
  11. #define TB_TYPE_INTPTR TB_TYPE_INTN(cast(u16)(8*build_context.ptr_size))
  12. #if defined(GB_SYSTEM_WINDOWS)
  13. #pragma warning(pop)
  14. #endif
  15. #define CG_STARTUP_RUNTIME_PROC_NAME "__$startup_runtime"
  16. #define CG_CLEANUP_RUNTIME_PROC_NAME "__$cleanup_runtime"
  17. #define CG_STARTUP_TYPE_INFO_PROC_NAME "__$startup_type_info"
  18. #define CG_TYPE_INFO_DATA_NAME "__$type_info_data"
  19. #define CG_TYPE_INFO_TYPES_NAME "__$type_info_types_data"
  20. #define CG_TYPE_INFO_NAMES_NAME "__$type_info_names_data"
  21. #define CG_TYPE_INFO_OFFSETS_NAME "__$type_info_offsets_data"
  22. #define CG_TYPE_INFO_USINGS_NAME "__$type_info_usings_data"
  23. #define CG_TYPE_INFO_TAGS_NAME "__$type_info_tags_data"
  24. struct cgModule;
  25. enum cgValueKind : u32 {
  26. cgValue_Value, // rvalue
  27. cgValue_Addr, // lvalue
  28. cgValue_Symbol, // global
  29. cgValue_Multi, // multiple values
  30. };
  31. struct cgValueMulti;
  32. struct cgValue {
  33. cgValueKind kind;
  34. Type * type;
  35. union {
  36. // NOTE: any value in this union must be a pointer
  37. TB_Symbol * symbol;
  38. TB_Node * node;
  39. cgValueMulti *multi;
  40. };
  41. };
  42. struct cgValueMulti {
  43. Slice<cgValue> values;
  44. };
  45. enum cgAddrKind {
  46. cgAddr_Default,
  47. cgAddr_Map,
  48. cgAddr_Context,
  49. cgAddr_SoaVariable,
  50. cgAddr_RelativePointer,
  51. cgAddr_RelativeSlice,
  52. cgAddr_Swizzle,
  53. cgAddr_SwizzleLarge,
  54. };
  55. struct cgAddr {
  56. cgAddrKind kind;
  57. cgValue addr;
  58. union {
  59. struct {
  60. cgValue key;
  61. Type *type;
  62. Type *result;
  63. } map;
  64. struct {
  65. Selection sel;
  66. } ctx;
  67. struct {
  68. cgValue index;
  69. Ast *index_expr;
  70. } soa;
  71. struct {
  72. cgValue index;
  73. Ast *node;
  74. } index_set;
  75. struct {
  76. bool deref;
  77. } relative;
  78. struct {
  79. Type *type;
  80. u8 count; // 2, 3, or 4 components
  81. u8 indices[4];
  82. } swizzle;
  83. struct {
  84. Type *type;
  85. Slice<i32> indices;
  86. } swizzle_large;
  87. };
  88. };
  89. struct cgTargetList {
  90. cgTargetList *prev;
  91. bool is_block;
  92. // control regions
  93. TB_Node * break_;
  94. TB_Node * continue_;
  95. TB_Node * fallthrough_;
  96. };
  97. struct cgBranchRegions {
  98. Ast * label;
  99. TB_Node *break_;
  100. TB_Node *continue_;
  101. };
  102. enum cgDeferExitKind {
  103. cgDeferExit_Default,
  104. cgDeferExit_Return,
  105. cgDeferExit_Branch,
  106. };
  107. enum cgDeferKind {
  108. cgDefer_Node,
  109. cgDefer_Proc,
  110. };
  111. struct cgDefer {
  112. cgDeferKind kind;
  113. isize scope_index;
  114. isize context_stack_count;
  115. TB_Node * control_region;
  116. union {
  117. Ast *stmt;
  118. struct {
  119. cgValue deferred;
  120. Slice<cgValue> result_as_args;
  121. } proc;
  122. };
  123. };
  124. struct cgContextData {
  125. cgAddr ctx;
  126. isize scope_index;
  127. isize uses;
  128. };
  129. struct cgControlRegion {
  130. TB_Node *control_region;
  131. isize scope_index;
  132. };
  133. struct cgProcedure {
  134. u32 flags;
  135. u16 state_flags;
  136. cgProcedure *parent;
  137. Array<cgProcedure *> children;
  138. TB_Function *func;
  139. TB_FunctionPrototype *proto;
  140. TB_Symbol *symbol;
  141. Entity * entity;
  142. cgModule *module;
  143. String name;
  144. Type * type;
  145. Ast * type_expr;
  146. Ast * body;
  147. u64 tags;
  148. ProcInlining inlining;
  149. bool is_foreign;
  150. bool is_export;
  151. bool is_entry_point;
  152. bool is_startup;
  153. TB_DebugType *debug_type;
  154. cgValue value;
  155. Ast *curr_stmt;
  156. cgTargetList * target_list;
  157. Array<cgDefer> defer_stack;
  158. Array<Scope *> scope_stack;
  159. Array<cgContextData> context_stack;
  160. Array<cgControlRegion> control_regions;
  161. Array<cgBranchRegions> branch_regions;
  162. Scope *curr_scope;
  163. i32 scope_index;
  164. bool in_multi_assignment;
  165. isize split_returns_index;
  166. bool return_by_ptr;
  167. PtrMap<Entity *, cgAddr> variable_map;
  168. };
  169. struct cgModule {
  170. TB_Module * mod;
  171. Checker * checker;
  172. CheckerInfo *info;
  173. RwMutex values_mutex;
  174. PtrMap<Entity *, cgValue> values;
  175. StringMap<cgValue> members;
  176. StringMap<cgProcedure *> procedures;
  177. PtrMap<TB_Function *, Entity *> procedure_values;
  178. Array<cgProcedure *> procedures_to_generate;
  179. RecursiveMutex debug_type_mutex;
  180. PtrMap<Type *, TB_DebugType *> debug_type_map;
  181. PtrMap<Type *, TB_DebugType *> proc_debug_type_map; // not pointer to
  182. RecursiveMutex proc_proto_mutex;
  183. PtrMap<Type *, TB_FunctionPrototype *> proc_proto_map;
  184. PtrMap<uintptr, TB_FileID> file_id_map; // Key: AstFile.id (i32 cast to uintptr)
  185. std::atomic<u32> nested_type_name_guid;
  186. std::atomic<u32> const_nil_guid;
  187. };
  188. #ifndef ABI_PKG_NAME_SEPARATOR
  189. #define ABI_PKG_NAME_SEPARATOR "@"
  190. #endif
  191. gb_global Entity *cg_global_type_info_data_entity = {};
  192. gb_global cgAddr cg_global_type_info_member_types = {};
  193. gb_global cgAddr cg_global_type_info_member_names = {};
  194. gb_global cgAddr cg_global_type_info_member_offsets = {};
  195. gb_global cgAddr cg_global_type_info_member_usings = {};
  196. gb_global cgAddr cg_global_type_info_member_tags = {};
  197. gb_global isize cg_global_type_info_data_index = 0;
  198. gb_global isize cg_global_type_info_member_types_index = 0;
  199. gb_global isize cg_global_type_info_member_names_index = 0;
  200. gb_global isize cg_global_type_info_member_offsets_index = 0;
  201. gb_global isize cg_global_type_info_member_usings_index = 0;
  202. gb_global isize cg_global_type_info_member_tags_index = 0;
  203. gb_internal TB_Arena *cg_arena(void);
  204. gb_internal cgValue cg_value(TB_Global * g, Type *type);
  205. gb_internal cgValue cg_value(TB_External *e, Type *type);
  206. gb_internal cgValue cg_value(TB_Function *f, Type *type);
  207. gb_internal cgValue cg_value(TB_Symbol * s, Type *type);
  208. gb_internal cgValue cg_value(TB_Node * node, Type *type);
  209. gb_internal cgAddr cg_addr(cgValue const &value);
  210. gb_internal cgValue cg_const_value(cgProcedure *p, Type *type, ExactValue const &value);
  211. gb_internal cgValue cg_const_nil(cgProcedure *p, Type *type);
  212. gb_internal cgValue cg_flatten_value(cgProcedure *p, cgValue value);
  213. gb_internal void cg_build_stmt(cgProcedure *p, Ast *stmt);
  214. gb_internal void cg_build_stmt_list(cgProcedure *p, Slice<Ast *> const &stmts);
  215. gb_internal void cg_build_when_stmt(cgProcedure *p, AstWhenStmt *ws);
  216. gb_internal cgValue cg_build_expr(cgProcedure *p, Ast *expr);
  217. gb_internal cgAddr cg_build_addr(cgProcedure *p, Ast *expr);
  218. gb_internal cgValue cg_build_addr_ptr(cgProcedure *p, Ast *expr);
  219. gb_internal Type * cg_addr_type(cgAddr const &addr);
  220. gb_internal cgValue cg_addr_load(cgProcedure *p, cgAddr addr);
  221. gb_internal void cg_addr_store(cgProcedure *p, cgAddr addr, cgValue value);
  222. gb_internal cgValue cg_addr_get_ptr(cgProcedure *p, cgAddr const &addr);
  223. gb_internal cgValue cg_emit_load(cgProcedure *p, cgValue const &ptr, bool is_volatile=false);
  224. gb_internal void cg_emit_store(cgProcedure *p, cgValue dst, cgValue const &src, bool is_volatile=false);
  225. gb_internal cgAddr cg_add_local(cgProcedure *p, Type *type, Entity *e, bool zero_init);
  226. gb_internal cgValue cg_address_from_load_or_generate_local(cgProcedure *p, cgValue value);
  227. gb_internal cgValue cg_copy_value_to_ptr(cgProcedure *p, cgValue value, Type *original_type, isize min_alignment);
  228. gb_internal cgValue cg_build_call_expr(cgProcedure *p, Ast *expr);
  229. gb_internal cgValue cg_find_procedure_value_from_entity(cgModule *m, Entity *e);
  230. gb_internal TB_DebugType *cg_debug_type(cgModule *m, Type *type);
  231. gb_internal String cg_get_entity_name(cgModule *m, Entity *e);
  232. gb_internal cgValue cg_typeid(cgProcedure *m, Type *t);
  233. gb_internal cgValue cg_emit_ptr_offset(cgProcedure *p, cgValue ptr, cgValue index);
  234. gb_internal cgValue cg_emit_array_ep(cgProcedure *p, cgValue s, cgValue index);
  235. gb_internal cgValue cg_emit_array_epi(cgProcedure *p, cgValue s, i64 index);
  236. gb_internal cgValue cg_emit_struct_ep(cgProcedure *p, cgValue s, i64 index);
  237. gb_internal cgValue cg_emit_deep_field_gep(cgProcedure *p, cgValue e, Selection const &sel);
  238. gb_internal cgValue cg_emit_conv(cgProcedure *p, cgValue value, Type *t);
  239. gb_internal cgValue cg_emit_comp_against_nil(cgProcedure *p, TokenKind op_kind, cgValue x);
  240. gb_internal cgValue cg_emit_comp(cgProcedure *p, TokenKind op_kind, cgValue left, cgValue right);
  241. gb_internal cgValue cg_emit_arith(cgProcedure *p, TokenKind op, cgValue lhs, cgValue rhs, Type *type);
  242. gb_internal bool cg_emit_goto(cgProcedure *p, TB_Node *control_region);
  243. gb_internal TB_Node *cg_control_region(cgProcedure *p, char const *name);
  244. gb_internal isize cg_append_tuple_values(cgProcedure *p, Array<cgValue> *dst_values, cgValue src_value);
  245. gb_internal cgValue cg_handle_param_value(cgProcedure *p, Type *parameter_type, ParameterValue const &param_value, TokenPos const &pos);
  246. gb_internal cgValue cg_builtin_len(cgProcedure *p, cgValue value);
  247. gb_internal cgValue cg_builtin_raw_data(cgProcedure *p, cgValue const &x);