Skip to content

Commit

Permalink
Handle __asm declaration in base text
Browse files Browse the repository at this point in the history
Accept `__asm` to output assembly code directly
not only in function, but also in base text.

String only, no argument (register specifier) allowed.
  • Loading branch information
tyfkda committed Dec 5, 2023
1 parent 74a17ec commit 76999d5
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cc/arch/aarch64/emit_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,11 @@ static void emit_defun(Function *func) {
// Static variables are emitted through global variables.
}

static void emit_asm(Expr *asmstr) {
assert(asmstr->kind == EX_STR);
EMIT_ASM(asmstr->str.buf);
}

void emit_code(Vector *decls) {
for (int i = 0, len = decls->len; i < len; ++i) {
Declaration *decl = decls->data[i];
Expand All @@ -600,6 +605,9 @@ void emit_code(Vector *decls) {
break;
case DCL_VARDECL:
break;
case DCL_ASM:
emit_asm(decl->asmstr);
break;
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/cc/arch/x64/emit_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,11 @@ static void emit_defun(Function *func) {
// Static variables are emitted through global variables.
}

static void emit_asm(Expr *asmstr) {
assert(asmstr->kind == EX_STR);
EMIT_ASM(asmstr->str.buf);
}

void emit_code(Vector *decls) {
for (int i = 0, len = decls->len; i < len; ++i) {
Declaration *decl = decls->data[i];
Expand All @@ -590,6 +595,9 @@ void emit_code(Vector *decls) {
break;
case DCL_VARDECL:
break;
case DCL_ASM:
emit_asm(decl->asmstr);
break;
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/cc/backend/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,8 @@ static void gen_decl(Declaration *decl) {
break;
case DCL_VARDECL:
break;
case DCL_ASM:
break;
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/cc/frontend/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,12 @@ Declaration *new_decl_vardecl(Vector *decls) {
return decl;
}

Declaration *new_decl_asm(Expr *str) {
Declaration *decl = new_decl(DCL_ASM);
decl->asmstr = str;
return decl;
}

// ================================================

// Function
Expand Down
3 changes: 3 additions & 0 deletions src/cc/frontend/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,7 @@ Function *new_func(Type *type, const Name *name, const Vector *params, int flag)
enum DeclKind {
DCL_DEFUN,
DCL_VARDECL,
DCL_ASM,
};

typedef struct Declaration {
Expand All @@ -491,8 +492,10 @@ typedef struct Declaration {
struct {
Vector *decls; // <VarDecl*>
} vardecl;
Expr *asmstr;
};
} Declaration;

Declaration *new_decl_defun(Function *func);
Declaration *new_decl_vardecl(Vector *decls);
Declaration *new_decl_asm(Expr *str);
8 changes: 8 additions & 0 deletions src/cc/frontend/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,14 @@ static Declaration *parse_global_var_decl(Type *rawtype, int storage, Type *type
}

static Declaration *parse_declaration(void) {
Token *tok;
if ((tok = match(TK_ASM)) != NULL) {
Stmt *asm_ = parse_asm(tok);
if (asm_->asm_.arg != NULL)
parse_error(PE_NOFATAL, asm_->token, "no argument required");
return new_decl_asm(asm_->asm_.str);
}

Type *rawtype = NULL;
int storage;
Token *ident;
Expand Down
3 changes: 3 additions & 0 deletions src/wcc/gen_wasm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1690,6 +1690,9 @@ static void gen_decl(Declaration *decl) {
break;
case DCL_VARDECL:
break;
case DCL_ASM:
assert(false);
break;
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/wcc/traverse.c
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,9 @@ static void traverse_decl(Declaration *decl) {
break;
case DCL_VARDECL:
break;
case DCL_ASM:
parse_error(PE_NOFATAL, decl->asmstr->token, "`__asm` not allowed");
break;
}
}

Expand Down

0 comments on commit 76999d5

Please sign in to comment.