# HG changeset patch # User William Astle # Date 1639958502 25200 # Node ID 724bcc4508bc8ca39566e67cd3a9c4071d6c4d5b # Parent b530ff19f7c08e15a6cf3694ce13e99223108e05 Add SETSTR/INCLUDESTR for some basic code building It seemed useful to have the ability to build up a variable containing arbitrary text and then to be able to include that in the assembly process like an include file. So add, undocumented for now, the following: SETTSTR varname="string" INCLUDESTSR "string" "string" must be enclosed in double quotes and may contain most of the usual escape sequences (\t, \r, etc.) as well as %(varname) to interpolate a variable value. To use it to create assembleable source code, you need to make sure it creates lines (ended by either \r or \n) with appropriate whitespace in appropriate places. diff -r b530ff19f7c0 -r 724bcc4508bc Makefile --- a/Makefile Tue Apr 13 15:41:23 2021 -0600 +++ b/Makefile Sun Dec 19 17:01:42 2021 -0700 @@ -66,7 +66,7 @@ lwlib_srcs := lw_alloc.c lw_realloc.c lw_free.c lw_error.c lw_expr.c \ lw_stack.c lw_string.c lw_stringlist.c lw_cmdline.c lw_strbuf.c \ - lw_strpool.c + lw_strpool.c lw_dict.c lwlib_srcs := $(addprefix lwlib/,$(lwlib_srcs)) lwlink_srcs := main.c lwlink.c readfiles.c expr.c script.c link.c output.c map.c @@ -78,7 +78,7 @@ insn_inh.c insn_logicmem.c insn_rel.c insn_rlist.c insn_rtor.c insn_tfm.c \ instab.c list.c lwasm.c macro.c main.c os9.c output.c pass1.c pass2.c \ pass3.c pass4.c pass5.c pass6.c pass7.c pragma.c pseudo.c section.c \ - struct.c symbol.c symdump.c unicorns.c + strings.c struct.c symbol.c symdump.c unicorns.c lwasm_srcs := $(addprefix lwasm/,$(lwasm_srcs)) lwasm_objs := $(lwasm_srcs:.c=.o) diff -r b530ff19f7c0 -r 724bcc4508bc lwasm/instab.c --- a/lwasm/instab.c Tue Apr 13 15:41:23 2021 -0600 +++ b/lwasm/instab.c Sun Dec 19 17:01:42 2021 -0700 @@ -320,6 +320,10 @@ #define pseudo_resolve_includebin NULL EMITFUNC(pseudo_emit_includebin); +PARSEFUNC(pseudo_parse_includestr); +#define pseudo_resolve_includestr NULL +#define pseudo_emit_includestr NULL + PARSEFUNC(pseudo_parse_include); #define pseudo_resolve_include NULL #define pseudo_emit_include NULL @@ -340,6 +344,10 @@ #define pseudo_resolve_endstruct NULL #define pseudo_emit_endstruct NULL +PARSEFUNC(pseudo_parse_setstr); +#define pseudo_resolve_setstr NULL +#define pseudo_emit_setstr NULL + // convenience ops PARSEFUNC(insn_parse_conv); #define insn_resolve_conv NULL @@ -669,6 +677,7 @@ { "end", { -1, -1, -1, -1 }, pseudo_parse_end, pseudo_resolve_end, pseudo_emit_end, lwasm_insn_normal}, { "includebin", { -1, -1, -1, -1}, pseudo_parse_includebin,pseudo_resolve_includebin, pseudo_emit_includebin, lwasm_insn_normal}, + { "includestr", { -1, -1, -1, -1}, pseudo_parse_includestr,pseudo_resolve_includestr, pseudo_emit_includestr, lwasm_insn_normal}, { "include", { -1, -1, -1, -1 }, pseudo_parse_include, pseudo_resolve_include, pseudo_emit_include, lwasm_insn_normal}, { "incl", { -1, -1, -1, -1 }, pseudo_parse_include, pseudo_resolve_include, pseudo_emit_include, lwasm_insn_normal}, { "use", { -1, -1, -1, -1 }, pseudo_parse_include, pseudo_resolve_include, pseudo_emit_include, lwasm_insn_normal}, @@ -707,6 +716,7 @@ { "endm", { -1, -1, -1, -1}, pseudo_parse_endm, pseudo_resolve_endm, pseudo_emit_endm, lwasm_insn_cond | lwasm_insn_setsym | lwasm_insn_endm}, { "setdp", { -1, -1, -1, -1}, pseudo_parse_setdp, pseudo_resolve_setdp, pseudo_emit_setdp, lwasm_insn_normal}, + { "setstr", { -1, -1, -1, -1}, pseudo_parse_setstr, pseudo_resolve_setstr, pseudo_emit_setstr, lwasm_insn_normal}, { "set", { -1, -1, -1, -1}, pseudo_parse_set, pseudo_resolve_set, pseudo_emit_set, lwasm_insn_setsym}, diff -r b530ff19f7c0 -r 724bcc4508bc lwasm/lwasm.h --- a/lwasm/lwasm.h Tue Apr 13 15:41:23 2021 -0600 +++ b/lwasm/lwasm.h Sun Dec 19 17:01:42 2021 -0700 @@ -29,6 +29,7 @@ #include #include #include +#include #include @@ -426,6 +427,8 @@ lw_stringlist_t include_list; // include paths lw_stack_t file_dir; // stack of the "current file" dir lw_stack_t includelist; + lw_dict_t stringvars; // dictionary of string variables (SETSTR/INCLUDESTR) + structtab_t *structs; // defined structures structtab_t *cstruct; // current structure @@ -496,4 +499,7 @@ /* skip to the start of the next token if the current parsing mode allows it */ void lwasm_skip_to_next_token(line_t *cl, char **p); +/* parse a generalized string enclosed in double quotes */ +char *lwasm_parse_general_string(line_t *cl, char **p); + #endif /* ___lwasm_h_seen___ */ diff -r b530ff19f7c0 -r 724bcc4508bc lwasm/pseudo.c --- a/lwasm/pseudo.c Tue Apr 13 15:41:23 2021 -0600 +++ b/lwasm/pseudo.c Sun Dec 19 17:01:42 2021 -0700 @@ -1583,6 +1583,41 @@ lw_free(fn); } +PARSEFUNC(pseudo_parse_includestr) +{ + char *str; + char buf[110]; + + l -> len = 0; + + if (!**p) + { + // no operand - include nothing + return; + } + + str = lwasm_parse_general_string(l, p); + if (!str) + { + // string parsing failed + return; + } + if (*str == '\0') + { + // empty string; don't do anything + lw_free(str); + return; + } + + /* add a book-keeping entry for line numbers */ + snprintf(buf, 100, "\001\001SETLINENO %d\n", l -> lineno + 1); + input_openstring(as, "INTERNAL", buf); + + /* add the constructed string to the input */ + input_openstring(as, "INCLUDESTR", str); + lw_free(str); +} + PARSEFUNC(pseudo_parse_align) { lw_expr_t e; diff -r b530ff19f7c0 -r 724bcc4508bc lwasm/strings.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lwasm/strings.c Sun Dec 19 17:01:42 2021 -0700 @@ -0,0 +1,235 @@ +/* +strings.c +Copyright © 2021 William Astle + +This file is part of LWASM. + +LWASM is free software: you can redistribute it and/or modify it under the +terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +more details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . + +Contains stuff associated with generalized string parsing including +interpolation. + +A general string is enclosed in double quotes. Within the string, the +following is supported: + +%(VAR): a string variable defined with SETSTR +%[SYM]: the value of SYM; must be constant on pass 1 or will resolve to "" +\": a literal " +\%: a literal % +\n: a newline +\r: a carriage return +\t: a tab +\f: a form feed +\e: ESC (0x1b) +\\: a backslash +\xXX: an 8 bit value where XX are hex digits + +*/ + +#include +#include + +#include +#include +#include +#include + +#include "lwasm.h" +#include "instab.h" + +void lwasm_stringvar_unset(asmstate_t *as, char *strname) +{ + if (!(as -> stringvars)) + return; + lw_dict_unset(as -> stringvars, strname); +} + +void lwasm_stringvar_set(asmstate_t *as, char *strname, char *strval) +{ + if (!(as -> stringvars)) + as -> stringvars = lw_dict_create(); + lw_dict_set(as -> stringvars, strname, strval); +} + +char *lwasm_stringvar_get(asmstate_t *as, char *strname) +{ + if (!(as -> stringvars)) + return ""; + return lw_dict_get(as -> stringvars, strname); +} + +PARSEFUNC(pseudo_parse_setstr) +{ + char *t1; + char *strname; + char *strval; + + l -> len = 0; + if (!**p) + { + lwasm_register_error(as, l, E_OPERAND_BAD); + return; + } + + for (t1 = *p; *t1 && *t1 != '='; t1++) + /* do nothing */; + strname = lw_alloc(t1 - *p + 1); + strncpy(strname, *p, t1 - *p); + strname[t1 - *p] = '\0'; + *p = t1; + if (**p == '\0') + { + lwasm_stringvar_unset(l -> as, strname); + lw_free(strname); + return; + } + (*p)++; + strval = lwasm_parse_general_string(l, p); + if (!strval) + { + lwasm_stringvar_unset(l -> as, strname); + lw_free(strname); + return; + } + lwasm_stringvar_set(l -> as, strname, strval); + lw_free(strval); +} + +char *lwasm_parse_general_string(line_t *l, char **p) +{ + struct lw_strbuf *sb; + + if (!**p || isspace(**p)) + return lw_strdup(""); + if (**p != '"') + { + lwasm_register_error(l -> as, l, E_OPERAND_BAD); + return NULL; + } + + (*p)++; + sb = lw_strbuf_new(); + while (**p && **p != '"') + { + switch (**p) + { + case '\\': + if ((*p)[1]) + { + (*p)++; + switch (**p) + { + case 'n': + lw_strbuf_add(sb, 10); + break; + + case 'r': + lw_strbuf_add(sb, 13); + break; + + case 't': + lw_strbuf_add(sb, 9); + break; + + case 'f': + lw_strbuf_add(sb, 12); + break; + + case 'e': + lw_strbuf_add(sb, 27); + break; + + case 'x': + if ((*p)[1] && (*p)[2]) + { + int d1 = (*p)[1]; + int d2 = (*p)[2]; + if (d1 < '0' || (d1 > '9' && d1 < 'A') || (d1 > 'F' && d1 < 'a') || d1 > 'f' || + d2 < '0' || (d2 > '9' && d2 < 'A') || (d2 > 'F' && d2 < 'a') || d2 > 'f') + { + lw_strbuf_add(sb, 'x'); + } + else + { + (*p) += 2; + d1 -= '0'; + d2 -= '0'; + if (d1 > 9) + d1 -= 7; + if (d1 > 15) + d1 -= 32; + if (d2 > 9) + d2 -= 7; + if (d2 > 15) + d2 -= 32; + lw_strbuf_add(sb, (d1 << 4) | d2); + } + } + else + { + lw_strbuf_add(sb, 'x'); + } + break; + + default: + lw_strbuf_add(sb, **p); + break; + } + } + break; + + case '%': + (*p)++; + if (**p == '(') + { + char *t1; + // string var + for (t1 = *p + 1; *t1 && *t1 != ')' && *t1 != '"'; t1++) + /* do nothing */ ; + if (*t1 != ')') + { + lw_strbuf_add(sb, '%'); + (*p)--; + } + else + { + char *strname; + strname = lw_alloc(t1 - *p); + strncpy(strname, *p + 1, t1 - *p); + strname[t1 - *p - 1] = '\0'; + *p = t1; + t1 = lwasm_stringvar_get(l -> as, strname); + lw_free(strname); + for (strname = t1; *strname; strname++) + lw_strbuf_add(sb, *strname); + } + } + else + { + // unknown % sequence; back up and output the % + (*p)--; + lw_strbuf_add(sb, '%'); + } + break; + + default: + lw_strbuf_add(sb, **p); + } + (*p)++; + } + if (**p == '"') + (*p)++; + return lw_strbuf_end(sb); +} + diff -r b530ff19f7c0 -r 724bcc4508bc lwlib/lw_dict.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lwlib/lw_dict.c Sun Dec 19 17:01:42 2021 -0700 @@ -0,0 +1,114 @@ +/* +lw_dict.c + +Copyright © 2021 William Astle + +This file is part of LWTOOLS. + +LWTOOLS is free software: you can redistribute it and/or modify it under the +terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +more details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +*/ + +#include +#include + +#define ___lw_dict_c_seen___ +#include "lw_dict.h" +#include "lw_string.h" +#include "lw_alloc.h" + +lw_dict_t lw_dict_create(void) +{ + lw_dict_t s; + + + s = lw_alloc(sizeof(struct lw_dict_priv)); + s -> head = NULL; + return s; +} + +void lw_dict_unset(lw_dict_t S, char *key) +{ + struct lw_dict_ent *e1, *e2; + + for (e2 = NULL, e1 = S -> head; e1; ) + { + if (strcmp(key, e1 -> key) == 0) + break; + e2 = e1; + e1 = e1 -> next; + } + if (!e1) + return; + if (!e2) + S -> head = e1 -> next; + else + e2 -> next = e1 -> next; + lw_free(e1 -> key); + lw_free(e1 -> value); + lw_free(e1); +} + +void lw_dict_destroy(lw_dict_t S) +{ + if (S) + { + while (S -> head) + lw_dict_unset(S, S -> head -> key); + lw_free(S); + } +} + +char *lw_dict_get(lw_dict_t S, char *key) +{ + struct lw_dict_ent *e1; + + for (e1 = S -> head; e1; e1 = e1 -> next) + if (strcmp(key, e1 -> key) == 0) + break; + if (e1) + return e1 -> value; + return ""; +} + +void lw_dict_set(lw_dict_t S, char *key, char *value) +{ + struct lw_dict_ent *e1; + + for (e1 = S -> head; e1; e1 = e1 -> next) + { + if (strcmp(key, e1 -> key) == 0) + { + lw_free(e1 -> value); + e1 -> value = lw_strdup(value); + return; + } + } + + e1 = lw_alloc(sizeof(struct lw_dict_ent)); + e1 -> next = S -> head; + S -> head = e1; + e1 -> key = lw_strdup(key); + e1 -> value = lw_strdup(value); +} + +lw_dict_t lw_dict_copy(lw_dict_t S) +{ + lw_dict_t R; + struct lw_dict_ent *e1; + + R = lw_dict_create(); + for (e1 = S -> head; e1; e1 = e1 -> next) + lw_dict_set(R, e1 -> key, e1 -> value); + return R; +} diff -r b530ff19f7c0 -r 724bcc4508bc lwlib/lw_dict.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lwlib/lw_dict.h Sun Dec 19 17:01:42 2021 -0700 @@ -0,0 +1,53 @@ +/* +lw_dict.h + +Copyright © 2010 William Astle + +This file is part of LWTOOLS. + +LWTOOLS is free software: you can redistribute it and/or modify it under the +terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +more details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . +*/ + +#ifndef ___lw_dict_h_seen___ +#define ___lw_dict_h_seen___ + + +#ifdef ___lw_dict_c_seen___ + +struct lw_dict_ent +{ + char *key; + char *value; + struct lw_dict_ent *next; +}; + +struct lw_dict_priv +{ + struct lw_dict_ent *head; +}; +typedef struct lw_dict_priv * lw_dict_t; + +#else /* def ___lw_dict_c_seen___ */ + +typedef void * lw_dict_t; +lw_dict_t lw_dict_create(void); +void lw_dict_destroy(lw_dict_t S); +void lw_dict_set(lw_dict_t S, char *key, char *val); +void lw_dict_unset(lw_dict_t S, char *key); +char *lw_dict_get(lw_dict_t S, char *key); +lw_dict_t lw_dict_copy(lw_dict_t S); + +#endif /* def ___lw_dict_c_seen___ */ + +#endif /* ___lw_dict_h_seen___ */