harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 72c8d715590c95f06060b2c237c1c18052b60389
parent f689760832707d39f73168a7652f783dfbd7158f
Author: Drew DeVault <sir@cmpwn.com>
Date:   Wed,  4 Aug 2021 10:35:46 +0200

gen: implement string constants

Signed-off-by: Drew DeVault <sir@cmpwn.com>

Diffstat:
Msrc/gen.c | 32+++++++++++++++++++++++++++++++-
1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/src/gen.c b/src/gen.c @@ -361,7 +361,37 @@ static void gen_const_string_at(struct gen_context *ctx, const struct expression *expr, struct gen_value out) { - assert(0); // TODO + const struct expression_constant *constexpr = &expr->constant; + const char *val = constexpr->string.value; + size_t len = constexpr->string.len; + + struct qbe_value global = mkqtmp(ctx, ctx->arch.ptr, "strdata.%d"); + global.kind = QV_GLOBAL; + + struct qbe_def *def = xcalloc(1, sizeof(struct qbe_def)); + def->name = global.name; + def->kind = Q_DATA; + def->data.items.type = QD_STRING; + def->data.items.str = xcalloc(1, len); + memcpy(def->data.items.str, val, len); + def->data.items.sz = len; + + if (len != 0) { + qbe_append_def(ctx->out, def); + } else { + free(def); + global = constl(0); + } + + enum qbe_instr store = store_for_type(ctx, &builtin_type_size); + struct qbe_value strp = mkcopy(ctx, &out, ".%d"); + struct qbe_value qlen = constl(len); + struct qbe_value offs = constl(builtin_type_size.size); + pushi(ctx->current, NULL, store, &qlen, &strp, NULL); + pushi(ctx->current, &strp, Q_ADD, &strp, &offs, NULL); + pushi(ctx->current, NULL, store, &qlen, &strp, NULL); + pushi(ctx->current, &strp, Q_ADD, &strp, &offs, NULL); + pushi(ctx->current, NULL, store, &global, &strp, NULL); } static void