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

Specifying literals size #26

Closed
ClementNerma opened this issue Feb 17, 2020 · 5 comments
Closed

Specifying literals size #26

ClementNerma opened this issue Feb 17, 2020 · 5 comments

Comments

@ClementNerma
Copy link

ClementNerma commented Feb 17, 2020

Hi there,

I'm using CustomASM for quite a bit of time now and I found it really enjoyable to use.
Still, I have one problem that is quite problematic to me: it's not possible to specify the maximum size of literals.

To understand the problem, let's consider the following ASM definition:

#cpudef
{
    #bits 8
    
    example {value} -> 0x10 @ 0x00 @ 0x00 @ value[7:0]
}

As oyu can see, the example instruction only uses 1 byte of its only operand.
Now let's consider we use this instruction like this:

main:
  example 0xAB
  example 0xABCD

This code will produce the following output:

 outp | addr | data

  0:0 |    0 |                ; main:
  0:0 |    0 | 10 00 00 ab    ; example 0xAB
  4:0 |    4 | 10 00 00 cd    ; example 0xABCD

The problem is we lose here the most significant byte of the operand in the second call. This is an error that can happen, but after compiling if we do not read the produced assembly we'll just be stuck with a bug that's hard to debug.

So I think it would be really useful to specify the maximum size of a literal, and throw a compilation error if the provided one exceeds this size. Something like this for instance:

#cpudef
{
    #bits 8
    
    example {value{1}} -> 0x10 @ 0x00 @ 0x00 @ value[7:0]
}

Or whatever syntax you may choose. In the case the literal is higher than 0xFF, an error would be thrown to indicate the literal is too big.

I don't know if this is complicated to implement though.

@moonheart08
Copy link

This can be done with asserts, but I'd like it if there was shorter syntax that had the same effect.

assert(imm >= 0)
assert(imm <= 0xFF) ; assert that this unsigned(!) byte is within range.
assert(imm <=  0x7f)
assert(imm >= !0x7f) ; assert that this signed(!) byte is within range.

@hlorenzi
Copy link
Owner

hlorenzi commented Feb 17, 2020

Oh, yeah! This was definitely in the plans. My planned syntax for this would be:

example {value: u8} -> 0x10 @ value[7:0] ; unsigned 8-bit constraint, accepts 0x00 through 0xff
example {value: s8} -> 0x10 @ value[7:0] ; signed 8-bit constraint, accepts -0x80 through 0x7f

(Perhaps even allowing you to suppress the [7:0] slice syntax)

These would throw errors similarly to the asserts moonheart08 posted above. But right now, you must use explicit asserts to get this behavior. I'll try to work on this feature as soon as possible!

@ClementNerma
Copy link
Author

ClementNerma commented Feb 17, 2020

This can be done with asserts, but I'd like it if there was shorter syntax that had the same effect.

assert(imm >= 0)
assert(imm <= 0xFF) ; assert that this unsigned(!) byte is within range.
assert(imm <=  0x7f)
assert(imm >= !0x7f) ; assert that this signed(!) byte is within range.

I didn't think of this one, that's pretty clever :)

Oh, yeah! This was definitely in the plans. My planned syntax for this would be:

example {value: u8} -> 0x10 @ value[7:0] ; unsigned 8-bit constraint, accepts 0x00 through 0xff
example {value: s8} -> 0x10 @ value[7:0] ; signed 8-bit constraint, accepts -0x80 through 0x7f

(Perhaps even allowing you to suppress the [7:0] slice syntax)

These would throw errors similarly to the asserts moonheart08 posted above. But right now, you must use explicit asserts to get this behavior. I'll try to work on this feature as soon as possible!

Oh that's great! This syntax is far better that I proposed.
If you need help on specific implementation points, I'd be happy to help if I can :)

@hlorenzi
Copy link
Owner

Done! You can now use this syntax, and even elide slices on argument usage. There is a third parameter type too:

example {value: u8} -> 0x10 @ value ; unsigned 8-bit constraint, accepts 0x00 through 0xff
example {value: s8} -> 0x10 @ value ; signed 8-bit constraint, accepts -0x80 through 0x7f
example {value: i8} -> 0x10 @ value ; signed/unsigned 8-bit constraint, accepts -0x80 through 0xff

And, of course, you can use whichever number of bits you like, such as {value: s32} or {value: u3}.

@ClementNerma
Copy link
Author

Oh that was quick :D
Thanks a lot for this improvement!

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

3 participants