From 94801b782ee6c6202228cb3b5c4a86c9fb5dde35 Mon Sep 17 00:00:00 2001 From: Imre Horvath Date: Fri, 14 Apr 2023 20:40:45 +0200 Subject: [PATCH 1/7] Add 64bit expressions to the assembler --- Makefile | 2 +- src/avra.c | 27 +++++++++++----------- src/avra.h | 21 +++++++++-------- src/device.c | 5 +++- src/directiv.c | 62 ++++++++++++++++++++++++++++---------------------- src/expr.c | 25 ++++++++++---------- src/map.c | 7 +++--- src/mnemonic.c | 42 +++++++++++++++++++++++----------- src/stdextra.c | 11 +++++---- 9 files changed, 118 insertions(+), 84 deletions(-) diff --git a/Makefile b/Makefile index 1f10c59..1654a52 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ OS = linux -VERSION = 1.4.2 +VERSION = 1.4.2b DISTFILES = src \ includes \ diff --git a/src/avra.c b/src/avra.c index 5aec61d..d473caa 100644 --- a/src/avra.c +++ b/src/avra.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "misc.h" #include "args.h" @@ -259,7 +260,7 @@ assemble(struct prog_info *pi) int load_arg_defines(struct prog_info *pi) { - int i; + int64_t expr_val; char *expr; char buff[256]; struct data_list *define; @@ -269,12 +270,12 @@ load_arg_defines(struct prog_info *pi) expr = get_next_token(buff, TERM_EQUAL); if (expr) { /* we reach this, when there is actually a value passed.. */ - if (!get_expr(pi, expr, &i)) { + if (!get_expr(pi, expr, &expr_val)) { return (False); } } else { /* if user didnt specify a value, we default to 1 */ - i = 1; + expr_val = 1; } /* Forward references allowed. But check, if everything is ok... */ if (pi->pass==PASS_1) { /* Pass 1 */ @@ -282,16 +283,16 @@ load_arg_defines(struct prog_info *pi) fprintf(stderr,"Error: Can't define symbol %s twice\n", buff); return (False); } - if (def_const(pi, buff, i)==False) + if (def_const(pi, buff, expr_val)==False) return (False); } else { /* Pass 2 */ - int j; - if (get_constant(pi, buff, &j)==False) { /* Defined in Pass 1 and now missing ? */ + int64_t expr_val2; + if (get_constant(pi, buff, &expr_val2)==False) { /* Defined in Pass 1 and now missing ? */ fprintf(stderr,"Constant %s is missing in pass 2\n",buff); return (False); } - if (i != j) { - fprintf(stderr,"Constant %s changed value from %d in pass1 to %d in pass 2\n",buff,j,i); + if (expr_val != expr_val2) { + fprintf(stderr,"Constant %s changed value from %" PRId64 " in pass1 to %" PRId64 " in pass 2\n",buff,expr_val2,expr_val); return (False); } /* OK. Definition is unchanged */ @@ -458,7 +459,7 @@ print_msg(struct prog_info *pi, int type, char *fmt, ...) int -def_const(struct prog_info *pi, const char *name, int value) +def_const(struct prog_info *pi, const char *name, int64_t value) { struct label *label; label = malloc(sizeof(struct label)); @@ -483,7 +484,7 @@ def_const(struct prog_info *pi, const char *name, int value) } int -def_var(struct prog_info *pi, char *name, int value) +def_var(struct prog_info *pi, char *name, int64_t value) { struct label *label; @@ -651,7 +652,7 @@ test_orglist(struct segment_info *si) /* Get the value of a label. Return FALSE if label was not found */ int -get_label(struct prog_info *pi,char *name,int *value) +get_label(struct prog_info *pi,char *name,int64_t *value) { struct label *label=search_symbol(pi,pi->first_label,name,NULL); if (label==NULL) return False; @@ -660,7 +661,7 @@ get_label(struct prog_info *pi,char *name,int *value) } int -get_constant(struct prog_info *pi,char *name,int *value) +get_constant(struct prog_info *pi,char *name,int64_t *value) { struct label *label=search_symbol(pi,pi->first_constant,name,NULL); if (label==NULL) return False; @@ -669,7 +670,7 @@ get_constant(struct prog_info *pi,char *name,int *value) } int -get_variable(struct prog_info *pi,char *name,int *value) +get_variable(struct prog_info *pi,char *name,int64_t *value) { struct label *label=search_symbol(pi,pi->first_variable,name,NULL); if (label==NULL) return False; diff --git a/src/avra.h b/src/avra.h index 363d91f..b806d5a 100644 --- a/src/avra.h +++ b/src/avra.h @@ -30,6 +30,7 @@ #include #include +#include #define IS_HOR_SPACE(x) ((x == ' ') || (x == 9)) #define IS_LABEL(x) (isalnum(x) || (x == '%') || (x == '_')) @@ -221,7 +222,7 @@ struct def { struct label { struct label *next; char *name; - int value; + int64_t value; }; struct macro { @@ -286,17 +287,17 @@ void init_segment_size(struct prog_info *pi, struct device *device); void rewind_segments(struct prog_info *pi); void advance_ip(struct segment_info *si, int offset); -int def_const(struct prog_info *pi, const char *name, int value); -int def_var(struct prog_info *pi, char *name, int value); +int def_const(struct prog_info *pi, const char *name, int64_t value); +int def_var(struct prog_info *pi, char *name, int64_t value); int def_orglist(struct segment_info *si); int fix_orglist(struct segment_info *si); void fprint_orglist(FILE *file, struct segment_info *si, struct orglist *orglist); void fprint_sef_orglist(FILE *file, struct segment_info *si); void fprint_segments(FILE *file, struct prog_info *pi); int test_orglist(struct segment_info *si); -int get_label(struct prog_info *pi,char *name,int *value); -int get_constant(struct prog_info *pi,char *name,int *value); -int get_variable(struct prog_info *pi,char *name,int *value); +int get_label(struct prog_info *pi,char *name,int64_t *value); +int get_constant(struct prog_info *pi,char *name,int64_t *value); +int get_variable(struct prog_info *pi,char *name,int64_t *value); struct label *test_label(struct prog_info *pi,char *name,char *message); struct label *test_constant(struct prog_info *pi,char *name,char *message); struct label *test_variable(struct prog_info *pi,char *name,char *message); @@ -321,8 +322,8 @@ char *get_next_token(char *scratch, int term); char *fgets_new(struct prog_info *pi, char *s, int size, FILE *stream); /* expr.c */ -int get_expr(struct prog_info *pi, char *data, int *value); -int get_symbol(struct prog_info *pi, char *label_name, int *data); +int get_expr(struct prog_info *pi, char *data, int64_t *value); +int get_symbol(struct prog_info *pi, char *label_name, int64_t *data); int par_length(char *data); /* mnemonic.c */ @@ -374,8 +375,8 @@ char *nocase_strcmp(const char *s, const char *t); char *nocase_strncmp(char *s, char *t, int n); char *nocase_strstr(char *s, char *t); int atox(char *s); -int atoi_n(char *s, int n); -int atox_n(char *s, int n); +int64_t atoi_n(char *s, int n); +int64_t atox_n(char *s, int n); char *my_strlwr(char *in); char *my_strupr(char *in); char *snprint_list(char *buf, size_t limit, const char *const list[]); diff --git a/src/device.c b/src/device.c index 286d64f..a6c40ba 100644 --- a/src/device.c +++ b/src/device.c @@ -28,6 +28,7 @@ #include #include +#include #include "misc.h" #include "avra.h" @@ -206,11 +207,13 @@ predef_dev(struct prog_info *pi) if (def_const(pi, temp, i)==False) return (False); } else { /* Pass 2 */ + int64_t expr_val; int j; - if (get_constant(pi, temp, &j)==False) { /* Defined in Pass 1 and now missing ? */ + if (get_constant(pi, temp, &expr_val)==False) { /* Defined in Pass 1 and now missing ? */ fprintf(stderr,"Constant %s is missing in pass 2\n",temp); return (False); } + j = (int)expr_val; if (i != j) { fprintf(stderr,"Constant %s changed value from %d in pass1 to %d in pass 2\n",temp,j,i); return (False); diff --git a/src/directiv.c b/src/directiv.c index 5627516..8c4df9f 100644 --- a/src/directiv.c +++ b/src/directiv.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "misc.h" #include "args.h" @@ -157,6 +158,7 @@ parse_directive(struct prog_info *pi) { int directive, pragma; int ok = True; + int64_t expr_val; int i; char *next, *data, buf[140]; struct file_info *fi_bak; @@ -183,8 +185,9 @@ parse_directive(struct prog_info *pi) return False; } get_next_token(next, TERM_END); - if (!get_expr(pi, next, &i)) + if (!get_expr(pi, next, &expr_val)) return (False); + i = (int)expr_val; if (i < 0) { print_msg(pi, MSGTYPE_ERROR, ".BYTE directive must have nonnegative operand"); return False; @@ -320,8 +323,9 @@ parse_directive(struct prog_info *pi) while (next) { data = get_next_token(next, TERM_COMMA); if (pi->pass == PASS_2) { - if (!get_expr(pi, next, &i)) + if (!get_expr(pi, next, &expr_val)) return (False); + i = (int)expr_val; if ((i < -32768) || (i > 65535)) print_msg(pi, MSGTYPE_WARNING, "Value %d is out of range (-32768 <= k <= 65535). Will be masked", i); } @@ -362,7 +366,7 @@ parse_directive(struct prog_info *pi) return (True); } get_next_token(data, TERM_END); - if (!get_expr(pi, data, &i)) + if (!get_expr(pi, data, &expr_val)) return (False); if (test_label(pi,next,"%s have already been defined as a label")!=NULL) return (True); @@ -372,16 +376,16 @@ parse_directive(struct prog_info *pi) if (pi->pass==PASS_1) { /* Pass 1 */ if (test_constant(pi,next,"Can't redefine constant %s, use .SET instead")!=NULL) return (True); - if (def_const(pi, next, i)==False) + if (def_const(pi, next, expr_val)==False) return (False); } else { /* Pass 2 */ - int j; - if (get_constant(pi, next, &j)==False) { /* Defined in Pass 1 and now missing ? */ + int64_t expr_val2; + if (get_constant(pi, next, &expr_val2)==False) { /* Defined in Pass 1 and now missing ? */ print_msg(pi, MSGTYPE_ERROR, "Constant %s is missing in pass 2", next); return (False); } - if (i != j) { - print_msg(pi, MSGTYPE_ERROR, "Constant %s changed value from %d in pass1 to %d in pass 2", next,j,i); + if (expr_val != expr_val2) { + print_msg(pi, MSGTYPE_ERROR, "Constant %s changed value from %" PRId64 " in pass1 to %" PRId64 " in pass 2", next,expr_val2,expr_val); return (False); } /* OK. Definition is unchanged */ @@ -449,7 +453,7 @@ parse_directive(struct prog_info *pi) if (data) { print_msg(pi, MSGTYPE_ERROR, ".INCLUDEPATH needs an operand!!!"); get_next_token(data, TERM_END); - if (!get_expr(pi, data, &i)) + if (!get_expr(pi, data, &expr_val)) return (False); } next = term_string(pi, next); @@ -502,8 +506,9 @@ parse_directive(struct prog_info *pi) return (True); } get_next_token(next, TERM_END); - if (!get_expr(pi, next, &i)) + if (!get_expr(pi, next, &expr_val)) return (False); + i = (int)expr_val; fix_orglist(pi->segment); pi->segment->addr = i; /* XXX advance */ def_orglist(pi->segment); @@ -525,14 +530,14 @@ parse_directive(struct prog_info *pi) return (True); } get_next_token(data, TERM_END); - if (!get_expr(pi, data, &i)) + if (!get_expr(pi, data, &expr_val)) return (False); if (test_label(pi,next,"%s have already been defined as a label")!=NULL) return (True); if (test_constant(pi,next,"%s have already been defined as a .EQU constant")!=NULL) return (True); - return (def_var(pi, next, i)); + return (def_var(pi, next, expr_val)); case DIRECTIVE_DEFINE: if (!next) { print_msg(pi, MSGTYPE_ERROR, ".DEFINE needs an operand"); @@ -541,10 +546,10 @@ parse_directive(struct prog_info *pi) data = get_next_token(next, TERM_SPACE); if (data) { get_next_token(data, TERM_END); - if (!get_expr(pi, data, &i)) + if (!get_expr(pi, data, &expr_val)) return (False); } else - i = 1; + expr_val = 1; if (test_label(pi,next,"%s have already been defined as a label")!=NULL) return (True); if (test_variable(pi,next,"%s have already been defined as a .SET variable")!=NULL) @@ -553,16 +558,16 @@ parse_directive(struct prog_info *pi) if (pi->pass==PASS_1) { /* Pass 1 */ if (test_constant(pi,next,"Can't redefine constant %s, use .SET instead")!=NULL) return (True); - if (def_const(pi, next, i)==False) + if (def_const(pi, next, expr_val)==False) return (False); } else { /* Pass 2 */ - int j; - if (get_constant(pi, next, &j)==False) { /* Defined in Pass 1 and now missing ? */ + int64_t expr_val2; + if (get_constant(pi, next, &expr_val2)==False) { /* Defined in Pass 1 and now missing ? */ print_msg(pi, MSGTYPE_ERROR, "Constant %s is missing in pass 2", next); return (False); } - if (i != j) { - print_msg(pi, MSGTYPE_ERROR, "Constant %s changed value from %d in pass1 to %d in pass 2", next,j,i); + if (expr_val != expr_val2) { + print_msg(pi, MSGTYPE_ERROR, "Constant %s changed value from %" PRId64 " in pass1 to %" PRId64 " in pass 2", next,expr_val2,expr_val); return (False); } /* OK. Definition is unchanged */ @@ -682,9 +687,9 @@ parse_directive(struct prog_info *pi) return (True); } get_next_token(next, TERM_END); - if (!get_expr(pi, next, &i)) + if (!get_expr(pi, next, &expr_val)) return (False); - if (i) + if (expr_val) pi->conditional_depth++; else { if (!spool_conditional(pi, False)) @@ -720,11 +725,11 @@ parse_directive(struct prog_info *pi) next++; } } else { - if (!get_expr(pi, next, &i)) { + if (!get_expr(pi, next, &expr_val)) { print_msg(pi, MSGTYPE_APPEND,"\n"); /* Add newline */ return (False); } - print_msg(pi, MSGTYPE_APPEND,"0x%02X",i); + print_msg(pi, MSGTYPE_APPEND,"0x%02" PRIX64,expr_val); } next = data; } @@ -796,6 +801,7 @@ term_string(struct prog_info *pi, char *string) int parse_db(struct prog_info *pi, char *next) { + int64_t expr_val; int i; int count; char *data; @@ -828,11 +834,12 @@ parse_db(struct prog_info *pi, char *next) } } else { if (pi->pass == PASS_2) { - if (!get_expr(pi, next, &i)) + if (!get_expr(pi, next, &expr_val)) return (False); + i = (int)expr_val; if ((i < -128) || (i > 255)) print_msg(pi, MSGTYPE_WARNING, "Value %d is out of range (-128 <= k <= 255). Will be masked", i); - if (pi->list_on) fprintf(pi->list_file, "%02X", i); + if (pi->list_on) fprintf(pi->list_file, "%02d", i); } count++; write_db(pi, (char)i, &prev, count); @@ -920,6 +927,7 @@ spool_conditional(struct prog_info *pi, int only_endif) int check_conditional(struct prog_info *pi, char *pbuff, int *current_depth, int *do_next, int only_endif) { + int64_t expr_val; int i = 0; char *next; char linebuff[LINEBUFFER_LENGTH]; @@ -947,9 +955,9 @@ check_conditional(struct prog_info *pi, char *pbuff, int *current_depth, int *do return (True); } get_next_token(next, TERM_END); - if (!get_expr(pi, next, &i)) + if (!get_expr(pi, next, &expr_val)) return (False); - if (i) + if (expr_val) pi->conditional_depth++; else { if (!spool_conditional(pi, False)) diff --git a/src/expr.c b/src/expr.c index 8d8baea..8aa6938 100644 --- a/src/expr.c +++ b/src/expr.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "misc.h" #include "avra.h" @@ -77,7 +78,7 @@ enum { struct element { struct element *next; - int data; + int64_t data; }; char *function_list[] = { @@ -96,10 +97,10 @@ char *function_list[] = { "log2" }; -int -log_2(int value) +int64_t +log_2(int64_t value) { - int i = 0; + int64_t i = 0; while (value >>= 1) i++; return (i); @@ -197,8 +198,8 @@ test_operator_at_precedence(int operator, int precedence) } -int -calc(struct prog_info *pi, int left, int operator, int right) /* TODO: Sjekk litt resultater */ +int64_t +calc(struct prog_info *pi, int64_t left, int operator, int64_t right) /* TODO: Sjekk litt resultater */ { switch (operator) { case OPERATOR_MUL: @@ -220,9 +221,9 @@ calc(struct prog_info *pi, int left, int operator, int right) /* TODO: Sjekk lit case OPERATOR_SUB: return (left - right); case OPERATOR_SHIFT_LEFT: - return (left << right); + return ((uint64_t)left << right); case OPERATOR_SHIFT_RIGHT: - return ((unsigned)left >> right); + return (left >> right); case OPERATOR_LESS_THAN: return (left < right); case OPERATOR_LESS_OR_EQUAL: @@ -271,8 +272,8 @@ get_function(char *function) return (-1); } -unsigned int -do_function(int function, int value) +uint64_t +do_function(int function, uint64_t value) { switch (function) { case FUNCTION_LOW: @@ -302,7 +303,7 @@ do_function(int function, int value) int -get_symbol(struct prog_info *pi, char *label_name, int *data) +get_symbol(struct prog_info *pi, char *label_name, int64_t *data) { struct label *label; struct macro_call *macro_call; @@ -343,7 +344,7 @@ par_length(char *data) } int -get_expr(struct prog_info *pi, char *data, int *value) +get_expr(struct prog_info *pi, char *data, int64_t *value) { /* Definition */ int ok, end, i, count, first_flag, length, function; diff --git a/src/map.c b/src/map.c index c28638d..81915b5 100644 --- a/src/map.c +++ b/src/map.c @@ -27,6 +27,7 @@ #include #include +#include #include "avra.h" #include "args.h" @@ -50,13 +51,13 @@ write_map_file(struct prog_info *pi) return; } for (label = pi->first_constant; label; label = label->next) - fprintf(fp,"%s%sC\t%04x\t%d\n",label->name,Space(label->name),label->value,label->value); + fprintf(fp,"%s%sC\t%04" PRIx64 "\t%" PRId64 "\n",label->name,Space(label->name),label->value,label->value); for (label = pi->first_variable; label; label = label->next) - fprintf(fp,"%s%sV\t%04x\t%d\n",label->name,Space(label->name),label->value,label->value); + fprintf(fp,"%s%sV\t%04" PRIx64 "\t%" PRId64 "\n",label->name,Space(label->name),label->value,label->value); for (label = pi->first_label; label; label = label->next) - fprintf(fp,"%s%sL\t%04x\t%d\n",label->name,Space(label->name),label->value,label->value); + fprintf(fp,"%s%sL\t%04" PRIx64 "\t%" PRId64 "\n",label->name,Space(label->name),label->value,label->value); fprintf(fp,"\n"); fclose(fp); diff --git a/src/mnemonic.c b/src/mnemonic.c index a51871a..8af4589 100644 --- a/src/mnemonic.c +++ b/src/mnemonic.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "misc.h" #include "avra.h" @@ -346,6 +347,7 @@ int parse_mnemonic(struct prog_info *pi) { int mnemonic; + int64_t expr_val; int i; int opcode = 0; int opcode2 = 0; @@ -426,8 +428,9 @@ parse_mnemonic(struct prog_info *pi) if (mnemonic >= MNEMONIC_TST) opcode |= ((i & 0x10) << 5) | (i & 0x0f); } else if (mnemonic <= MNEMONIC_RCALL) { - if (!get_expr(pi, operand1, &i)) + if (!get_expr(pi, operand1, &expr_val)) return (False); + i = (int)expr_val; i -= pi->cseg->addr + 1; if (mnemonic <= MNEMONIC_BRID) { if ((i < -64) || (i > 63)) @@ -439,8 +442,9 @@ parse_mnemonic(struct prog_info *pi) opcode = i & 0x0fff; } } else if (mnemonic <= MNEMONIC_CALL) { - if (!get_expr(pi, operand1, &i)) + if (!get_expr(pi, operand1, &expr_val)) return (False); + i = (int)expr_val; if ((i < 0) || (i > 4194303)) print_msg(pi, MSGTYPE_ERROR, "Address out of range (0 <= k <= 4194303)"); opcode = ((i & 0x3e0000) >> 13) | ((i & 0x010000) >> 16); @@ -450,8 +454,9 @@ parse_mnemonic(struct prog_info *pi) if (!get_bitnum(pi, operand1, &i)) return (False); opcode = i; - if (!get_expr(pi, operand2, &i)) + if (!get_expr(pi, operand2, &expr_val)) return (False); + i = (int)expr_val; i -= pi->cseg->addr + 1; if ((i < -64) || (i > 63)) print_msg(pi, MSGTYPE_ERROR, "Branch out of range (-64 <= k <= 63)"); @@ -493,8 +498,9 @@ parse_mnemonic(struct prog_info *pi) if (!((i == 24) || (i == 26) || (i == 28) || (i == 30))) print_msg(pi, MSGTYPE_ERROR, "%s can only use registers R24, R26, R28 or R30", instruction_list[mnemonic].mnemonic); opcode = ((i - 24) / 2) << 4; - if (!get_expr(pi, operand2, &i)) + if (!get_expr(pi, operand2, &expr_val)) return (False); + i = (int)expr_val; if ((i < 0) || (i > 63)) print_msg(pi, MSGTYPE_ERROR, "Constant out of range (0 <= k <= 63)"); opcode |= ((i & 0x30) << 2) | (i & 0x0f); @@ -503,8 +509,9 @@ parse_mnemonic(struct prog_info *pi) if (i < 16) print_msg(pi, MSGTYPE_ERROR, "%s can only use a high register (r16 - r31)", instruction_list[mnemonic].mnemonic); opcode = (i & 0x0f) << 4; - if (!get_expr(pi, operand2, &i)) + if (!get_expr(pi, operand2, &expr_val)) return (False); + i = (int)expr_val; if ((i < -128) || (i > 255)) print_msg(pi, MSGTYPE_WARNING, "Constant out of range (-128 <= k <= 255). Will be masked"); if (mnemonic == MNEMONIC_CBR) @@ -519,22 +526,25 @@ parse_mnemonic(struct prog_info *pi) } else if (mnemonic == MNEMONIC_IN) { i = get_register(pi, operand1); opcode = i << 4; - if (!get_expr(pi, operand2, &i)) + if (!get_expr(pi, operand2, &expr_val)) return (False); + i = (int)expr_val; if ((i < 0) || (i > 63)) print_msg(pi, MSGTYPE_ERROR, "I/O out of range (0 <= P <= 63)"); opcode |= ((i & 0x30) << 5) | (i & 0x0f); } else if (mnemonic == MNEMONIC_OUT) { - if (!get_expr(pi, operand1, &i)) + if (!get_expr(pi, operand1, &expr_val)) return (False); + i = (int)expr_val; if ((i < 0) || (i > 63)) print_msg(pi, MSGTYPE_ERROR, "I/O out of range (0 <= P <= 63)"); opcode = ((i & 0x30) << 5) | (i & 0x0f); i = get_register(pi, operand2); opcode |= i << 4; } else if (mnemonic <= MNEMONIC_CBI) { - if (!get_expr(pi, operand1, &i)) + if (!get_expr(pi, operand1, &expr_val)) return (False); + i = (int)expr_val; if ((i < 0) || (i > 31)) print_msg(pi, MSGTYPE_ERROR, "I/O out of range (0 <= P <= 31)"); opcode = i << 3; @@ -549,8 +559,9 @@ parse_mnemonic(struct prog_info *pi) mnemonic = MNEMONIC_LDS_AVR8L; opcode &= 0x00f0; } - if (!get_expr(pi, operand2, &i)) + if (!get_expr(pi, operand2, &expr_val)) return (False); + i = (int)expr_val; if (pi->device->flag & DF_AVR8L) { if ((i < 0x40) || (i > 0xbf)) print_msg(pi, MSGTYPE_ERROR, "SRAM out of range (0x40 <= k <= 0xbf)"); @@ -562,8 +573,9 @@ parse_mnemonic(struct prog_info *pi) instruction_long = True; } } else if (mnemonic == MNEMONIC_STS) { - if (!get_expr(pi, operand1, &i)) + if (!get_expr(pi, operand1, &expr_val)) return (False); + i = (int)expr_val; /* AVR8L has one word STS. High nibble of k in funny order */ if (pi->device->flag & DF_AVR8L) { mnemonic = MNEMONIC_STS_AVR8L; @@ -604,8 +616,9 @@ parse_mnemonic(struct prog_info *pi) print_msg(pi, MSGTYPE_ERROR, "Garbage in second operand (%s)", operand2); return (False); } - if (!get_expr(pi, &operand2[i + 1], &i)) + if (!get_expr(pi, &operand2[i + 1], &expr_val)) return (False); + i = (int)expr_val; if ((i < 0) || (i > 63)) print_msg(pi, MSGTYPE_ERROR, "Displacement out of range (0 <= q <= 63)"); opcode |= ((i & 0x20) << 8) | ((i & 0x18) << 7) | (i & 0x07); @@ -622,8 +635,9 @@ parse_mnemonic(struct prog_info *pi) print_msg(pi, MSGTYPE_ERROR, "Garbage in first operand (%s)", operand1); return (False); } - if (!get_expr(pi, &operand1[i + 1], &i)) + if (!get_expr(pi, &operand1[i + 1], &expr_val)) return (False); + i = (int)expr_val; if ((i < 0) || (i > 63)) print_msg(pi, MSGTYPE_ERROR, "Displacement out of range (0 <= q <= 63)"); opcode = ((i & 0x20) << 8) | ((i & 0x18) << 7) | (i & 0x07); @@ -736,8 +750,10 @@ get_register(struct prog_info *pi, char *data) int get_bitnum(struct prog_info *pi, char *data, int *ret) { - if (!get_expr(pi, data, ret)) + int64_t expr_val; + if (!get_expr(pi, data, &expr_val)) return (False); + *ret = (int)expr_val; if ((*ret < 0) || (*ret > 7)) { print_msg(pi, MSGTYPE_ERROR, "Operand out of range (0 <= s <= 7)"); return (False); diff --git a/src/stdextra.c b/src/stdextra.c index 968618e..da8e18b 100644 --- a/src/stdextra.c +++ b/src/stdextra.c @@ -27,6 +27,7 @@ #include #include +#include #include "misc.h" @@ -98,10 +99,11 @@ atox(char *s) } /* n ascii chars to int. */ -int +int64_t atoi_n(char *s, int n) { - int i = 0, ret = 0; + int i = 0; + int64_t ret = 0; while ((s[i] != '\0') && n) { ret = 10 * ret + (s[i] - '0'); @@ -112,10 +114,11 @@ atoi_n(char *s, int n) } /* n ascii chars to hex, where 0 < n <= 8. Ignores "0x". */ -int +int64_t atox_n(char *s, int n) { - int i = 0, ret = 0; + int i = 0; + int64_t ret = 0; while ((s[i] != '\0') && n) { ret <<= 4; From e72ca96f86356237786c276bea77d44e5ff71304 Mon Sep 17 00:00:00 2001 From: Imre Horvath Date: Sat, 15 Apr 2023 11:44:28 +0200 Subject: [PATCH 2/7] Tidy up 64bit integer includes --- src/avra.c | 1 + src/avra.h | 4 ++-- src/coff.c | 2 +- src/device.c | 2 +- src/directiv.c | 1 + src/expr.c | 2 +- src/mnemonic.c | 2 +- src/stdextra.c | 5 +++-- 8 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/avra.c b/src/avra.c index d473caa..c041f5e 100644 --- a/src/avra.c +++ b/src/avra.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include "misc.h" diff --git a/src/avra.h b/src/avra.h index b806d5a..03f9714 100644 --- a/src/avra.h +++ b/src/avra.h @@ -30,7 +30,7 @@ #include #include -#include +#include #define IS_HOR_SPACE(x) ((x == ' ') || (x == 9)) #define IS_LABEL(x) (isalnum(x) || (x == '%') || (x == '_')) @@ -374,7 +374,7 @@ char *Space(char *n); char *nocase_strcmp(const char *s, const char *t); char *nocase_strncmp(char *s, char *t, int n); char *nocase_strstr(char *s, char *t); -int atox(char *s); +int64_t atox(char *s); int64_t atoi_n(char *s, int n); int64_t atox_n(char *s, int n); char *my_strlwr(char *in); diff --git a/src/coff.c b/src/coff.c index 657592a..3fd531b 100644 --- a/src/coff.c +++ b/src/coff.c @@ -574,7 +574,7 @@ parse_stabs(struct prog_info *pi, char *p) if (*p2 == '0') - TypeCode = atox(p2); /* presume to be hex 0x */ + TypeCode = (int)atox(p2); /* presume to be hex 0x */ else TypeCode = atoi(p2); diff --git a/src/device.c b/src/device.c index a6c40ba..6839020 100644 --- a/src/device.c +++ b/src/device.c @@ -28,7 +28,7 @@ #include #include -#include +#include #include "misc.h" #include "avra.h" diff --git a/src/directiv.c b/src/directiv.c index 8c4df9f..dbae150 100644 --- a/src/directiv.c +++ b/src/directiv.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include "misc.h" diff --git a/src/expr.c b/src/expr.c index 8aa6938..ab7049b 100644 --- a/src/expr.c +++ b/src/expr.c @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include "misc.h" #include "avra.h" diff --git a/src/mnemonic.c b/src/mnemonic.c index 8af4589..7fd47cf 100644 --- a/src/mnemonic.c +++ b/src/mnemonic.c @@ -29,7 +29,7 @@ #include #include #include -#include +#include #include "misc.h" #include "avra.h" diff --git a/src/stdextra.c b/src/stdextra.c index da8e18b..31d660f 100644 --- a/src/stdextra.c +++ b/src/stdextra.c @@ -80,10 +80,11 @@ nocase_strstr(char *s, char *t) } /* Convert ascii to hex. Ignores "0x". */ -int +int64_t atox(char *s) { - int i = 0, ret = 0; + int i = 0; + int64_t ret = 0; while (s[i] != '\0') { ret <<= 4; From b8644945c448392e0e56430a51db67a2c6b5eaf6 Mon Sep 17 00:00:00 2001 From: Imre Horvath Date: Sat, 15 Apr 2023 13:02:39 +0200 Subject: [PATCH 3/7] Add regression tests for 64bit expr values --- tests/regression/64bit-expr-values/test.asm | 47 +++++++++++++++++++ .../64bit-expr-values/test.hex.expected | 3 ++ 2 files changed, 50 insertions(+) create mode 100644 tests/regression/64bit-expr-values/test.asm create mode 100644 tests/regression/64bit-expr-values/test.hex.expected diff --git a/tests/regression/64bit-expr-values/test.asm b/tests/regression/64bit-expr-values/test.asm new file mode 100644 index 0000000..faac4dd --- /dev/null +++ b/tests/regression/64bit-expr-values/test.asm @@ -0,0 +1,47 @@ +.device atmega328p + +; Multiply test (result exceeds 32bit integer range) + +.equ F_CPU = 16000000 + +.if 1000*F_CPU/1000000 != 16000 + .error "1000*F_CPU/1000000 != 16000" +.endif + +; 64bit contant tests + +.equ BIG_CONST = 0x1A2B3C4D5E6F7182 + +.if LOW(BIG_CONST) != 0x82 + .error "LOW(BIG_CONST) != 0x82" +.endif + +.if HIGH(BIG_CONST) != 0x71 + .error "HIGH(BIG_CONST) != 0x71" +.endif + +.if BYTE3(BIG_CONST) != 0x6F + .error "BYTE3(BIG_CONST) != 0x6F" +.endif + +.if BYTE4(BIG_CONST) != 0x5E + .error "BYTE4(BIG_CONST) != 0x5E" +.endif + +.if LOW(BIG_CONST>>32) != 0x4D + .error "LOW(BIG_CONST>>32) != 0x4D" +.endif + +.if HIGH(BIG_CONST>>32) != 0x3C + .error "HIGH(BIG_CONST>>32) != 0x3C" +.endif + +.if BYTE3(BIG_CONST>>32) != 0x2B + .error "BYTE3(BIG_CONST>>32) != 0x2B" +.endif + +.if BYTE4(BIG_CONST>>32) != 0x1A + .error "BYTE4(BIG_CONST>>32) != 0x1A" +.endif + +nop \ No newline at end of file diff --git a/tests/regression/64bit-expr-values/test.hex.expected b/tests/regression/64bit-expr-values/test.hex.expected new file mode 100644 index 0000000..1e97d40 --- /dev/null +++ b/tests/regression/64bit-expr-values/test.hex.expected @@ -0,0 +1,3 @@ +:020000020000FC +:020000000000FE +:00000001FF From 0e6e4435071c28578b36d3f4045834a01abf17f0 Mon Sep 17 00:00:00 2001 From: Imre Horvath Date: Sat, 15 Apr 2023 19:21:06 +0200 Subject: [PATCH 4/7] Add missing newline to the end of test.asm --- tests/regression/64bit-expr-values/test.asm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/regression/64bit-expr-values/test.asm b/tests/regression/64bit-expr-values/test.asm index faac4dd..a56beba 100644 --- a/tests/regression/64bit-expr-values/test.asm +++ b/tests/regression/64bit-expr-values/test.asm @@ -44,4 +44,4 @@ .error "BYTE4(BIG_CONST>>32) != 0x1A" .endif -nop \ No newline at end of file +nop From bb367e12de675fbe718880258300e8853a852352 Mon Sep 17 00:00:00 2001 From: Imre Horvath Date: Tue, 9 May 2023 01:23:32 +0200 Subject: [PATCH 5/7] Add appropriate type for out parameters --- src/coff.c | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/coff.c b/src/coff.c index 3fd531b..636bc8a 100644 --- a/src/coff.c +++ b/src/coff.c @@ -32,6 +32,7 @@ #include #include +#include #include #include #include @@ -706,7 +707,7 @@ int stab_add_lineno(struct prog_info *pi, int LineNumber, char *pLabel, char *pFunction) { - int Address; + int64_t Address; struct lineno *pln; struct syment *pEntry; union auxent *pAux; @@ -754,7 +755,7 @@ int stab_add_lbracket(struct prog_info *pi, int Level, char *pLabel, char *pFunction) { - int Address; + int64_t Address; struct syment *pEntry; union auxent *pAux; @@ -790,7 +791,7 @@ int stab_add_rbracket(struct prog_info *pi, int Level, char *pLabel, char *pFunction) { - int Address; + int64_t Address; struct syment *pEntry; union auxent *pAux; @@ -907,7 +908,8 @@ int stab_add_function(struct prog_info *pi, char *pName, char *pLabel) { - int n, Address; + int n; + int64_t Address; unsigned short CoffType, Type; struct syment *pEntry; char *pType; @@ -991,7 +993,8 @@ int stab_add_global(struct prog_info *pi, char *pName, char *pType) { - int n, Address, IsArray, SymbolIndex; + int n, IsArray, SymbolIndex; + int64_t Address; unsigned short CoffType, Type; struct syment *pEntry; char *p; @@ -1129,7 +1132,8 @@ int stab_add_static_symbol(struct prog_info *pi, char *pName, char *pType, char *pLabel) { - int n, Address; + int n; + int64_t Address; unsigned short CoffType, Type; struct syment *pEntry; From 758fb9d23980f58e154b0c26b5cf4a1fc66f5c7f Mon Sep 17 00:00:00 2001 From: Imre Horvath Date: Fri, 26 May 2023 22:46:25 +0200 Subject: [PATCH 6/7] Undo unnecessary variable renaming --- src/avra.c | 16 +++++------ src/avra.h | 2 +- src/device.c | 9 +++---- src/directiv.c | 73 +++++++++++++++++++++++--------------------------- src/mnemonic.c | 47 ++++++++++++-------------------- 5 files changed, 63 insertions(+), 84 deletions(-) diff --git a/src/avra.c b/src/avra.c index c041f5e..e203ba8 100644 --- a/src/avra.c +++ b/src/avra.c @@ -261,7 +261,7 @@ assemble(struct prog_info *pi) int load_arg_defines(struct prog_info *pi) { - int64_t expr_val; + int64_t i; char *expr; char buff[256]; struct data_list *define; @@ -271,12 +271,12 @@ load_arg_defines(struct prog_info *pi) expr = get_next_token(buff, TERM_EQUAL); if (expr) { /* we reach this, when there is actually a value passed.. */ - if (!get_expr(pi, expr, &expr_val)) { + if (!get_expr(pi, expr, &i)) { return (False); } } else { /* if user didnt specify a value, we default to 1 */ - expr_val = 1; + i = 1; } /* Forward references allowed. But check, if everything is ok... */ if (pi->pass==PASS_1) { /* Pass 1 */ @@ -284,16 +284,16 @@ load_arg_defines(struct prog_info *pi) fprintf(stderr,"Error: Can't define symbol %s twice\n", buff); return (False); } - if (def_const(pi, buff, expr_val)==False) + if (def_const(pi, buff, i)==False) return (False); } else { /* Pass 2 */ - int64_t expr_val2; - if (get_constant(pi, buff, &expr_val2)==False) { /* Defined in Pass 1 and now missing ? */ + int64_t j; + if (get_constant(pi, buff, &j)==False) { /* Defined in Pass 1 and now missing ? */ fprintf(stderr,"Constant %s is missing in pass 2\n",buff); return (False); } - if (expr_val != expr_val2) { - fprintf(stderr,"Constant %s changed value from %" PRId64 " in pass1 to %" PRId64 " in pass 2\n",buff,expr_val2,expr_val); + if (i != j) { + fprintf(stderr,"Constant %s changed value from %" PRId64 " in pass1 to %" PRId64 " in pass 2\n",buff,j,i); return (False); } /* OK. Definition is unchanged */ diff --git a/src/avra.h b/src/avra.h index 03f9714..1b89905 100644 --- a/src/avra.h +++ b/src/avra.h @@ -330,7 +330,7 @@ int par_length(char *data); int parse_mnemonic(struct prog_info *pi); int get_mnemonic_type(struct prog_info *pi); int get_register(struct prog_info *pi, char *data); -int get_bitnum(struct prog_info *pi, char *data, int *ret); +int get_bitnum(struct prog_info *pi, char *data, int64_t *ret); int get_indirect(struct prog_info *pi, char *operand); int is_supported(struct prog_info *pi, char *name); int count_supported_instructions(int flags); diff --git a/src/device.c b/src/device.c index 6839020..e453bdb 100644 --- a/src/device.c +++ b/src/device.c @@ -29,6 +29,7 @@ #include #include #include +#include #include "misc.h" #include "avra.h" @@ -207,15 +208,13 @@ predef_dev(struct prog_info *pi) if (def_const(pi, temp, i)==False) return (False); } else { /* Pass 2 */ - int64_t expr_val; - int j; - if (get_constant(pi, temp, &expr_val)==False) { /* Defined in Pass 1 and now missing ? */ + int64_t j; + if (get_constant(pi, temp, &j)==False) { /* Defined in Pass 1 and now missing ? */ fprintf(stderr,"Constant %s is missing in pass 2\n",temp); return (False); } - j = (int)expr_val; if (i != j) { - fprintf(stderr,"Constant %s changed value from %d in pass1 to %d in pass 2\n",temp,j,i); + fprintf(stderr,"Constant %s changed value from %" PRId64 " in pass1 to %d in pass 2\n",temp,j,i); return (False); } /* OK. definition is unchanged */ diff --git a/src/directiv.c b/src/directiv.c index dbae150..d038fd3 100644 --- a/src/directiv.c +++ b/src/directiv.c @@ -159,8 +159,7 @@ parse_directive(struct prog_info *pi) { int directive, pragma; int ok = True; - int64_t expr_val; - int i; + int64_t i; char *next, *data, buf[140]; struct file_info *fi_bak; @@ -186,9 +185,8 @@ parse_directive(struct prog_info *pi) return False; } get_next_token(next, TERM_END); - if (!get_expr(pi, next, &expr_val)) + if (!get_expr(pi, next, &i)) return (False); - i = (int)expr_val; if (i < 0) { print_msg(pi, MSGTYPE_ERROR, ".BYTE directive must have nonnegative operand"); return False; @@ -324,18 +322,17 @@ parse_directive(struct prog_info *pi) while (next) { data = get_next_token(next, TERM_COMMA); if (pi->pass == PASS_2) { - if (!get_expr(pi, next, &expr_val)) + if (!get_expr(pi, next, &i)) return (False); - i = (int)expr_val; if ((i < -32768) || (i > 65535)) - print_msg(pi, MSGTYPE_WARNING, "Value %d is out of range (-32768 <= k <= 65535). Will be masked", i); + print_msg(pi, MSGTYPE_WARNING, "Value %" PRId64 " is out of range (-32768 <= k <= 65535). Will be masked", i); } if (pi->pass == PASS_2) { if (pi->list_line && pi->list_on) { fprintf(pi->list_file, " %s\n", pi->list_line); pi->list_line = NULL; fprintf(pi->list_file, "%c:%06lx %04x\n", - pi->segment->ident, pi->segment->addr, i); + pi->segment->ident, pi->segment->addr, (int)i); } if (pi->segment == pi->eseg) { write_ee_byte(pi, pi->eseg->addr, (unsigned char)i); @@ -367,7 +364,7 @@ parse_directive(struct prog_info *pi) return (True); } get_next_token(data, TERM_END); - if (!get_expr(pi, data, &expr_val)) + if (!get_expr(pi, data, &i)) return (False); if (test_label(pi,next,"%s have already been defined as a label")!=NULL) return (True); @@ -377,16 +374,16 @@ parse_directive(struct prog_info *pi) if (pi->pass==PASS_1) { /* Pass 1 */ if (test_constant(pi,next,"Can't redefine constant %s, use .SET instead")!=NULL) return (True); - if (def_const(pi, next, expr_val)==False) + if (def_const(pi, next, i)==False) return (False); } else { /* Pass 2 */ - int64_t expr_val2; - if (get_constant(pi, next, &expr_val2)==False) { /* Defined in Pass 1 and now missing ? */ + int64_t j; + if (get_constant(pi, next, &j)==False) { /* Defined in Pass 1 and now missing ? */ print_msg(pi, MSGTYPE_ERROR, "Constant %s is missing in pass 2", next); return (False); } - if (expr_val != expr_val2) { - print_msg(pi, MSGTYPE_ERROR, "Constant %s changed value from %" PRId64 " in pass1 to %" PRId64 " in pass 2", next,expr_val2,expr_val); + if (i != j) { + print_msg(pi, MSGTYPE_ERROR, "Constant %s changed value from %" PRId64 " in pass1 to %" PRId64 " in pass 2", next,j,i); return (False); } /* OK. Definition is unchanged */ @@ -454,7 +451,7 @@ parse_directive(struct prog_info *pi) if (data) { print_msg(pi, MSGTYPE_ERROR, ".INCLUDEPATH needs an operand!!!"); get_next_token(data, TERM_END); - if (!get_expr(pi, data, &expr_val)) + if (!get_expr(pi, data, &i)) return (False); } next = term_string(pi, next); @@ -507,9 +504,8 @@ parse_directive(struct prog_info *pi) return (True); } get_next_token(next, TERM_END); - if (!get_expr(pi, next, &expr_val)) + if (!get_expr(pi, next, &i)) return (False); - i = (int)expr_val; fix_orglist(pi->segment); pi->segment->addr = i; /* XXX advance */ def_orglist(pi->segment); @@ -531,14 +527,14 @@ parse_directive(struct prog_info *pi) return (True); } get_next_token(data, TERM_END); - if (!get_expr(pi, data, &expr_val)) + if (!get_expr(pi, data, &i)) return (False); if (test_label(pi,next,"%s have already been defined as a label")!=NULL) return (True); if (test_constant(pi,next,"%s have already been defined as a .EQU constant")!=NULL) return (True); - return (def_var(pi, next, expr_val)); + return (def_var(pi, next, i)); case DIRECTIVE_DEFINE: if (!next) { print_msg(pi, MSGTYPE_ERROR, ".DEFINE needs an operand"); @@ -547,10 +543,10 @@ parse_directive(struct prog_info *pi) data = get_next_token(next, TERM_SPACE); if (data) { get_next_token(data, TERM_END); - if (!get_expr(pi, data, &expr_val)) + if (!get_expr(pi, data, &i)) return (False); } else - expr_val = 1; + i = 1; if (test_label(pi,next,"%s have already been defined as a label")!=NULL) return (True); if (test_variable(pi,next,"%s have already been defined as a .SET variable")!=NULL) @@ -559,16 +555,16 @@ parse_directive(struct prog_info *pi) if (pi->pass==PASS_1) { /* Pass 1 */ if (test_constant(pi,next,"Can't redefine constant %s, use .SET instead")!=NULL) return (True); - if (def_const(pi, next, expr_val)==False) + if (def_const(pi, next, i)==False) return (False); } else { /* Pass 2 */ - int64_t expr_val2; - if (get_constant(pi, next, &expr_val2)==False) { /* Defined in Pass 1 and now missing ? */ + int64_t j; + if (get_constant(pi, next, &j)==False) { /* Defined in Pass 1 and now missing ? */ print_msg(pi, MSGTYPE_ERROR, "Constant %s is missing in pass 2", next); return (False); } - if (expr_val != expr_val2) { - print_msg(pi, MSGTYPE_ERROR, "Constant %s changed value from %" PRId64 " in pass1 to %" PRId64 " in pass 2", next,expr_val2,expr_val); + if (i != j) { + print_msg(pi, MSGTYPE_ERROR, "Constant %s changed value from %" PRId64 " in pass1 to %" PRId64 " in pass 2", next,j,i); return (False); } /* OK. Definition is unchanged */ @@ -688,9 +684,9 @@ parse_directive(struct prog_info *pi) return (True); } get_next_token(next, TERM_END); - if (!get_expr(pi, next, &expr_val)) + if (!get_expr(pi, next, &i)) return (False); - if (expr_val) + if (i) pi->conditional_depth++; else { if (!spool_conditional(pi, False)) @@ -726,11 +722,11 @@ parse_directive(struct prog_info *pi) next++; } } else { - if (!get_expr(pi, next, &expr_val)) { + if (!get_expr(pi, next, &i)) { print_msg(pi, MSGTYPE_APPEND,"\n"); /* Add newline */ return (False); } - print_msg(pi, MSGTYPE_APPEND,"0x%02" PRIX64,expr_val); + print_msg(pi, MSGTYPE_APPEND,"0x%02" PRIX64,i); } next = data; } @@ -802,8 +798,7 @@ term_string(struct prog_info *pi, char *string) int parse_db(struct prog_info *pi, char *next) { - int64_t expr_val; - int i; + int64_t i; int count; char *data; char prev = 0; @@ -835,12 +830,11 @@ parse_db(struct prog_info *pi, char *next) } } else { if (pi->pass == PASS_2) { - if (!get_expr(pi, next, &expr_val)) + if (!get_expr(pi, next, &i)) return (False); - i = (int)expr_val; if ((i < -128) || (i > 255)) - print_msg(pi, MSGTYPE_WARNING, "Value %d is out of range (-128 <= k <= 255). Will be masked", i); - if (pi->list_on) fprintf(pi->list_file, "%02d", i); + print_msg(pi, MSGTYPE_WARNING, "Value %" PRId64 " is out of range (-128 <= k <= 255). Will be masked", i); + if (pi->list_on) fprintf(pi->list_file, "%02" PRId64, i); } count++; write_db(pi, (char)i, &prev, count); @@ -928,8 +922,7 @@ spool_conditional(struct prog_info *pi, int only_endif) int check_conditional(struct prog_info *pi, char *pbuff, int *current_depth, int *do_next, int only_endif) { - int64_t expr_val; - int i = 0; + int64_t i = 0; char *next; char linebuff[LINEBUFFER_LENGTH]; @@ -956,9 +949,9 @@ check_conditional(struct prog_info *pi, char *pbuff, int *current_depth, int *do return (True); } get_next_token(next, TERM_END); - if (!get_expr(pi, next, &expr_val)) + if (!get_expr(pi, next, &i)) return (False); - if (expr_val) + if (i) pi->conditional_depth++; else { if (!spool_conditional(pi, False)) diff --git a/src/mnemonic.c b/src/mnemonic.c index 7fd47cf..a9c5920 100644 --- a/src/mnemonic.c +++ b/src/mnemonic.c @@ -347,8 +347,7 @@ int parse_mnemonic(struct prog_info *pi) { int mnemonic; - int64_t expr_val; - int i; + int64_t i; int opcode = 0; int opcode2 = 0; int instruction_long = False; @@ -428,9 +427,8 @@ parse_mnemonic(struct prog_info *pi) if (mnemonic >= MNEMONIC_TST) opcode |= ((i & 0x10) << 5) | (i & 0x0f); } else if (mnemonic <= MNEMONIC_RCALL) { - if (!get_expr(pi, operand1, &expr_val)) + if (!get_expr(pi, operand1, &i)) return (False); - i = (int)expr_val; i -= pi->cseg->addr + 1; if (mnemonic <= MNEMONIC_BRID) { if ((i < -64) || (i > 63)) @@ -442,9 +440,8 @@ parse_mnemonic(struct prog_info *pi) opcode = i & 0x0fff; } } else if (mnemonic <= MNEMONIC_CALL) { - if (!get_expr(pi, operand1, &expr_val)) + if (!get_expr(pi, operand1, &i)) return (False); - i = (int)expr_val; if ((i < 0) || (i > 4194303)) print_msg(pi, MSGTYPE_ERROR, "Address out of range (0 <= k <= 4194303)"); opcode = ((i & 0x3e0000) >> 13) | ((i & 0x010000) >> 16); @@ -454,9 +451,8 @@ parse_mnemonic(struct prog_info *pi) if (!get_bitnum(pi, operand1, &i)) return (False); opcode = i; - if (!get_expr(pi, operand2, &expr_val)) + if (!get_expr(pi, operand2, &i)) return (False); - i = (int)expr_val; i -= pi->cseg->addr + 1; if ((i < -64) || (i > 63)) print_msg(pi, MSGTYPE_ERROR, "Branch out of range (-64 <= k <= 63)"); @@ -498,9 +494,8 @@ parse_mnemonic(struct prog_info *pi) if (!((i == 24) || (i == 26) || (i == 28) || (i == 30))) print_msg(pi, MSGTYPE_ERROR, "%s can only use registers R24, R26, R28 or R30", instruction_list[mnemonic].mnemonic); opcode = ((i - 24) / 2) << 4; - if (!get_expr(pi, operand2, &expr_val)) + if (!get_expr(pi, operand2, &i)) return (False); - i = (int)expr_val; if ((i < 0) || (i > 63)) print_msg(pi, MSGTYPE_ERROR, "Constant out of range (0 <= k <= 63)"); opcode |= ((i & 0x30) << 2) | (i & 0x0f); @@ -509,9 +504,8 @@ parse_mnemonic(struct prog_info *pi) if (i < 16) print_msg(pi, MSGTYPE_ERROR, "%s can only use a high register (r16 - r31)", instruction_list[mnemonic].mnemonic); opcode = (i & 0x0f) << 4; - if (!get_expr(pi, operand2, &expr_val)) + if (!get_expr(pi, operand2, &i)) return (False); - i = (int)expr_val; if ((i < -128) || (i > 255)) print_msg(pi, MSGTYPE_WARNING, "Constant out of range (-128 <= k <= 255). Will be masked"); if (mnemonic == MNEMONIC_CBR) @@ -526,25 +520,22 @@ parse_mnemonic(struct prog_info *pi) } else if (mnemonic == MNEMONIC_IN) { i = get_register(pi, operand1); opcode = i << 4; - if (!get_expr(pi, operand2, &expr_val)) + if (!get_expr(pi, operand2, &i)) return (False); - i = (int)expr_val; if ((i < 0) || (i > 63)) print_msg(pi, MSGTYPE_ERROR, "I/O out of range (0 <= P <= 63)"); opcode |= ((i & 0x30) << 5) | (i & 0x0f); } else if (mnemonic == MNEMONIC_OUT) { - if (!get_expr(pi, operand1, &expr_val)) + if (!get_expr(pi, operand1, &i)) return (False); - i = (int)expr_val; if ((i < 0) || (i > 63)) print_msg(pi, MSGTYPE_ERROR, "I/O out of range (0 <= P <= 63)"); opcode = ((i & 0x30) << 5) | (i & 0x0f); i = get_register(pi, operand2); opcode |= i << 4; } else if (mnemonic <= MNEMONIC_CBI) { - if (!get_expr(pi, operand1, &expr_val)) + if (!get_expr(pi, operand1, &i)) return (False); - i = (int)expr_val; if ((i < 0) || (i > 31)) print_msg(pi, MSGTYPE_ERROR, "I/O out of range (0 <= P <= 31)"); opcode = i << 3; @@ -559,9 +550,8 @@ parse_mnemonic(struct prog_info *pi) mnemonic = MNEMONIC_LDS_AVR8L; opcode &= 0x00f0; } - if (!get_expr(pi, operand2, &expr_val)) + if (!get_expr(pi, operand2, &i)) return (False); - i = (int)expr_val; if (pi->device->flag & DF_AVR8L) { if ((i < 0x40) || (i > 0xbf)) print_msg(pi, MSGTYPE_ERROR, "SRAM out of range (0x40 <= k <= 0xbf)"); @@ -573,9 +563,8 @@ parse_mnemonic(struct prog_info *pi) instruction_long = True; } } else if (mnemonic == MNEMONIC_STS) { - if (!get_expr(pi, operand1, &expr_val)) + if (!get_expr(pi, operand1, &i)) return (False); - i = (int)expr_val; /* AVR8L has one word STS. High nibble of k in funny order */ if (pi->device->flag & DF_AVR8L) { mnemonic = MNEMONIC_STS_AVR8L; @@ -616,9 +605,8 @@ parse_mnemonic(struct prog_info *pi) print_msg(pi, MSGTYPE_ERROR, "Garbage in second operand (%s)", operand2); return (False); } - if (!get_expr(pi, &operand2[i + 1], &expr_val)) + if (!get_expr(pi, &operand2[i + 1], &i)) return (False); - i = (int)expr_val; if ((i < 0) || (i > 63)) print_msg(pi, MSGTYPE_ERROR, "Displacement out of range (0 <= q <= 63)"); opcode |= ((i & 0x20) << 8) | ((i & 0x18) << 7) | (i & 0x07); @@ -635,9 +623,8 @@ parse_mnemonic(struct prog_info *pi) print_msg(pi, MSGTYPE_ERROR, "Garbage in first operand (%s)", operand1); return (False); } - if (!get_expr(pi, &operand1[i + 1], &expr_val)) + if (!get_expr(pi, &operand1[i + 1], &i)) return (False); - i = (int)expr_val; if ((i < 0) || (i > 63)) print_msg(pi, MSGTYPE_ERROR, "Displacement out of range (0 <= q <= 63)"); opcode = ((i & 0x20) << 8) | ((i & 0x18) << 7) | (i & 0x07); @@ -748,12 +735,12 @@ get_register(struct prog_info *pi, char *data) } int -get_bitnum(struct prog_info *pi, char *data, int *ret) +get_bitnum(struct prog_info *pi, char *data, int64_t *ret) { - int64_t expr_val; - if (!get_expr(pi, data, &expr_val)) + int64_t i; + if (!get_expr(pi, data, &i)) return (False); - *ret = (int)expr_val; + *ret = i; if ((*ret < 0) || (*ret > 7)) { print_msg(pi, MSGTYPE_ERROR, "Operand out of range (0 <= s <= 7)"); return (False); From efb779901f83abc863e978e24f5f0ba348862a11 Mon Sep 17 00:00:00 2001 From: Imre Horvath Date: Sat, 27 May 2023 08:05:02 +0200 Subject: [PATCH 7/7] Fix format specifier --- src/directiv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/directiv.c b/src/directiv.c index d038fd3..9fc0fc3 100644 --- a/src/directiv.c +++ b/src/directiv.c @@ -834,7 +834,7 @@ parse_db(struct prog_info *pi, char *next) return (False); if ((i < -128) || (i > 255)) print_msg(pi, MSGTYPE_WARNING, "Value %" PRId64 " is out of range (-128 <= k <= 255). Will be masked", i); - if (pi->list_on) fprintf(pi->list_file, "%02" PRId64, i); + if (pi->list_on) fprintf(pi->list_file, "%02" PRIX64, i); } count++; write_db(pi, (char)i, &prev, count);