Posted By
Rachy on 2024-12-12 02:05:22
| Preic BASIC source pre-processing tool
My new project is ripe for public introduction:
https://github.com/almosr/preic
Preic is a tool that makes cross-developing Commodore BASIC programs a lot easier. For the details have a look at the link.
A little game is included in the project as an example, working on both C16/116/Plus/4 and C64. Maybe I publish that separately.
|
|
Posted By
Crown on 2024-12-12 02:43:36
| Re: Preic BASIC source pre-processing tool
Great, doing the other direction might be also worth considering: ie. from existing hard to read BASIC code -> to generate beautified easy to read BASIC source. Some of the steps could be automated but some might need a manually assisted process, like picking variable names or breaking up into multiple files.
|
|
Posted By
Rachy on 2024-12-12 02:49:24
| Re: Preic BASIC source pre-processing tool
Interesting, I didn't need such tool before, although it should be possible, just like a disassembler. However, exactly like disassembly, making BASIC code more understandable is not trivial.
I leave this to someone else. The petcat tool is a useful start, it could turn BASIC program into source code text. (So, it is working both directions.)
|
|
Posted By
Rachy on 2024-12-21 02:00:14
| Re: Preic BASIC source pre-processing tool
New release is out: v1.1.0
https://github.com/almosr/preic/releases/tag/v1.1.0
Added
Optional command line parameter -ld for specifying source library directory. Optimisation r for removing REM commands from source code. Processing flag $ for converting hexadecimal numbers to decimal in the source code.
Changed
When literal label is located at the beginning of the line, but not followed by equal sign then it is considered a label usage instead of definition and processing doesn't throw an error. Literal labels can be embedded into each other to create more complex structures, infinite recursion is detected.
Fixed
Handling of one character variable names, previously these names could cause processing errors. Join line optimisation when a line started with a label then it was joined with the next line if that did not start with a label.
|
|
Posted By
MMS on 2024-12-22 06:56:19
| Re: Preic BASIC source pre-processing tool
I really like this approach, and superb as we knew everything from Rachy
This code looks like a great Turbo Pascal OR Turbo C code I really liked; the hexa conversion to decimal on the fly is priceless instead of the DEC("ABCD"), I havte using the Decimal values of known and easy to remember addresses like $1800 ($$1800 NOW)
|
|
Posted By
Rachy on 2024-12-22 15:08:02
| Re: Preic BASIC source pre-processing tool
I am glad someone else apart from me finds this tool useful.
|
|
Posted By
gerliczer on 2024-12-25 06:58:40
| Re: Preic BASIC source pre-processing tool
Two ideas, but maybe they are already on the to do list. 1) GOTO after THEN is superfluous, it is enough to declare the line to which the programme should jump. 2) Optimization directives to mark critical code sequence and variables, so those can be moved forward to the beginning of the programme and variable tables.
|
|
Posted By
Rachy on 2024-12-25 15:06:28
| Re: Preic BASIC source pre-processing tool
@gerliczer Thanks for the suggestions. I am going to implement the GOTO optimisation in the next release.
Regarding your second idea: how do imagine these would work exactly? What would be the input/output of the processing? I assume the variables could be initialised at the very beginning of the program. However, moving a code block to the beginning of the program might have major consequences to the execution. I guess it would work for subroutines, but nothing else most likely.
|
|
Posted By
Csabo on 2024-12-25 15:38:32
| Re: Preic BASIC source pre-processing tool
Optimizing BASIC code in general would be great IMHO (regardless of what the code was written in). E.g. replace all most used variables with one-letter ones (depending on usage count, if there are more than 26, combine lines and remove REM statements (such programs already exists, but still), move subroutines around (exactly as gerliczer suggested). GOTO and GOSUB are particularly slow: since there's no line number -> memory location table, the interpreter has to traverse through the entire code to find the lines. Therefore the further in the code the destination lines are, the slower the execution.
|
|
Posted By
Rachy on 2024-12-25 16:54:14
| Re: Preic BASIC source pre-processing tool
@Csabo I was thinking about optimising the variable names, at the moment all names are double characters. I put that on the TODO list. How often a variable is used that is more tricky because repeating the variable multiple times in the source code might not mean that it is most used at runtime. However, 26 characters are available which means this many variables can be optimised right away which is a lot anyway.
Removing REM commands is already implemented, so your wish came true already.
Moving the subroutines around can be tricky. It would require deep understanding of the source code, so it won't be done automatically. Doing by yourself is an option, but has to be done carefully to avoid side-effects.
|
|
Posted By
gerliczer on 2024-12-26 01:36:42
| Re: Preic BASIC source pre-processing tool
As Csabo already explained, the linear search for looking up jump and call targets can really slow down running. Therefore, based on the direction of the programmer, frequently called subroutines and other parts of the main loop that are often targeted by GOTO instructions could be moved forward in the compiled code. I think, it is not unheard of that a BASIC programme is required to start with a command like RUN10000. Or to begin with the line 0 GOTO10000.
AFAIK, variables also are looked up with linear search in the memory. Therefore, variables used in the main loop and other frequently used ones should be placed towards the front of those tables. Which could be done with initialization in the proper sequence either in the first line or in line 10000 and above in the previous example.
Preic could e.g. interpret specific remarks, which will be removed anyway, as user directives or markups, like REM MAIN-LOOP-START, REM MAIN-LOOP-END, REM SUB-START-#, REM SUB-END-#, REM-VARIABLE-#-variable, and rearrange the code accordingly. It is the programmer's responsibility to use these features in a safe and sane way. Preic should only check the basic validity of these directives and generate a BASIC programme according their instructions. You know, user guided whole programme optimization. Or the programmer can ignore the feature and write his or her programme unoptimized, or do hand optimization.
|
|
Posted By
Rachy on 2024-12-26 01:43:03
| Re: Preic BASIC source pre-processing tool
@gerliczer I understand the issue, of course. I am just not sure how complicated could be finding any issues with the optimised code, so this could get messy real quick. Anyway, I give it a shot, we will see what comes out of it.
|
|
Posted By
gerliczer on 2024-12-26 03:51:41
| Re: Preic BASIC source pre-processing tool
It can't be any worse than with the original BASIC. Maybe you could add some analysing function that checks if some variables are assigned to and referenced in a manner that suggests side effects and report them after compilation.
|
|
Posted By
Rachy on 2024-12-27 02:29:23
| Re: Preic BASIC source pre-processing tool
New release: v1.2.0
https://github.com/almosr/preic/releases/tag/v1.2.0
Added
Conditional section definitions using #ifdef - #endif pre-processing directives. Pre-processing directive #define and #undef for creating and removing pre-processing flags. Command line option -d for creating pre-processing flags. Optimisation t for removing GOTO command after THEN and ELSE commands. Processing flag v for using short, one character long variable names when possible. Pre-processing directive #frequent and #endfrequent for marking certain part of the code as frequently executed. Flag for frequently used variables to be defined as early as possible.
Fixed
Handling of space character in path names for included files. Remove whitespace from beginning and end of literal label values. Selecting BASIC variable names to accept non-alphanumeric characters in the label name, but pick valid BASIC variable name instead.
After enabling all code optimisations on the example Firefighter it became noticeably faster. I didn't try to measure it exactly, it feels roughly 10-15% speed up compared to the released version.
|
|
Posted By
gerliczer on 2024-12-27 05:01:28
| Re: Preic BASIC source pre-processing tool
After enabling all code optimisations on the example Firefighter it became noticeably faster. I didn't try to measure it exactly, it feels roughly 10-15% speed up compared to the released version.
Cool. Nice job.
|
|
Posted By
SVS on 2024-12-27 07:46:21
| Re: Preic BASIC source pre-processing tool
Great work! A suggestion for a further optimization: The condition "variable<>0" can be optimized to "variable". For example "IF ab<>0 THEN 2000" works too as "IF ab THEN 2000"
|
|
Posted By
Rachy on 2024-12-27 20:07:36
| Re: Preic BASIC source pre-processing tool
Thanks, @SVS!
I am going to add that too, the more the merrier.
|
|
Posted By
gerliczer on 2024-12-28 01:42:46
| Re: Preic BASIC source pre-processing tool
There is a BASIC Tricks article in the Programming/BASIC section of the Plus/4 Encyclopedia. There may be some other useful things regarding BASIC optimization.
|
|
|
Posted By
SVS on 2024-12-28 04:06:09
| Re: Preic BASIC source pre-processing tool
@rRachy: be informed that the trick I suggested you, works only if it is the lone comparison in the expression. For example:"IF a THEN" works; "IF (a OR b=10) THEN" doesn't work.
|
|
Posted By
Rachy on 2024-12-28 17:18:48
| Re: Preic BASIC source pre-processing tool
Yep, thanks, I found that.
Also: new patch release v1.2.1, some improvements for variable name handling.
https://github.com/almosr/preic/releases/tag/v1.2.1
Added
- Check for variables that are marked as frequently used to assign short BASIC names to these variables.
Fixed
- Variable type was not considered when BASIC variable names are picked, now same name can be reused for different types.
|
|
Posted By
gerliczer on 2024-12-30 07:20:37
| Re: Preic BASIC source pre-processing tool
The Readme has a title Important*:. That asterisk looks to be a type. Or is there some additional comment on that which I'm missing.
|
|
Posted By
Rachy on 2024-12-30 13:11:19
| Re: Preic BASIC source pre-processing tool
Thanks, that's just a typo, unmatched formatting.
|
|
Posted By
Rachy on 2024-12-31 21:52:12
| Re: Preic BASIC source pre-processing tool
New Year release: v1.3.0
https://github.com/almosr/preic/releases/tag/v1.3.0
Added
- Optimisation i for simplifying IF statements when a variable is checked against non-zero value that can be reduced to the variable itself.
Changed
- Literal labels are ordered by name instead of value in label list dump.
Fixed
-Missing file and line info from pre-processing directive error. -Line joining when previous line finished with an END command. -Changed order of source processing steps. Previous version completed code optimisation before pre-processing and produced less optimal code. In this version pre-processing is completed before optimisation then line number assignment. This way line joining and other optimisations are producing better outcome. -Line joining to also join following lines to a line with line label at the beginning. -Line joining to ignore strings in double quotes when assessing whether the line could be joined with others.
|
|
Posted By
Rachy on 2025-01-11 01:04:26
| Re: Preic BASIC source pre-processing tool
New release: v1.4.0
https://github.com/almosr/preic/releases/tag/v1.4.0
Added
- Parameters code, data, remark and print to #include directive for reading binary files and converting them into source code. - Pre-processing directives #function and #call for declaring and calling functions (subroutines) with parameters.
Fixed
- Missing file and line info from source reading error.
|
|