Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions lib/c.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,17 @@ char *memcpy(char *dest, char *src, int count)
void *memset(void *s, int c, int n)
{
int i = 0;
char *ptr = s;
char *ptr = (char *) s;
char byte_val = (char) c;
for (; i + 4 <= n; i += 4) {
ptr[i] = c;
ptr[i + 1] = c;
ptr[i + 2] = c;
ptr[i + 3] = c;
ptr[i] = byte_val;
ptr[i + 1] = byte_val;
ptr[i + 2] = byte_val;
ptr[i + 3] = byte_val;
}

for (; i < n; i++)
ptr[i] = c;
ptr[i] = byte_val;

return s;
}
Expand Down Expand Up @@ -282,7 +283,7 @@ void __fmtbuf_write_char(fmtbuf_t *fmtbuf, int val)
if (fmtbuf->n <= 1)
return;

char ch = val & 0xFF;
char ch = (char) (val & 0xFF);
fmtbuf->buf[0] = ch;
fmtbuf->buf += 1;
fmtbuf->n -= 1;
Expand Down Expand Up @@ -418,12 +419,12 @@ void __format_to_buf(fmtbuf_t *fmtbuf, char *format, int *var_args)
switch (format[si]) {
case 's':
/* append param pi as string */
l = strlen(v);
__fmtbuf_write_str(fmtbuf, v, l);
l = strlen((char *) v);
__fmtbuf_write_str(fmtbuf, (char *) v, l);
break;
case 'c':
/* append param pi as char */
__fmtbuf_write_char(fmtbuf, v);
__fmtbuf_write_char(fmtbuf, (char) v);
break;
case 'o':
/* append param as octal */
Expand Down Expand Up @@ -551,8 +552,8 @@ char *fgets(char *str, int n, FILE *stream)
str[i] = 0;
return str;
}
/* Not support casting yet. Simply assign it. */
str[i] = c;
/* Use explicit cast for clarity */
str[i] = (char) c;

if (c == '\n') {
str[i + 1] = 0;
Expand Down Expand Up @@ -683,7 +684,7 @@ void *malloc(int size)
__alloc_tail->size = allocated->size;
chunk_clear_freed(__alloc_tail);

void *ptr = __alloc_tail + 1;
void *ptr = (void *) (__alloc_tail + 1);
return ptr;
}

Expand Down Expand Up @@ -750,8 +751,8 @@ void free(void *ptr)
if (!ptr)
return;

