-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Allow linking with extern vars, fixes #58
- Loading branch information
Showing
12 changed files
with
306 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
void fprintf(void *, char *, ...); | ||
|
||
extern void *stderr; | ||
extern void *stdout; | ||
|
||
int main() | ||
{ | ||
fprintf(stdout, "This is stdout!\n"); | ||
fprintf(stderr, "This is stderr!\n"); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include "extern.h" | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "../codegen/codegen.h" | ||
|
||
#include "parser.h" | ||
#include "var_decl.h" | ||
#include "type.h" | ||
|
||
void extern_debug(int depth, parser_node *node) | ||
{ | ||
node_extern *ex = (node_extern *)node->data; | ||
printtabs(depth); | ||
printf("Extern:\n"); | ||
ex->var_decl->debug(depth + 1, ex->var_decl); | ||
} | ||
|
||
apply_result *extern_apply(parser_node *node, context *ctx) | ||
{ | ||
node_extern *ex = (node_extern *)node->data; | ||
node_var_decl *vd = ex->var_decl->data; | ||
node_type *tp = vd->type->data; | ||
|
||
add_text(ctx, "extern %s", vd->identity); | ||
new_global_symbol(ctx, vd->identity, cc_asprintf("[%s]", vd->identity), tp->type); | ||
|
||
return NULL; | ||
} | ||
|
||
parser_node *parse_extern(typed_token **tkns_ptr) | ||
{ | ||
typed_token *tkn = *tkns_ptr; | ||
if (tkn->type_id == TKN_EXTERN) | ||
{ | ||
tkn = tkn->next; | ||
parser_node *decl = parse_var_decl(&tkn); | ||
if (decl) | ||
{ | ||
*tkns_ptr = tkn; | ||
|
||
parser_node *node = (parser_node *)malloc(sizeof(parser_node)); | ||
node->data = (void *)malloc(sizeof(node_extern)); | ||
node->debug = extern_debug; | ||
node->apply = extern_apply; | ||
node_extern *ex = (node_extern *)node->data; | ||
ex->var_decl = decl; | ||
|
||
return node; | ||
} | ||
} | ||
|
||
return NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#ifndef EXTERN_H | ||
#define EXTERN_H | ||
|
||
#include "../lexer.h" | ||
#include "parser.h" | ||
|
||
parser_node *parse_extern(typed_token **tkns_ptr); | ||
|
||
typedef struct | ||
{ | ||
parser_node *var_decl; | ||
} node_extern; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
section .data | ||
__temp_str_0 db `This is stdout!\n`, 0 | ||
__temp_str_1 db `This is stderr!\n`, 0 | ||
__main_size: equ 80 | ||
section .text | ||
extern stderr | ||
extern stdout | ||
extern fprintf | ||
global main | ||
main: | ||
push rbp | ||
mov rbp, rsp | ||
sub rsp, __main_size | ||
mov rax, rsp | ||
add rax, 0 | ||
mov [rsp+0], rax | ||
mov rax, [stdout] | ||
mov [rsp+8], rax | ||
mov rax, __temp_str_0 | ||
mov [rsp+16], rax | ||
mov rdi, [rsp+8] | ||
mov rsi, [rsp+16] | ||
mov rax, rsp | ||
add rax, 0 | ||
mov [rsp+24], rax | ||
call fprintf | ||
mov rax, rsp | ||
add rax, 0 | ||
mov [rsp+32], rax | ||
mov rax, [stderr] | ||
mov [rsp+40], rax | ||
mov rax, __temp_str_1 | ||
mov [rsp+48], rax | ||
mov rdi, [rsp+40] | ||
mov rsi, [rsp+48] | ||
mov rax, rsp | ||
add rax, 0 | ||
mov [rsp+56], rax | ||
call fprintf | ||
mov rax, 0 | ||
mov rsp, rbp | ||
pop rbp | ||
ret | ||
mov rsp, rbp | ||
pop rbp | ||
ret | ||
extern exit | ||
global _start | ||
_start: | ||
; Pass argc and argv | ||
mov rdi, [rsp] | ||
mov rsi, rsp | ||
add rsi, 8 | ||
call main | ||
mov rdi, rax | ||
call exit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
TKN_VOID | ||
TKN_ID(fprintf) | ||
TKN_L_PAREN | ||
TKN_VOID | ||
TKN_STAR | ||
TKN_COMMA | ||
TKN_CHAR | ||
TKN_STAR | ||
TKN_COMMA | ||
TKN_DOTS | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_EXTERN | ||
TKN_VOID | ||
TKN_STAR | ||
TKN_ID(stderr) | ||
TKN_SEMICOLON | ||
TKN_EXTERN | ||
TKN_VOID | ||
TKN_STAR | ||
TKN_ID(stdout) | ||
TKN_SEMICOLON | ||
TKN_INT | ||
TKN_ID(main) | ||
TKN_L_PAREN | ||
TKN_R_PAREN | ||
TKN_L_BRACE | ||
TKN_ID(fprintf) | ||
TKN_L_PAREN | ||
TKN_ID(stdout) | ||
TKN_COMMA | ||
TKN_LIT_STR(This is stdout! | ||
) | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_ID(fprintf) | ||
TKN_L_PAREN | ||
TKN_ID(stderr) | ||
TKN_COMMA | ||
TKN_LIT_STR(This is stderr! | ||
) | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_RETURN | ||
TKN_LIT_INT(0) | ||
TKN_SEMICOLON | ||
TKN_R_BRACE | ||
TKN_EOF |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
TKN_VOID | ||
TKN_ID(fprintf) | ||
TKN_L_PAREN | ||
TKN_VOID | ||
TKN_STAR | ||
TKN_COMMA | ||
TKN_CHAR | ||
TKN_STAR | ||
TKN_COMMA | ||
TKN_DOTS | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_EXTERN | ||
TKN_VOID | ||
TKN_STAR | ||
TKN_ID(stderr) | ||
TKN_SEMICOLON | ||
TKN_EXTERN | ||
TKN_VOID | ||
TKN_STAR | ||
TKN_ID(stdout) | ||
TKN_SEMICOLON | ||
TKN_INT | ||
TKN_ID(main) | ||
TKN_L_PAREN | ||
TKN_R_PAREN | ||
TKN_L_BRACE | ||
TKN_ID(fprintf) | ||
TKN_L_PAREN | ||
TKN_ID(stdout) | ||
TKN_COMMA | ||
TKN_LIT_STR(This is stdout! | ||
) | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_ID(fprintf) | ||
TKN_L_PAREN | ||
TKN_ID(stderr) | ||
TKN_COMMA | ||
TKN_LIT_STR(This is stderr! | ||
) | ||
TKN_R_PAREN | ||
TKN_SEMICOLON | ||
TKN_RETURN | ||
TKN_LIT_INT(0) | ||
TKN_SEMICOLON | ||
TKN_R_BRACE | ||
TKN_EOF |
Oops, something went wrong.