From 13f521666b01d1f785060160600d4e74e4576bf8 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Mon, 20 Jul 2020 14:54:14 -0400 Subject: [PATCH] Assembler now accepts "c[5]" as equivalent to "c5". This only accepts a constant integers, you can't currently do something like "c[2+3]" even if the constants could be completely folded during assembly. I don't know if Microsoft's tools allow that, will have to revisit later if so. --- mojoshader_assembler.c | 62 ++++++++++++++++++++++++++++-------------- 1 file changed, 41 insertions(+), 21 deletions(-) diff --git a/mojoshader_assembler.c b/mojoshader_assembler.c index c41f7fb..bb2a94d 100644 --- a/mojoshader_assembler.c +++ b/mojoshader_assembler.c @@ -696,31 +696,51 @@ static int parse_source_token_maybe_relative(Context *ctx, const int relok) pushback(ctx); // not relative addressing? else { - if (!relok) - fail(ctx, "Relative addressing not permitted here."); - else - retval++; - - parse_source_token_maybe_relative(ctx, 0); - relative = 1; + // quick hack here to make "c[5]" convert to "c5". This will also + // work with "c0[5]" but that's possibly illegal...? + int skip_relative_parsing = 0; + if ((regtype == REG_TYPE_CONST) && (regnum == 0)) + { + uint32 ui32 = 0; + if (nexttoken(ctx) != TOKEN_INT_LITERAL) + pushback(ctx); + else if (!ui32fromtoken(ctx, &ui32)) + pushback(ctx); + else + { + regnum = ui32; + skip_relative_parsing = 1; + } // else + } // if - if (nexttoken(ctx) != ((Token) '+')) - pushback(ctx); - else + if (!skip_relative_parsing) { - // !!! FIXME: maybe c3[a0.x + 5] is legal and becomes c[a0.x + 8] ? - if (regnum != 0) - fail(ctx, "Relative addressing with explicit register number."); + if (!relok) + fail(ctx, "Relative addressing not permitted here."); + else + retval++; - uint32 ui32 = 0; - if ( (nexttoken(ctx) != TOKEN_INT_LITERAL) || - (!ui32fromtoken(ctx, &ui32)) || - (ctx->tokenlen != 0) ) + parse_source_token_maybe_relative(ctx, 0); + relative = 1; + + if (nexttoken(ctx) != ((Token) '+')) + pushback(ctx); + else { - fail(ctx, "Invalid relative addressing offset"); - } // if - regnum += (int) ui32; - } // else + // !!! FIXME: maybe c3[a0.x + 5] is legal and becomes c[a0.x + 8] ? + if (regnum != 0) + fail(ctx, "Relative addressing with explicit register number."); + + uint32 ui32 = 0; + if ( (nexttoken(ctx) != TOKEN_INT_LITERAL) || + (!ui32fromtoken(ctx, &ui32)) || + (ctx->tokenlen != 0) ) + { + fail(ctx, "Invalid relative addressing offset"); + } // if + regnum += (int) ui32; + } // else + } // if if (nexttoken(ctx) != ((Token) ']')) fail(ctx, "Expected ']'");