char *__ptr = ptr;
chunk_t *cur = __ptr - sizeof(chunk_t);
char *__ptr = (char *) ptr;
chunk_t *cur = (chunk_t *) (__ptr - sizeof(chunk_t));
if (IS_CHUNK_GET_FREED(cur->size)) {
printf("free(): double free detected\n");
abort();
Expand Down
7 changes: 7 additions & 0 deletions src/arm-codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ void update_elf_offset(ph2_ir_t *ph2_ir)
case OP_sign_ext:
elf_offset += 4;
return;
case OP_cast:
elf_offset += 4;
return;
default:
fatal("Unknown opcode");
}
Expand Down Expand Up @@ -439,6 +442,10 @@ void emit_ph2_ir(ph2_ir_t *ph2_ir)
/* TODO: Allow to sign extends to other types */
emit(__sxtb(__AL, rd, rn, 0));
return;
case OP_cast:
/* Generic cast operation - for now, just move the value */
emit(__mov_r(__AL, rd, rn));
return;
default:
fatal("Unknown opcode");
}
Expand Down
4 changes: 3 additions & 1 deletion src/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ typedef enum {
T_typedef,
T_enum,
T_struct,
T_union,
T_sizeof,
T_elipsis, /* ... */
T_switch,
Expand Down Expand Up @@ -184,6 +185,7 @@ typedef enum {
TYPE_int,
TYPE_char,
TYPE_struct,
TYPE_union,
TYPE_typedef
} base_type_t;

Expand Down Expand Up @@ -254,6 +256,7 @@ typedef enum {
/* data type conversion */
OP_trunc,
OP_sign_ext,
OP_cast,

/* entry point of the state machine */
OP_start
Expand Down Expand Up @@ -531,7 +534,6 @@ typedef struct {
int polluted;
} regfile_t;

/* FIXME: replace char[2] with a short data type in ELF header structures */
/* ELF header */
typedef struct {
char e_ident[16];
Expand Down
10 changes: 5 additions & 5 deletions src/elf.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void elf_write_byte(strbuf_t *elf_array, int val)

char e_extract_byte(int v, int b)
{
return (v >> (b << 3)) & 0xFF;
return (char) ((v >> (b << 3)) & 0xFF);
}

void elf_write_int(strbuf_t *elf_array, int val)
Expand Down Expand Up @@ -97,7 +97,7 @@ void elf_generate_header(void)
* 34 | | |
*/
/* ELF file header */
hdr.e_ident[0] = 0x7F; /* ELF magic number */
hdr.e_ident[0] = (char) 0x7F; /* ELF magic number */
hdr.e_ident[1] = 'E';
hdr.e_ident[2] = 'L';
hdr.e_ident[3] = 'F';
Expand All @@ -124,13 +124,13 @@ void elf_generate_header(void)
elf_symtab->size +
elf_strtab->size; /* section header offset */
hdr.e_flags = ELF_FLAGS; /* flags */
hdr.e_ehsize[0] = 0x34; /* header size */
hdr.e_ehsize[0] = (char) 0x34; /* header size */
hdr.e_ehsize[1] = 0;
hdr.e_phentsize[0] = 0x20; /* program header size */
hdr.e_phentsize[0] = (char) 0x20; /* program header size */
hdr.e_phentsize[1] = 0;
hdr.e_phnum[0] = 1; /* number of program headers */
hdr.e_phnum[1] = 0;
hdr.e_shentsize[0] = 0x28; /* section header size */
hdr.e_shentsize[0] = (char) 0x28; /* section header size */
hdr.e_shentsize[1] = 0;
hdr.e_shnum[0] = 6; /* number of section headers */
hdr.e_shnum[1] = 0;
Expand Down
7 changes: 6 additions & 1 deletion src/globals.c
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,8 @@ int hard_mul_div = 0;
type_t *find_type(char *type_name, int flag)
{
for (int i = 0; i < types_idx; i++) {
if (TYPES[i].base_type == TYPE_struct) {
if (TYPES[i].base_type == TYPE_struct ||
TYPES[i].base_type == TYPE_union) {
if (flag == 1)
continue;
if (!strcmp(TYPES[i].type_name, type_name))
Expand Down Expand Up @@ -1398,6 +1399,10 @@ void dump_bb_insn(func_t *func, basic_block_t *bb, bool *at_func_start)
printf("%%%s = sign_ext %%%s, %d", rd->var_name, rs1->var_name,
insn->sz);
break;
case OP_cast:
print_indent(1);
printf("%%%s = cast %%%s", rd->var_name, rs1->var_name);
break;
default:
printf("<Unsupported opcode: %d>", insn->opcode);
break;
Expand Down
138 changes: 132 additions & 6 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

/* Hash table constants */
#define NUM_DIRECTIVES 11
#define NUM_KEYWORDS 15
#define NUM_KEYWORDS 16

/* Preprocessor directive hash table using existing shecc hashmap */
hashmap_t *DIRECTIVE_MAP = NULL;
Expand Down Expand Up @@ -112,6 +112,8 @@ void lex_init_keywords()
token_values[13] = T_default;
names[14] = "continue";
token_values[14] = T_continue;
names[15] = "union";
token_values[15] = T_union;

/* hashmap insertion */
for (int i = 0; i < NUM_KEYWORDS; i++) {
Expand Down Expand Up @@ -203,6 +205,17 @@ bool is_hex(char c)
(c >= 'A' && c <= 'F');
}

int hex_digit_value(char c)
{
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}

bool is_numeric(char buffer[])
{
bool hex = false;
Expand Down Expand Up @@ -330,8 +343,21 @@ token_t lex_token_internal(bool aliasing)
token_str[i++] = next_char;
} while (is_hex(read_char(false)));

} else if (token_str[0] == '0' && ((next_char | 32) == 'b')) {
/* Binary: starts with 0b or 0B */
token_str[i++] = next_char;

read_char(false);
if (next_char != '0' && next_char != '1')
error("Invalid binary literal: expected 0 or 1 after 0b");

do {
token_str[i++] = next_char;
read_char(false);
} while (next_char == '0' || next_char == '1');

} else if (token_str[0] == '0') {
/* Octal: starts with 0 but not followed by 'x' */
/* Octal: starts with 0 but not followed by 'x' or 'b' */
while (is_digit(next_char)) {
if (next_char >= '8')
error("Invalid octal digit: must be in range 0-7");
Expand Down Expand Up @@ -413,8 +439,58 @@ token_t lex_token_internal(bool aliasing)
token_str[i - 1] = '\\';
else if (next_char == '0')
token_str[i - 1] = '\0';
else
abort();
else if (next_char == 'a')
token_str[i - 1] = '\a';
else if (next_char == 'b')
token_str[i - 1] = '\b';
else if (next_char == 'v')
token_str[i - 1] = '\v';
else if (next_char == 'f')
token_str[i - 1] = '\f';
else if (next_char == 'e') /* GNU extension: ESC character */
token_str[i - 1] = 27;
else if (next_char == '?')
token_str[i - 1] = '?';
else if (next_char == 'x') {
/* Hexadecimal escape sequence \xHH */
read_char(false);
if (!is_hex(next_char))
error("Invalid hex escape sequence");
int value = 0;
int count = 0;
while (is_hex(next_char) && count < 2) {
value = (value << 4) + hex_digit_value(next_char);
read_char(false);
count++;
}
token_str[i - 1] = value;
/* Back up one character as we read one too many */
SOURCE->size--;
next_char = SOURCE->elements[SOURCE->size];
} else if (next_char >= '0' && next_char <= '7') {
/* Octal escape sequence \nnn */
int value = next_char - '0';
read_char(false);
if (next_char >= '0' && next_char <= '7') {
value = (value << 3) + (next_char - '0');
read_char(false);
if (next_char >= '0' && next_char <= '7') {
value = (value << 3) + (next_char - '0');
} else {
/* Back up one character */
SOURCE->size--;
next_char = SOURCE->elements[SOURCE->size];
}
} else {
/* Back up one character */
SOURCE->size--;
next_char = SOURCE->elements[SOURCE->size];
}
token_str[i - 1] = value;
} else {
/* Handle unknown escapes gracefully */
token_str[i - 1] = next_char;
}
} else {
token_str[i++] = next_char;
}
Expand Down Expand Up @@ -445,8 +521,58 @@ token_t lex_token_internal(bool aliasing)
token_str[0] = '\\';
else if (next_char == '0')
token_str[0] = '\0';
else
abort();
else if (next_char == 'a')
token_str[0] = '\a';
else if (next_char == 'b')
token_str[0] = '\b';
else if (next_char == 'v')
token_str[0] = '\v';
else if (next_char == 'f')
token_str[0] = '\f';
else if (next_char == 'e') /* GNU extension: ESC character */
token_str[0] = 27;
else if (next_char == '?')
token_str[0] = '?';
else if (next_char == 'x') {
/* Hexadecimal escape sequence \xHH */
read_char(false);
if (!is_hex(next_char))
error("Invalid hex escape sequence");
int value = 0;
int count = 0;
while (is_hex(next_char) && count < 2) {
value = (value << 4) + hex_digit_value(next_char);
read_char(false);
count++;
}
token_str[0] = value;
/* Back up one character as we read one too many */
SOURCE->size--;
next_char = SOURCE->elements[SOURCE->size];
} else if (next_char >= '0' && next_char <= '7') {
/* Octal escape sequence \nnn */
int value = next_char - '0';
read_char(false);
if (next_char >= '0' && next_char <= '7') {
value = (value << 3) + (next_char - '0');
read_char(false);
if (next_char >= '0' && next_char <= '7') {
value = (value << 3) + (next_char - '0');
} else {
/* Back up one character */
SOURCE->size--;
next_char = SOURCE->elements[SOURCE->size];
}
} else {
/* Back up one character */
SOURCE->size--;
next_char = SOURCE->elements[SOURCE->size];
}
token_str[0] = value;
} else {
/* Handle unknown escapes gracefully */
token_str[0] = next_char;
}
} else {
token_str[0] = next_char;
}
Expand Down
Loading