Thursday, 10 October 2019

Magic Numbers

Generally they're a bad thing. Magic Numbers in code are when someone has put + 3187 in an expression or something, and you don't know why. There seems to be a modern trend of "not commenting anything", on the apparent grounds that the code is self evident. Invariably it isn't.

Anyways, I wrote this script in python which tries various mappings as a way of finding a simple method of mapping the BASIC tokens onto P-Codes.

After some tinkering, I found the following :-

  • Firstly reduce any tokens of values 187 or more by 39.
  • Secondly deduct 33 for any character tokens (e.g. 0-127)
  • Thirdly deduct 123 for any tokens (e.g. 128+)

This rather neatly maps all the tokens on characters onto 0-59 (room for expansion, of course) which leaves 60-255 free.

The reason I want this is there 4 other P-Codes which are internal - these are branch, branch if tos = 0, 16 bit push, 6 bit push (-1 to 62, 1 byte) and 15 bit push (0-32767, 2 bytes). The last pair (6 and 15 bit pushes) take up 192 of the available opcodes. However, of the other 60, about 1/3 of them are not used.

To "syntax check" I have also generated a bitmap for values from 32 (first character) to 204 (last token). This is a small array with 1 bit per value, set if this is syntactically legal, so if LEFT$ say is used, then this bit will be clear, so an error can be signalled.

So compilation is fairly simple really. Just those three steps above.  Additional code is required for data, back-origin, identifiers, constants, but these aren't really complicated.

So plan the first is to write a compiler in Python. Then a 6502 run time, with a skeleton to run the compiled code. Then see if the compiler will compile itself - if it starts at the same memory location, it should produce exactly the same binary code.

No comments:

Post a Comment