60 }
61
62 static struct symbol *get_binop_type(struct expression *expr)
63 {
64 struct symbol *left, *right;
65
66 left = get_type(expr->left);
67 if (!left)
68 return NULL;
69
70 if (expr->op == SPECIAL_LEFTSHIFT ||
71 expr->op == SPECIAL_RIGHTSHIFT) {
72 if (type_positive_bits(left) < 31)
73 return &int_ctype;
74 return left;
75 }
76 right = get_type(expr->right);
77 if (!right)
78 return NULL;
79
80 if (expr->op == '-' &&
81 (is_ptr_type(left) && is_ptr_type(right)))
82 return ssize_t_ctype;
83
84 if (left->type == SYM_PTR || left->type == SYM_ARRAY)
85 return left;
86 if (right->type == SYM_PTR || right->type == SYM_ARRAY)
87 return right;
88
89 if (type_positive_bits(left) < 31 && type_positive_bits(right) < 31)
90 return &int_ctype;
91
92 if (type_positive_bits(left) > type_positive_bits(right))
93 return left;
94 return right;
95 }
96
97 static struct symbol *get_type_symbol(struct expression *expr)
98 {
99 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
126 struct ident *member;
127 struct symbol *sym;
128
129 if (!expr || expr->type != EXPR_DEREF)
130 return NULL;
131
132 member = expr->member;
133 sym = get_type(expr->deref);
134 if (!sym) {
135 // sm_msg("could not find struct type");
136 return NULL;
137 }
138 if (sym->type == SYM_PTR)
139 sym = get_real_base_type(sym);
140 sym = get_member_symbol(sym->symbol_list, member);
141 if (!sym)
142 return NULL;
143 return get_real_base_type(sym);
144 }
145
146 static struct symbol *get_return_type(struct expression *expr)
147 {
148 struct symbol *tmp;
149
150 tmp = get_type(expr->fn);
151 if (!tmp)
152 return NULL;
153 /* this is to handle __builtin_constant_p() */
154 if (tmp->type != SYM_FN)
155 tmp = get_base_type(tmp);
156 return get_real_base_type(tmp);
157 }
158
159 static struct symbol *get_expr_stmt_type(struct statement *stmt)
160 {
161 if (stmt->type != STMT_COMPOUND)
162 return NULL;
163 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
164 if (stmt->type == STMT_LABEL)
165 stmt = stmt->label_statement;
166 if (stmt->type != STMT_EXPRESSION)
167 return NULL;
168 return get_type(stmt->expression);
169 }
386 }
387
388 int is_pointer(struct expression *expr)
389 {
390 return type_is_ptr(get_type(expr));
391 }
392
393 int returns_pointer(struct symbol *sym)
394 {
395 if (!sym)
396 return 0;
397 sym = get_base_type(sym);
398 if (!sym || sym->type != SYM_FN)
399 return 0;
400 sym = get_base_type(sym);
401 if (sym && sym->type == SYM_PTR)
402 return 1;
403 return 0;
404 }
405
406 sval_t sval_type_max(struct symbol *base_type)
407 {
408 sval_t ret;
409
410 if (!base_type || !type_bits(base_type))
411 base_type = &llong_ctype;
412 ret.type = base_type;
413
414 ret.value = (~0ULL) >> (64 - type_positive_bits(base_type));
415 return ret;
416 }
417
418 sval_t sval_type_min(struct symbol *base_type)
419 {
420 sval_t ret;
421
422 if (!base_type || !type_bits(base_type))
423 base_type = &llong_ctype;
424 ret.type = base_type;
425
426 if (type_unsigned(base_type) || is_ptr_type(base_type)) {
427 ret.value = 0;
428 return ret;
429 }
430
431 ret.value = (~0ULL) << type_positive_bits(base_type);
432
433 return ret;
434 }
435
436 int nr_bits(struct expression *expr)
437 {
438 struct symbol *type;
439
440 type = get_type(expr);
441 if (!type)
|
60 }
61
62 static struct symbol *get_binop_type(struct expression *expr)
63 {
64 struct symbol *left, *right;
65
66 left = get_type(expr->left);
67 if (!left)
68 return NULL;
69
70 if (expr->op == SPECIAL_LEFTSHIFT ||
71 expr->op == SPECIAL_RIGHTSHIFT) {
72 if (type_positive_bits(left) < 31)
73 return &int_ctype;
74 return left;
75 }
76 right = get_type(expr->right);
77 if (!right)
78 return NULL;
79
80 if (type_is_fp(left)) {
81 if (type_is_fp(right)) {
82 if (type_bits(left) > type_bits(right))
83 return left;
84 return right;
85 }
86 return left;
87 }
88
89 if (type_is_fp(right)) {
90 if (type_is_fp(left)) {
91 if (type_bits(right) > type_bits(left))
92 return right;
93 return left;
94 }
95 return right;
96 }
97
98 if (expr->op == '-' &&
99 (is_ptr_type(left) && is_ptr_type(right)))
100 return ssize_t_ctype;
101
102 if (left->type == SYM_PTR || left->type == SYM_ARRAY)
103 return left;
104 if (right->type == SYM_PTR || right->type == SYM_ARRAY)
105 return right;
106
107 if (type_positive_bits(left) < 31 && type_positive_bits(right) < 31)
108 return &int_ctype;
109
110 if (type_positive_bits(left) > type_positive_bits(right))
111 return left;
112 return right;
113 }
114
115 static struct symbol *get_type_symbol(struct expression *expr)
116 {
117 if (!expr || expr->type != EXPR_SYMBOL || !expr->symbol)
144 struct ident *member;
145 struct symbol *sym;
146
147 if (!expr || expr->type != EXPR_DEREF)
148 return NULL;
149
150 member = expr->member;
151 sym = get_type(expr->deref);
152 if (!sym) {
153 // sm_msg("could not find struct type");
154 return NULL;
155 }
156 if (sym->type == SYM_PTR)
157 sym = get_real_base_type(sym);
158 sym = get_member_symbol(sym->symbol_list, member);
159 if (!sym)
160 return NULL;
161 return get_real_base_type(sym);
162 }
163
164 static struct symbol *handle__builtin_choose_expr(struct expression *expr)
165 {
166 struct expression *const_expr, *expr1, *expr2;
167 sval_t sval;
168
169 const_expr = get_argument_from_call_expr(expr->args, 0);
170 expr1 = get_argument_from_call_expr(expr->args, 1);
171 expr2 = get_argument_from_call_expr(expr->args, 2);
172
173 if (!get_value(const_expr, &sval) || !expr1 || !expr2)
174 return NULL;
175 if (sval.value)
176 return get_type(expr1);
177 else
178 return get_type(expr2);
179 }
180
181 static struct symbol *get_return_type(struct expression *expr)
182 {
183 struct symbol *tmp;
184
185 if (sym_name_is("__builtin_choose_expr", expr->fn))
186 return handle__builtin_choose_expr(expr);
187
188 tmp = get_type(expr->fn);
189 if (!tmp)
190 return NULL;
191 /* this is to handle __builtin_constant_p() */
192 if (tmp->type != SYM_FN)
193 tmp = get_base_type(tmp);
194 return get_real_base_type(tmp);
195 }
196
197 static struct symbol *get_expr_stmt_type(struct statement *stmt)
198 {
199 if (stmt->type != STMT_COMPOUND)
200 return NULL;
201 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
202 if (stmt->type == STMT_LABEL)
203 stmt = stmt->label_statement;
204 if (stmt->type != STMT_EXPRESSION)
205 return NULL;
206 return get_type(stmt->expression);
207 }
424 }
425
426 int is_pointer(struct expression *expr)
427 {
428 return type_is_ptr(get_type(expr));
429 }
430
431 int returns_pointer(struct symbol *sym)
432 {
433 if (!sym)
434 return 0;
435 sym = get_base_type(sym);
436 if (!sym || sym->type != SYM_FN)
437 return 0;
438 sym = get_base_type(sym);
439 if (sym && sym->type == SYM_PTR)
440 return 1;
441 return 0;
442 }
443
444 static sval_t fp_max(struct symbol *type)
445 {
446 sval_t ret = { .type = type };
447
448 if (type == &float_ctype)
449 ret.fvalue = FLT_MAX;
450 else if (type == &double_ctype)
451 ret.dvalue = DBL_MAX;
452 else
453 ret.ldvalue = LDBL_MAX;
454
455 return ret;
456 }
457
458 sval_t sval_type_max(struct symbol *base_type)
459 {
460 sval_t ret;
461
462 if (type_is_fp(base_type))
463 return fp_max(base_type);
464
465 if (!base_type || !type_bits(base_type))
466 base_type = &llong_ctype;
467 ret.type = base_type;
468
469 ret.value = (~0ULL) >> (64 - type_positive_bits(base_type));
470 return ret;
471 }
472
473 static sval_t fp_min(struct symbol *type)
474 {
475 sval_t ret = { .type = type };
476
477 if (type == &float_ctype)
478 ret.fvalue = -FLT_MAX;
479 else if (type == &double_ctype)
480 ret.dvalue = -DBL_MAX;
481 else
482 ret.ldvalue = -LDBL_MAX;
483
484 return ret;
485 }
486
487 sval_t sval_type_min(struct symbol *base_type)
488 {
489 sval_t ret;
490
491 if (type_is_fp(base_type))
492 return fp_min(base_type);
493
494 if (!base_type || !type_bits(base_type))
495 base_type = &llong_ctype;
496 ret.type = base_type;
497
498 if (type_unsigned(base_type) || is_ptr_type(base_type)) {
499 ret.value = 0;
500 return ret;
501 }
502
503 ret.value = (~0ULL) << type_positive_bits(base_type);
504
505 return ret;
506 }
507
508 int nr_bits(struct expression *expr)
509 {
510 struct symbol *type;
511
512 type = get_type(expr);
513 if (!type)
|