This is a maintenance fork
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

299 lines
8.2 KiB

12 months ago
12 months ago
12 months ago
12 months ago
12 months ago
  1. #include "third_party/chibicc/chibicc.h"
  2. /* TODO(jart): Why can't these be const? */
  3. Type ty_void[1] = {{TY_VOID, 1, 1}};
  4. Type ty_bool[1] = {{TY_BOOL, 1, 1}};
  5. Type ty_char[1] = {{TY_CHAR, 1, 1}};
  6. Type ty_short[1] = {{TY_SHORT, 2, 2}};
  7. Type ty_int[1] = {{TY_INT, 4, 4}};
  8. Type ty_long[1] = {{TY_LONG, 8, 8}};
  9. Type ty_int128[1] = {{TY_INT128, 16, 16}};
  10. Type ty_uchar[1] = {{TY_CHAR, 1, 1, true}};
  11. Type ty_ushort[1] = {{TY_SHORT, 2, 2, true}};
  12. Type ty_uint[1] = {{TY_INT, 4, 4, true}};
  13. Type ty_ulong[1] = {{TY_LONG, 8, 8, true}};
  14. Type ty_uint128[1] = {{TY_INT128, 16, 16, true}};
  15. Type ty_float[1] = {{TY_FLOAT, 4, 4}};
  16. Type ty_double[1] = {{TY_DOUBLE, 8, 8}};
  17. Type ty_ldouble[1] = {{TY_LDOUBLE, 16, 16}};
  18. static Type *new_type(TypeKind kind, int size, int align) {
  19. Type *ty = alloc_type();
  20. ty->kind = kind;
  21. ty->size = size;
  22. ty->align = align;
  23. return ty;
  24. }
  25. bool is_integer(Type *ty) {
  26. TypeKind k = ty->kind;
  27. return k == TY_BOOL || k == TY_CHAR || k == TY_SHORT || k == TY_INT ||
  28. k == TY_LONG || k == TY_INT128 || k == TY_ENUM;
  29. }
  30. bool is_flonum(Type *ty) {
  31. return ty->kind == TY_FLOAT || ty->kind == TY_DOUBLE ||
  32. ty->kind == TY_LDOUBLE;
  33. }
  34. bool is_numeric(Type *ty) {
  35. return is_integer(ty) || is_flonum(ty);
  36. }
  37. bool is_compatible(Type *t1, Type *t2) {
  38. if (t1 == t2) return true;
  39. if (t1->origin) return is_compatible(t1->origin, t2);
  40. if (t2->origin) return is_compatible(t1, t2->origin);
  41. if (t1->kind != t2->kind) return false;
  42. switch (t1->kind) {
  43. case TY_CHAR:
  44. case TY_SHORT:
  45. case TY_INT:
  46. case TY_LONG:
  47. case TY_INT128:
  48. return t1->is_unsigned == t2->is_unsigned;
  49. case TY_FLOAT:
  50. case TY_DOUBLE:
  51. case TY_LDOUBLE:
  52. return true;
  53. case TY_PTR:
  54. return is_compatible(t1->base, t2->base);
  55. case TY_FUNC: {
  56. if (!is_compatible(t1->return_ty, t2->return_ty)) return false;
  57. if (t1->is_variadic != t2->is_variadic) return false;
  58. Type *p1 = t1->params;
  59. Type *p2 = t2->params;
  60. for (; p1 && p2; p1 = p1->next, p2 = p2->next) {
  61. if (!is_compatible(p1, p2)) return false;
  62. }
  63. return p1 == NULL && p2 == NULL;
  64. }
  65. case TY_ARRAY:
  66. if (!is_compatible(t1->base, t2->base)) return false;
  67. return t1->array_len < 0 && t2->array_len < 0 &&
  68. t1->array_len == t2->array_len;
  69. }
  70. return false;
  71. }
  72. Type *copy_type(Type *ty) {
  73. Type *ret = alloc_type();
  74. *ret = *ty;
  75. ret->origin = ty;
  76. return ret;
  77. }
  78. Type *pointer_to(Type *base) {
  79. Type *ty = new_type(TY_PTR, 8, 8);
  80. ty->base = base;
  81. ty->is_unsigned = true;
  82. return ty;
  83. }
  84. Type *func_type(Type *return_ty) {
  85. // The C spec disallows sizeof(<function type>), but
  86. // GCC allows that and the expression is evaluated to 1.
  87. Type *ty = new_type(TY_FUNC, 1, 1);
  88. ty->return_ty = return_ty;
  89. return ty;
  90. }
  91. Type *array_of(Type *base, int len) {
  92. Type *ty = new_type(TY_ARRAY, base->size * len, base->align);
  93. ty->base = base;
  94. ty->array_len = len;
  95. return ty;
  96. }
  97. Type *vla_of(Type *base, Node *len) {
  98. Type *ty = new_type(TY_VLA, 8, 8);
  99. ty->base = base;
  100. ty->vla_len = len;
  101. return ty;
  102. }
  103. Type *enum_type(void) {
  104. Type *ty = new_type(TY_ENUM, 4, 4);
  105. ty->is_unsigned = true;
  106. return ty;
  107. }
  108. Type *struct_type(void) {
  109. return new_type(TY_STRUCT, 0, 1);
  110. }
  111. static Type *get_common_type(Type *ty1, Type *ty2) {
  112. if (ty1->base) return pointer_to(ty1->base);
  113. if (ty1->kind == TY_FUNC) return pointer_to(ty1);
  114. if (ty2->kind == TY_FUNC) return pointer_to(ty2);
  115. if (ty1->kind == TY_LDOUBLE || ty2->kind == TY_LDOUBLE) return ty_ldouble;
  116. if (ty1->kind == TY_DOUBLE || ty2->kind == TY_DOUBLE) return ty_double;
  117. if (ty1->kind == TY_FLOAT || ty2->kind == TY_FLOAT) return ty_float;
  118. if (ty1->size < 4) ty1 = ty_int;
  119. if (ty2->size < 4) ty2 = ty_int;
  120. if (ty1->size != ty2->size) return (ty1->size < ty2->size) ? ty2 : ty1;
  121. if (ty2->is_unsigned) return ty2;
  122. return ty1;
  123. }
  124. // For many binary operators, we implicitly promote operands so that
  125. // both operands have the same type. Any integral type smaller than
  126. // int is always promoted to int. If the type of one operand is larger
  127. // than the other's (e.g. "long" vs. "int"), the smaller operand will
  128. // be promoted to match with the other.
  129. //
  130. // This operation is called the "usual arithmetic conversion".
  131. static void usual_arith_conv(Node **lhs, Node **rhs) {
  132. Type *ty = get_common_type((*lhs)->ty, (*rhs)->ty);
  133. *lhs = new_cast(*lhs, ty);
  134. *rhs = new_cast(*rhs, ty);
  135. }
  136. void add_type(Node *node) {
  137. if (!node || node->ty) return;
  138. add_type(node->lhs);
  139. add_type(node->rhs);
  140. add_type(node->cond);
  141. add_type(node->then);
  142. add_type(node->els);
  143. add_type(node->init);
  144. add_type(node->inc);
  145. for (Node *n = node->body; n; n = n->next) add_type(n);
  146. for (Node *n = node->args; n; n = n->next) add_type(n);
  147. switch (node->kind) {
  148. case ND_NUM:
  149. node->ty = ty_int;
  150. return;
  151. case ND_ADD:
  152. case ND_SUB:
  153. case ND_MUL:
  154. case ND_DIV:
  155. case ND_REM:
  156. case ND_BINAND:
  157. case ND_BINOR:
  158. case ND_BINXOR:
  159. usual_arith_conv(&node->lhs, &node->rhs);
  160. node->ty = node->lhs->ty;
  161. return;
  162. case ND_NEG: {
  163. Type *ty = get_common_type(ty_int, node->lhs->ty);
  164. node->lhs = new_cast(node->lhs, ty);
  165. node->ty = ty;
  166. return;
  167. }
  168. case ND_ASSIGN:
  169. if (node->lhs->ty->kind == TY_ARRAY)
  170. error_tok(node->lhs->tok, "not an lvalue!");
  171. if (node->lhs->ty->kind != TY_STRUCT)
  172. node->rhs = new_cast(node->rhs, node->lhs->ty);
  173. node->ty = node->lhs->ty;
  174. return;
  175. case ND_EQ:
  176. case ND_NE:
  177. case ND_LT:
  178. case ND_LE:
  179. usual_arith_conv(&node->lhs, &node->rhs);
  180. node->ty = ty_int;
  181. return;
  182. case ND_FUNCALL:
  183. node->ty = node->func_ty->return_ty;
  184. return;
  185. case ND_NOT:
  186. case ND_LOGOR:
  187. case ND_LOGAND:
  188. node->ty = ty_int;
  189. return;
  190. case ND_BITNOT:
  191. case ND_SHL:
  192. case ND_SHR:
  193. node->ty = node->lhs->ty;
  194. return;
  195. case ND_VAR:
  196. case ND_VLA_PTR:
  197. node->ty = node->var->ty;
  198. return;
  199. case ND_COND:
  200. if (node->then->ty->kind == TY_VOID || node->els->ty->kind == TY_VOID) {
  201. node->ty = ty_void;
  202. } else {
  203. usual_arith_conv(&node->then, &node->els);
  204. node->ty = node->then->ty;
  205. }
  206. return;
  207. case ND_COMMA:
  208. node->ty = node->rhs->ty;
  209. return;
  210. case ND_MEMBER:
  211. node->ty = node->member->ty;
  212. return;
  213. case ND_ADDR: {
  214. Type *ty = node->lhs->ty;
  215. if (ty->kind == TY_ARRAY) {
  216. node->ty = pointer_to(ty->base);
  217. } else {
  218. node->ty = pointer_to(ty);
  219. }
  220. return;
  221. }
  222. case ND_DEREF:
  223. #if 0
  224. if (node->lhs->ty->size == 16 && (node->lhs->ty->kind == TY_FLOAT ||
  225. node->lhs->ty->kind == TY_DOUBLE)) {
  226. node->ty = node->lhs->ty;
  227. } else {
  228. #endif
  229. if (!node->lhs->ty->base) {
  230. error_tok(node->tok, "invalid pointer dereference");
  231. }
  232. if (node->lhs->ty->base->kind == TY_VOID) {
  233. /* TODO(jart): Does standard permit this? */
  234. /* https://lkml.org/lkml/2018/3/20/845 */
  235. error_tok(node->tok, "dereferencing a void pointer");
  236. }
  237. node->ty = node->lhs->ty->base;
  238. #if 0
  239. }
  240. #endif
  241. return;
  242. case ND_STMT_EXPR:
  243. if (node->body) {
  244. Node *stmt = node->body;
  245. for (;;) {
  246. if (stmt->next) {
  247. stmt = stmt->next;
  248. } else {
  249. if (stmt->kind == ND_LABEL && stmt->lhs) {
  250. stmt = stmt->lhs;
  251. } else {
  252. break;
  253. }
  254. }
  255. }
  256. if (stmt->kind == ND_EXPR_STMT) {
  257. node->ty = stmt->lhs->ty;
  258. return;
  259. }
  260. }
  261. error_tok(node->tok,
  262. "statement expression returning void is not supported");
  263. return;
  264. case ND_LABEL_VAL:
  265. node->ty = pointer_to(ty_void);
  266. return;
  267. case ND_CAS:
  268. add_type(node->cas_addr);
  269. add_type(node->cas_old);
  270. add_type(node->cas_new);
  271. node->ty = ty_bool;
  272. if (node->cas_addr->ty->kind != TY_PTR)
  273. error_tok(node->cas_addr->tok, "pointer expected");
  274. if (node->cas_old->ty->kind != TY_PTR)
  275. error_tok(node->cas_old->tok, "pointer expected");
  276. return;
  277. case ND_EXCH:
  278. if (node->lhs->ty->kind != TY_PTR)
  279. error_tok(node->cas_addr->tok, "pointer expected");
  280. node->ty = node->lhs->ty->base;
  281. return;
  282. }
  283. }