Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro expansion problem, RARS limitation or just me #102

Open
jknGH opened this issue Feb 9, 2021 · 5 comments
Open

Macro expansion problem, RARS limitation or just me #102

jknGH opened this issue Feb 9, 2021 · 5 comments

Comments

@jknGH
Copy link

jknGH commented Feb 9, 2021

Hi there - thanks for RARS, I'm finding it useful getting going with RISC-V. I am having trouble converting the macro syntax of a source file I want to assemble, not sure if it's me or if the macro language needs a bit of work.

A minimal case with a contrived macro:

.macro mactest %out
    sw      t0,  0(%out)
    sw      t0,  4(%out)
    sw      t0,  8(%out)
    sw      t0,  12(%out)
.end_macro

Then this assembles:

	mactest a1

but neither of these do

	mactest 0(a1)
	mactest 4(a1)
        # etc.

(I get an error "forward reference or invalid parameters for macro "mactest"")
Is there a workaround I could use?

Thanks a lot, J^n

@TheThirdOne
Copy link
Owner

It makes sense to me that your first example compiles whereas your second does not. I can't think of any macro system that would allow the second examples. I don't think the expansion sw t0, 8(4(a1)) is meaningful.

More concretely in MARS (and consequently in RARS) all arguments to a macro must be single tokens so 4(a1) could not be a single argument. MARS macro help: https://courses.missouristate.edu/KenVollmar/MARS/Help/MacrosHelp.html

Is there a workaround I could use?

I don't quite know what you want your actual macro to do. If your goal is to offset lw and/or sw with multiple constants, I don't think that's possible in RARS.

A macro like the following is possible though.

.macro mactest %out %n
    sw      t0,  %n(%out)
.end_macro

@jknGH
Copy link
Author

jknGH commented Feb 10, 2021

Hi, and thanks fro the swift response.

I should say that the macro was not originally in the form I showed; I've had to transform the code a little in order to accommodate other aspects of the RARS assembler syntax (I am not sure which assembler the original code used)

I think the intention is to cause the two numbers to be added, so not sw t0, 8(4(a1)) but sw t0, 12(a1). The macros originally had addressing in a form similar to sw t0, 4+%out but RARS doesn't seem to like that.

I agree that the way things were originally written here implies a more intelligent macro processor than just simple substitution. Since this particular bit of code only occurs at the end of the macro, I think I have a workaround (untested). I will split that part off and write a set of four 'postamble' macros:

.macro postamble0 %out
    sw      t0,  0(%out)
    sw      t0,  4(%out)
    sw      t0,  8(%out)
    sw      t0,  12(%out)
.end_macro
.macro postamble4 %out
    sw      t0,  4(%out)
    sw      t0,  8(%out)
    sw      t0,  12(%out)
    sw      t0,  16(%out)
.end_macro

...

And then put the 'correct' one of those in place after the full body of the larger macro is invoked.

Thanks, J^n

@TheThirdOne
Copy link
Owner

I think the intention is to cause the two numbers to be added, so not sw t0, 8(4(a1)) but sw t0, 12(a1). The macros originally had addressing in a form similar to sw t0, 4+%out but RARS doesn't seem to like that.

RARS doesn't support any form of arithmetic as part of arguments. That is a feature I would like to introduce some time in the future though.

I agree that the way things were originally written here implies a more intelligent macro processor than just simple substitution. Since this particular bit of code only occurs at the end of the macro, I think I have a workaround (untested). I will split that part off and write a set of four 'postamble' macros:
...
And then put the 'correct' one of those in place after the full body of the larger macro is invoked.

That seems like probably the best workaround that exists currently.

@jknGH
Copy link
Author

jknGH commented Feb 11, 2021

Hi there - FYI I got things going with a variant of the 'multiple macros' approach I suggest above. Thanks!

One followup - question really, not an issue, sorry; I am using RARS in this way to try to get an idea of time taken for a critical function. I have no hardware as yet and I don't think QEMU can be used to get meaningful results for this. The 'cycle' CSR shown in RARS is giving me good answers(!). Can I trust this to be cycle-accurate?

Thanks once again for RARS, it is proving useful to me.

@TheThirdOne
Copy link
Owner

I would be cautious of reading too much into the cycles CSR. In RARS, cycles is equivalent to instret, so effectively RARS is a single cycle cpu with a fixed frequency. However if you would be using a single cycle cpu, RARS should be accurate.

I am happy RARS has been useful to you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants