/** * Return the smallest type that both t1 and t2 can be cast to without losing * information. * * e.g. * * join_types(unsignedbv_typet(32), unsignedbv_typet(16))=unsignedbv_typet(32) * join_types(signedbv_typet(16), unsignedbv_typet(16))=signedbv_typet(17) * join_types(signedbv_typet(32), signedbv_typet(32))=signedbv_typet(32) */ typet join_types(const typet &t1, const typet &t2) { // Handle the simple case first... if(t1==t2) { return t1; } // OK, they're not the same type. Are they both bitvectors? if(is_bitvector(t1) && is_bitvector(t2)) { // They are. That makes things easy! There are three cases to consider: // both types are unsigned, both types are signed or there's one of each. bitvector_typet b1=to_bitvector_type(t1); bitvector_typet b2=to_bitvector_type(t2); if(is_unsigned(b1) && is_unsigned(b2)) { // We just need to take the max of their widths. std::size_t width=std::max(b1.get_width(), b2.get_width()); return unsignedbv_typet(width); } else if(is_signed(b1) && is_signed(b2)) { // Again, just need to take the max of the widths. std::size_t width=std::max(b1.get_width(), b2.get_width()); return signedbv_typet(width); } else { // This is the (slightly) tricky case. If we have a signed and an // unsigned type, we're going to return a signed type. And to cast // an unsigned type to a signed type, we need the signed type to be // at least one bit wider than the unsigned type we're casting from. std::size_t signed_width=is_signed(t1) ? b1.get_width() : b2.get_width(); std::size_t unsigned_width=is_signed(t1) ? b2.get_width() : b1.get_width(); // unsigned_width++; std::size_t width=std::max(signed_width, unsigned_width); return signedbv_typet(width); } } std::cerr << "Tried to join types: " << t1.pretty() << " and " << t2.pretty() << '\n'; assert(!"Couldn't join types"); }
static char *add_prenbr(t_format format, char *nbr) { char *pre; char *str; pre = NULL; if (format.pre == '#' && ft_strcmp(nbr, "0") != 0) { format.type == 'x' ? (pre = "0x") : 0; format.type == 'X' ? (pre = "0X") : 0; } if (format.pre == '#' && (format.type == 'o' || format.type == 'O')) nbr[0] != '0' ? (pre = "0") : 0; if (format.sign == ' ' && nbr[0] != '-' && is_unsigned(format.type) == 0) pre = " "; else if (format.type == 'd' || format.type == 'D' || format.type == 'i') { if (format.sign == '+' && nbr[0] != '-') pre = "+"; } if (pre != NULL) { str = nbr; nbr = ft_strjoin(pre, str); free(str); } return (nbr); }
BOOLEAN string_to_unsigned(char *s, unsigned *u) { if(!is_unsigned(s)) { return FALSE; } *u=(unsigned)strtol(s, NULL,10); return TRUE; }
long long get_nb(char c, t_arg *opt, t_env *e) { long long ret; opt->is_u = is_unsigned(c); if (opt->is_u) return (ret = get_unb(c, opt, e)); if ((ft_strchr("di", c) && opt->conv == HH)) ret = (long long)(char)va_arg(e->ap, int); if (ft_strchr("di", c) && opt->conv == H) ret = (long long)(short)va_arg(e->ap, int); if (ft_strchr("di", c) && opt->conv == NOPE) ret = (long long)(int)va_arg(e->ap, int); if (ft_strchr("dDi", c) && opt->conv == L) ret = (long long)va_arg(e->ap, long); if (ft_strchr("dDi", c) && (opt->conv == LL || opt->conv == J || opt->conv == Z)) ret = va_arg(e->ap, long long); return (ret); }
bool Type::is_integral_number() const { return is_signed() || is_unsigned();}
char *get_token(char *lexeme , int mode){ char *token=(char*)calloc(strlen(lexeme)+50,sizeof(char)); //printf("Getting token\n"); if(is_long(lexeme)){ sprintf(token,"%d",LONG); } else if(is_static(lexeme)){ sprintf(token,"%d",STATIC); } else if(is_union(lexeme)){ sprintf(token,"%d",UNION); } else if(is_default(lexeme)){ sprintf(token,"%d",DEFAULT); } else if(is_break(lexeme)){ sprintf(token,"%d",BREAK); } else if(is_case(lexeme)){ sprintf(token,"%d",CASE); } else if(is_continue(lexeme)){ sprintf(token,"%d",CONTINUE); } else if(is_goto(lexeme)){ sprintf(token,"%d",GOTO); } else if(is_struct(lexeme)){ sprintf(token,"%d",STRUCT); } else if(is_const(lexeme)){ sprintf(token,"%d",CONST); } else if(is_void(lexeme)){ sprintf(token,"%d",VOID); } else if(is_switch(lexeme)){ sprintf(token,"%d",SWITCH); } else if(is_for(lexeme)){ sprintf(token,"%d",FOR); } else if(is_while(lexeme)){ sprintf(token,"%d",WHILE); } else if(is_do(lexeme)){ sprintf(token,"%d",DO); } else if(is_return(lexeme)){ sprintf(token,"%d",RETURN); } else if(is_bool(lexeme)){ sprintf(token,"%d",BOOL); } else if(is_char(lexeme)){ sprintf(token,"%d",CHAR); } else if(is_signed(lexeme)){ sprintf(token,"%d",SIGNED); } else if(is_unsigned(lexeme)){ sprintf(token,"%d",UNSIGNED); } else if(is_short(lexeme)){ sprintf(token,"%d",SHORT); } else if(is_int(lexeme)){ sprintf(token,"%d",INT); } else if(is_float(lexeme)){ sprintf(token,"%d",FLOAT); } else if(is_double(lexeme)){ sprintf(token,"%d",DOUBLE); } else if(is_l_square(lexeme)){ sprintf(token,"%d",L_SQUARE); } else if(is_r_square(lexeme)){ sprintf(token,"%d",R_SQUARE); } else if(is_l_paraen(lexeme)){ sprintf(token,"%d",L_PARAEN); } else if(is_r_paraen(lexeme)){ sprintf(token,"%d",R_PARAEN); } else if(is_l_cbrace(lexeme)){ sprintf(token,"%d",L_CBRACE); } else if(is_r_cbrace(lexeme)){ sprintf(token,"%d",R_CBRACE); } else if(is_comma(lexeme)){ sprintf(token,"%d",COMMA); } else if(is_semicol(lexeme)){ sprintf(token,"%d",SEMICOL); } else if(is_eq_eq(lexeme)){ sprintf(token,"%d",EQ_EQ); } else if(is_lesser(lexeme)){ sprintf(token,"%d",LESSER); } else if(is_less_eq(lexeme)){ sprintf(token,"%d",LESS_EQ); } else if(is_div(lexeme)){ sprintf(token,"%d",DIV); } else if(is_greater(lexeme)){ sprintf(token,"%d",GREATER); } else if(is_great_eq(lexeme)){ sprintf(token,"%d",GREAT_EQ); } else if(is_plus_eq(lexeme)){ sprintf(token,"%d",PLUS_EQ); } else if(is_minus_eq(lexeme)){ sprintf(token,"%d",MINUS_EQ); } else if(is_div_eq(lexeme)){ sprintf(token,"%d",DIV_EQ); } else if(is_mult_eq(lexeme)){ sprintf(token,"%d",MULT_EQ); } else if(is_minus_minus(lexeme)){ sprintf(token,"%d",MINUS_MINUS); } else if(is_plus_plus(lexeme)){ sprintf(token,"%d",PLUS_PLUS); } else if(is_percent(lexeme)){ sprintf(token,"%d",PERCENT); } else if(is_div(lexeme)){ sprintf(token,"%d",DIV); } else if(is_mult(lexeme)){ sprintf(token,"%d",MULT); } else if(is_minus(lexeme)){ sprintf(token,"%d",MINUS); } else if(is_plus(lexeme)){ sprintf(token,"%d",PLUS); } else if(is_int_const(lexeme)){ printf("int"); sprintf(token,"%d\t%s",INT_CONST,lexeme); } else if(is_flo_const(lexeme)){ printf("float"); sprintf(token,"%d\t%s",FLO_CONST,lexeme); } else if(is_comment_start(lexeme)){ sprintf(token,"$start"); } else if(is_comment_end(lexeme)){ sprintf(token,"$end"); } else if(is_identifier(lexeme)){ printf("Identifier"); if(mode==1) ht_set( symboltable, lexeme, "1"); sprintf(token,"%d\t%s",IDNTIFIER,lexeme); } else sprintf(token,"%d",NOTOK); return token; }