Skip to content

Commit

Permalink
Allow block comment on assembler
Browse files Browse the repository at this point in the history
  • Loading branch information
tyfkda committed Apr 21, 2024
1 parent 070af5a commit 7ce3ad4
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 71 deletions.
104 changes: 59 additions & 45 deletions src/as/parse_asm.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,47 @@ inline bool is_label_chr(char c) {
return is_label_first_chr(c) || isdigit(c);
}

const char *skip_until_delimiter(const char *p) {
static const char *get_label_end(ParseInfo *info) {
const char *start = info->p;
const char *p = start;
if (*p == '"') {
++p;
for (char c; c = *p, c != '\0'; ++p) {
if (c == '"') {
++p;
for (;;) {
char c = *p;
if (c == '\0') {
parse_error(info, "String not closed");
break;
}

++p;
if (c == '"')
break;
if (c == '\\')
++p;
}
start += 2;
} else {
for (char c; c = *p, !isspace(c) && c != ':' && c != '\0'; ++p)
;
const unsigned char *q = (const unsigned char*)p;
int ucc = 0;
for (;;) {
int uc = *++q;
if (ucc > 0) {
if (!isutf8follow(uc)) {
parse_error(info, "Illegal byte sequence");
return NULL;
}
--ucc;
continue;
}
if ((ucc = isutf8first(uc) - 1) > 0)
continue;
if (!is_label_chr(uc))
break;
}
p = (const char*)q;
}
if (p <= start)
parse_error(info, "Empty label");
return p;
}

Expand All @@ -119,45 +145,18 @@ const Name *unquote_label(const char *p, const char *q) {
return alloc_name(p, q, false);
if (q[-1] != '"' || q == p + 2)
return NULL;
// TODO: Unquote
// TODO: Unescape
return alloc_name(p + 1, q - 1, false);
}

static const Name *parse_label(ParseInfo *info) {
const char *p = info->p;
const char *start = p;
if (*p == '"') {
p = skip_until_delimiter(p);
if (p[-1] != '"' || p == start + 2)
return NULL;

info->p = p;
return alloc_name(start + 1, p - 1, false);
}

unsigned char *q = (unsigned char*)p;
int uc = *q;
int ucc = isutf8first(uc) - 1;
if (!(ucc > 0 || is_label_first_chr(uc)))
const char *start = info->p;
const char *p = get_label_end(info);
if (p == start)
return NULL;

for (;;) {
uc = *++q;
if (ucc > 0) {
if (!isutf8follow(uc)) {
parse_error(info, "Illegal byte sequence");
return NULL;
}
--ucc;
continue;
}
if ((ucc = isutf8first(uc) - 1) > 0)
continue;
if (!is_label_chr(uc))
break;
}
info->p = p = (char*)q;
return alloc_name(start, p, false);
info->p = p;
return unquote_label(start, p);
}

static const Name *parse_section_name(ParseInfo *info) {
Expand Down Expand Up @@ -457,20 +456,38 @@ Expr *parse_expr(ParseInfo *info) {
return parse_add(info);
}

static void check_line_end(ParseInfo *info) {
const char *p = info->p;
for (;;) {
const char *q = block_comment_start(p);
if (q == NULL)
break;
p = block_comment_end(q);
if (p == NULL) {
parse_error(info, "Block comment not closed");
return;
}
}
p = skip_whitespaces(p);
if (*p != '\0' && !(*p == '/' && p[1] == '/')) {
parse_error(info, "Syntax error");
}
}

Line *parse_line(ParseInfo *info) {
Line *line = calloc_or_die(sizeof(*line));
line->label = NULL;
line->inst.op = NOOP;
line->dir = NODIRECTIVE;

const char *p = skip_whitespaces(info->rawline);
const char *q = skip_until_delimiter(p);
info->p = p;
const char *q = get_label_end(info);
const char *r = skip_whitespaces(q);
if (*r == ':') {
const Name *label = unquote_label(p, q);
if (label == NULL) {
parse_error(info, "Illegal label");
err = true;
} else {
info->p = p;
line->label = label;
Expand All @@ -488,10 +505,7 @@ Line *parse_line(ParseInfo *info) {
} else if (*p != '\0') {
info->p = p;
parse_inst(info, &line->inst);
if (*info->p != '\0' && !(*info->p == '/' && info->p[1] == '/')) {
parse_error(info, "Syntax error");
err = true;
}
check_line_end(info);
}
}
return line;
Expand Down
1 change: 0 additions & 1 deletion src/as/parse_asm.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ void parse_error(const ParseInfo *info, const char *message);
void parse_inst(ParseInfo *info, Inst *inst);

bool immediate(const char **pp, int64_t *value);
const char *skip_until_delimiter(const char *p);
const Name *unquote_label(const char *p, const char *q);
Expr *parse_expr(ParseInfo *info);

Expand Down
3 changes: 1 addition & 2 deletions src/cc/arch/aarch64/emit_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,12 @@ static void emit_defun(Function *func) {
label = quote_label(MANGLE(label));
_GLOBL(label);
} else {
emit_comment("%.*s: static func", NAMES(func->name));
label = quote_label(label);
_LOCAL(label);
}
EMIT_ALIGN(4);
#if XCC_TARGET_PLATFORM != XCC_PLATFORM_APPLE
EMIT_ASM(".type", fmt("%.*s", NAMES(func->name)), "@function");
EMIT_ASM(".type", quote_label(fmt_name(func->name)), "@function");
#endif
EMIT_LABEL(label);

Expand Down
3 changes: 1 addition & 2 deletions src/cc/arch/riscv64/emit_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,12 @@ static void emit_defun(Function *func) {
label = quote_label(MANGLE(label));
_GLOBL(label);
} else {
emit_comment("%.*s: static func", NAMES(func->name));
label = quote_label(label);
_LOCAL(label);
}
EMIT_ALIGN(2);
#if XCC_TARGET_PLATFORM != XCC_PLATFORM_APPLE
EMIT_ASM(".type", fmt("%.*s", NAMES(func->name)), "@function");
EMIT_ASM(".type", quote_label(fmt_name(func->name)), "@function");
#endif
EMIT_LABEL(label);

Expand Down
2 changes: 1 addition & 1 deletion src/cc/arch/x64/emit_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static void emit_defun(Function *func) {
_LOCAL(label);
}
#if XCC_TARGET_PLATFORM != XCC_PLATFORM_APPLE
EMIT_ASM(".type", fmt("%.*s", NAMES(func->name)), "@function");
EMIT_ASM(".type", quote_label(fmt_name(func->name)), "@function");
#endif
EMIT_LABEL(label);

Expand Down
2 changes: 1 addition & 1 deletion src/cc/backend/emit_util.c
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ void emit_varinfo(const VarInfo *varinfo, const Initializer *init) {
_LOCAL(label);
}
#if XCC_TARGET_PLATFORM != XCC_PLATFORM_APPLE
EMIT_ASM(".type", fmt("%.*s", NAMES(name)), "@object");
EMIT_ASM(".type", quote_label(fmt_name(name)), "@object");
#endif

if (init != NULL) {
Expand Down
2 changes: 1 addition & 1 deletion src/cc/backend/emit_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ bool function_not_returned(FuncBackend *fnbe);

#if XCC_TARGET_PLATFORM == XCC_PLATFORM_APPLE
#define _RODATA() _SECTION("__DATA,__const")
#define _LOCAL(x) ((void)0)
#define _LOCAL(x) emit_comment("%s: static func", x)
#else
#define _RODATA() _SECTION(".rodata")
#define _LOCAL(x) EMIT_ASM(".local", x)
Expand Down
15 changes: 0 additions & 15 deletions src/cc/frontend/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -360,21 +360,6 @@ static bool read_next_line(void) {
return true;
}

const char *block_comment_start(const char *p) {
const char *q = skip_whitespaces(p);
return (*q == '/' && q[1] == '*') ? q : NULL;
}

const char *block_comment_end(const char *p) {
for (;;) {
p = strchr(p, '*');
if (p == NULL)
return NULL;
if (*(++p) == '/')
return p + 1;
}
}

static const char *skip_block_comment(const char *p) {
for (;;) {
p = block_comment_end(p);
Expand Down
3 changes: 0 additions & 3 deletions src/cc/frontend/lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ Token *alloc_dummy_ident(void);
const char *get_lex_p(void);
void lex_error(const char *p, const char *fmt, ...) __attribute__((noreturn));

const char *block_comment_start(const char *p);
const char *block_comment_end(const char *p);

typedef bool (*LexEofCallback)(void);
LexEofCallback set_lex_eof_callback(LexEofCallback callback);
bool lex_eof_continue(void);
Expand Down
15 changes: 15 additions & 0 deletions src/util/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,21 @@ const char *skip_whitespaces(const char *s) {
return s;
}

const char *block_comment_start(const char *p) {
const char *q = skip_whitespaces(p);
return (*q == '/' && q[1] == '*') ? q : NULL;
}

const char *block_comment_end(const char *p) {
for (;;) {
p = strchr(p, '*');
if (p == NULL)
return NULL;
if (*(++p) == '/')
return p + 1;
}
}

int64_t wrap_value(int64_t value, int size, bool is_unsigned) {
if (is_unsigned) {
switch (size) {
Expand Down
2 changes: 2 additions & 0 deletions src/util/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ bool is_im8(intptr_t x);
bool is_im16(intptr_t x);
bool is_im32(intptr_t x);
const char *skip_whitespaces(const char *s);
const char *block_comment_start(const char *p);
const char *block_comment_end(const char *p);
int64_t wrap_value(int64_t value, int size, bool is_unsigned);

// Container
Expand Down

0 comments on commit 7ce3ad4

Please sign in to comment.