Skip to content

Commit 8516867

Browse files
committed
Started on 16-bit operations
1 parent 48f5e4f commit 8516867

File tree

7 files changed

+337
-148
lines changed

7 files changed

+337
-148
lines changed

makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tests/build/%.asm: tests/%.c qcc
1717
mkdir -p tests/build
1818
rm -f tests/build/$(notdir $(basename $<)).asm
1919
rm -f tests/build/$(notdir $(basename $<)).log
20-
cd tests; ../qcc $(notdir $<) -o build/$(notdir $(basename $<)).asm >> build/$(notdir $(basename $<)).log || true
20+
cd tests; ../qcc $(notdir $<) -o build/$(notdir $(basename $<)).asm >> build/$(notdir $(basename $<)).log || rm -f build/$(notdir $(basename $<)).asm || true
2121
cat tests/build/$(notdir $(basename $<)).log
2222

2323
# Assemble test

src/generator.c

Lines changed: 261 additions & 127 deletions
Large diffs are not rendered by default.

src/parser.c

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,8 @@ static struct Node* assignment () {
382382
} else if ((node->type->kind == TY_INT) && (node->Assignment.right->type->kind == TY_POINTER)) {
383383
warning(node->token, "assignment to '%s' from '%s' makes integer from pointer without a cast", node->type->name, node->Assignment.right->type->name);
384384
} else if (get_common_type(node->token, node->type, node->Assignment.right->type)->kind != node->type->kind) {
385-
error(node->token, "cannot assign '%s' to '%s'", node->Assignment.right->type->name, node->type->name);
385+
// error(node->token, "cannot assign '%s' to '%s'", node->Assignment.right->type->name, node->type->name);
386+
warning(node->token, "assignment makes '%s' from '%s' without a cast", node->type->name, node->Assignment.right->type->name);
386387
}
387388
}
388389

@@ -597,20 +598,6 @@ static struct Node* program () {
597598
eat();
598599
}
599600
}
600-
601-
// // TODO this is gross please fix
602-
// struct Token* revert_token = current_token;
603-
// struct Type* type_node = type();
604-
// eat(TK_ID);
605-
// int is_func = peek(TK_LPAREN);
606-
// current_token = revert_token;
607-
608-
// if (is_func) {
609-
// list_add(&node->Program.function_declarations, function_decl());
610-
// } else {
611-
// list_add(&node->Program.global_variables, var_decl());
612-
// eat_kind(TK_SEMICOLON);
613-
// }
614601
}
615602
return node;
616603
}

src/register.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ struct Register {
66
int size;
77
int free;
88

9+
struct Register* parent_reg;
910
struct Register* high_reg;
1011
struct Register* low_reg;
1112
};

src/snippets.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#ifndef _SNIPPETS_H
2+
#define _SNIPPETS_H
3+
4+
void add_u8(FILE* fp, struct Register* left_reg, struct Register* right_reg) {
5+
if (strcmp(left_reg->name, "a") != 0) fprintf(fp, "\tmov a, %s\n", left_reg->name);
6+
fprintf(fp, "\tadd %s\n", right_reg->name);
7+
if (strcmp(left_reg->name, "a") != 0) fprintf(fp, "\tmov %s, a\n", left_reg->name);
8+
}
9+
10+
void sub_u8(FILE* fp, struct Register* left_reg, struct Register* right_reg) {
11+
if (strcmp(left_reg->name, "a") != 0) fprintf(fp, "\tmov a, %s\n", left_reg->name);
12+
fprintf(fp, "\tsub %s\n", right_reg->name);
13+
if (strcmp(left_reg->name, "a") != 0) fprintf(fp, "\tmov %s, a\n", left_reg->name);
14+
}
15+
16+
void and_u8(FILE* fp, struct Register* left_reg, struct Register* right_reg) {
17+
if (strcmp(left_reg->name, "a") != 0) fprintf(fp, "\tmov a, %s\n", left_reg->name);
18+
fprintf(fp, "\tand %s\n", right_reg->name);
19+
if (strcmp(left_reg->name, "a") != 0) fprintf(fp, "\tmov %s, a\n", left_reg->name);
20+
}
21+
22+
void or_u8(FILE* fp, struct Register* left_reg, struct Register* right_reg) {
23+
if (strcmp(left_reg->name, "a") != 0) fprintf(fp, "\tmov a, %s\n", left_reg->name);
24+
fprintf(fp, "\tor %s\n", right_reg->name);
25+
if (strcmp(left_reg->name, "a") != 0) fprintf(fp, "\tmov %s, a\n", left_reg->name);
26+
}
27+
28+
void cmp_u8(FILE* fp, struct Register* left_reg, struct Register* right_reg) {
29+
if (strcmp(left_reg->name, "a") != 0) fprintf(fp, "\tmov a, %s\n", left_reg->name);
30+
fprintf(fp, "\tcmp %s\n", right_reg->name);
31+
if (strcmp(left_reg->name, "a") != 0) fprintf(fp, "\tmov %s, a\n", left_reg->name);
32+
}
33+
34+
void zero_u8(FILE* fp, struct Register* left_reg) {
35+
if (strcmp(left_reg->name, "a") != 0) fprintf(fp, "\tmov a, %s\n", left_reg->name);
36+
fprintf(fp, "\tcmp 0\n");
37+
fprintf(fp, "\tlde\n");
38+
if (strcmp(left_reg->name, "a") != 0) fprintf(fp, "\tmov %s, a\n", left_reg->name);
39+
}
40+
41+
void equal_u8(FILE* fp, struct Register* left_reg, struct Register* right_reg) {
42+
if (strcmp(left_reg->name, "a") != 0) fprintf(fp, "\tmov a, %s\n", left_reg->name);
43+
fprintf(fp, "\tcmp %s\n", right_reg->name);
44+
fprintf(fp, "\tlde\n");
45+
if (strcmp(left_reg->name, "a") != 0) fprintf(fp, "\tmov %s, a\n", left_reg->name);
46+
}
47+
48+
#endif

tests/006.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ char main() {
44
x = 5;
55

66
if (x != 5) return 1;
7-
if (x > 5) return 1;
8-
if (x < 5) return 1;
9-
if (x >= 6) return 1;
10-
if (x <= 4) return 1;
7+
if (x > 5) return 2;
8+
if (x < 5) return 3;
9+
if (x >= 6) return 4;
10+
if (x <= 4) return 5;
1111
if (x == 5) return 0;
1212

13-
return 0;
13+
return 6;
1414
}

tests/015.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Test 16 bit operations
2+
3+
#include "io.h"
4+
5+
char main() {
6+
int x = 1234;
7+
int y = 4321;
8+
int z = 1111;
9+
10+
x = x + y - z;
11+
12+
print_hex_u16(x);
13+
print("\n");
14+
15+
if (x != 4444) return 1;
16+
if (x == 4444) return 0;
17+
18+
return 2;
19+
}

0 commit comments

Comments
 (0)