92 points · by rbanffy · 1y ago · 76 comments
Whether it was the best language for 8-bit programming, it certainly was a great fit for the 6502, as the language targeted the peculiarities of that chip. Accessing hardware-specific features of the 8-bit Atari's was a snap, which was necessary in order to do anything more interesting than sieves or print loops.
Action! probably could've been ported to the Apple line, but 8-bits were winding down by the time it was released. Porting to 16-bit machines like the IBM PC or Mac (or even the Atari ST) would have been a tougher sell, since Pascal and C were better established by that point, and worked well on those machines.
Two bad things about Action!: Charging a license fee to distribute the runtime, and that dumb bang in the name.
As in after X recursion, you'd get in trouble as the memory kept allocating new stack frames for every recurse...
Again on System/360, I benchmarked BCPL procedure call costs against subroutine call costs in Fortran G (the non-optimizing compiler). BCPL was about 3 times faster.
That said, as soon as you added multi-tasking (what we'd now call threads), it all went to hell. It's not an accident that one IBM PL/I manual of the 1960s said “Do not use procedures, they are expensive.”
As mentioned by others, it was the tiny stack in the 6502 that killed this approach. I appreciate all those who pine for the 6502, but it made implementing modern (even for the 1970s) languages almost impossible.
But you still need some recursion and C and Pascal can do that.
But Apple Pascal was released for the Apple II in 1979. Based on UCSD Pascal, the Apple Pascal system was basically an OS that simply was an IDE; and it worked perfectly well on 8-bit hardware. I had quite a lot of fun with it back in the day.
The article is broadly correct. The 6502 had what amounts to a mixed-performance address space. All indirect addressing had to be done via pairs of registers in the zero page at addresses 0-255. Essentially all the "pointers" in your application wanted naturally to live as one of these 128 "pointer registers". But that's not the way natural code generation wants to work, where the pointers get stored in the data memory along with everything else.
So compiled languages need to have some kind of trampoline for every pointer to copy it into the memory where it needed to live, which was a really tall order for the optimizers of 1983.
Or they could just cheat and compile to a virtualized instruction set and feed that to an interpreter. Apple chose this, twice: once with Woz's sweet16 in which Integer BASIC was written, and again with the port of the P-Code interpreter for Pascal.
The 6502 might not have been able to handle Pascal well, but Borland Turbo Pascal for CP/M (z80, 8080, etc) worked very, very well. It was also released in 1983 or so, but dunno whether it or Factor was 'first'.
Because, yeah, the 6502 is a difficult target for high level languages.
Even worse, the bytecode was standard UCSD Pascal, designed for mainframes, and not really how you'd design bytecode if you knew you were going to implement it on a 6502.
We can actually compile Pascal / C style languages to the 6502 pretty well these days. The way to do it is treat it the same as a RISC machine with a lot of registers (let's say 32 2-byte pairs in zero page, leaving 75% of ZP for globals and a handful of important runtime functions that benefit from self-modifying code). Split the 32 pseudo-registers up into A, S, T registers the same as you do for any RISC (or x86_64 for that matter).
It's problem to handle recursion. Just the same as any function call, put anything you still want after the function call into S registers, and in the called function if it needs to use S registers then it needs to save them on a stack (NOT the 256 byte hardware stack) first and restore them after. For more than one or two saved registers it's better to use a runtime function for this -- 12 cycles overhead, but lots of bytes of code saved. RISC-V's -msave-restore provides a good model for this.
You can make a nice set of 2-address arithmetic routines between 16 or 32 bit Zero-Page registers by using the X and Y registers to pass which locations to work on.
add16:
clc
lda 0,x
adc 0,y
sta 0,x
lda 1,x
adc 1,y
sta 1,x
ret
...
ldx #dst
ldy #src
jsr add16
This reduces the call site from 13 bytes to 7 bytes (and you can often reuse an X or Y), while increasing the execution time for an inline add from 20 cycles to 42 (1 cycle extra per instruction for the indexed addressing = 6, 4 cycles for loading X and Y, 12 cycles for the jsr/ret)For 32 bit arithmetic the time overhead is much reduced. For floating point it would be negligable!
There is no convenient way to make 3-address routines, but `mov16` is only 5 instructions and 28 cycles. Or just 4 instructions (8 bytes) and 12 cycles if inlined, which might be a better tradeoff i.e.
lda src1
sta dst
lda src1+1
sta dst+1
ldx #dst
ldx #src2
jsr add16
vs ldx #dst
ldy #src1
jsr mov16
ldy #src2
jsr add16
Stack-based 6502 code is both bigger and slower than (pseudo) register-based code -- a zero-argument `jsr add16` itself is smaller, but it's significantly slower, and loading and storing values between stack and somewhere else will be much more code and much slower.Stack-based bytecode has a whole lot more overhead again to fetch the next instruction and dispatch to the correct code for it. Register-based bytecode would be a much better idea, as it uses several times fewer bytecode instructions, and is more compact to boot (see Dalvik vs JVM ... sad that webasm didn't pay attention).
I agree about the general approach to taking advantage of the zero page, but I don't have the 6502 competency to comment on your proposed instruction sequences.
That's not my experience, and I don't see how it can be true, even theoretically, for typical programs.
I don't at all mind more instructions, if that works out smaller or at least not bigger. E.g. RISC-V using `c.slli; c.srli` or `c.slli; c.srai` (all 2-byte opcodes) to extract unsigned or signed bitfields instead of having a 4-byte "extract field" instruction.
It is true that a stack-based ISA can have a size advantage in evaluating very complex expressions, such as those typical in scientific / engineering computing, when you get a lot of arithmetic operators in a row with no arguments needed.
But in general purpose programs the vast majority of lines of C code have only very simple expressions. `x=x+y`. `x=x+10`. `if x<y`.
Register ISAs provide short addressing for the most frequently-used 8 or 16 or 32 variables in a function (the registers). Experience shows 8 isn't really enough, 32 is usually a little more than you need -- I think some study showed 24 was enough but you don't get a code size advantage from that, so ...
Stack based ISAs do similarly. JVM can load or store any of four local variables within a 1-byte instruction. For more than than you can refer to 256 variables with a 2-byte instruction. Four really isn't enough, but 256 is a waste of code size. WASM doesn't have any option to include a variable number in the main opcode byte -- `local.get 1` is two bytes.
A very typical statement in programs is `x += y`.
JVM 4 bytes:
iload 0 // 1A
iload 1 // 1B
iadd // 60
istore 0 // 3B
If x and y aren't in the first 4 locals then it's 7 bytesWASM 7 bytes:
(local.get 0) // 20 00
(local.get 1) // 20 01
(i32.add) // 6A
(local.set 0) // 21 00
ARMv7 / ARMv6-M / ARMv4T 2 bytes: add r0, r0, r1 // 08 44
RISC-V with C extension 2 bytes: add a0, a0, a1 // 2E 95
The Arm example works with 16 local variables (only the most common operations MOV, ADD, and CMP can use all 16 registers, other operations can only use 8), the RISC-V example works with all 32 registers (again only C.MV, C.ADD, C.ADDI, C.SLLI work with all 32 registers, other 2-byte opcodes use only 8).8086, M68k, PDP-11, Super-H, MSP430 can all also do this very common operation with a 2-byte instruction, with anything from 8 to 16 local variables.
Exactly the same analysis applies to `x += 10`.
Most programs consist almost entirely of statements like these, not things like `det = a(ei − fh) − b(di − fg) + c(dh − e*g)`
There are two main problems with stack code:
- you need four opcodes: `load`, `load`, `add`, `store`. Each opcode needs at minimum 3 or 4 bits, so no matter what you do you're going to have 12-16 bits to specify four actions. In contrast, the register machine can just use a single `add` opcode, again using 3 or 4 bits.
- all the common stack ISAs use a minimum of 8 bits for an instruction (even the extremely common `iadd` or `i32.add` and use a multiple of 8 bits for every instruction. Four instructions is going to be minimum 4 bytes.
- the register machine needs only one 3 or 4 bit opcode for `add` AND can pack the other operands arbitrarily into a 16-bit instruction, using whatever field size and position makes sense.
- the register machine needs to mention the dst/rs1 variable only once.
Stack machine code could be improved in size a little if you bit-packed the instructions, but you'll still have the problems of needing more opcode fields and needing to repeat variable names.
Perhaps ironically, some accumulator machines do better. For example the much-hated PIC microcontroller actually does this well:
MOVF 0x21, W ; Load y (from address 0x21) into W register
ADDWF 0x20, F ; Add W (containing y) to x (at address 0x20), store result in x
Instructions such as ADDWF have a bit to specify whether to put the result of the add into W (the accumulator) or the source register.On the smaller chips each of those instructions is 12 bits (with 32 registers, many with special purposes), though those are very limited and most applications use mid-range chips with 14 bit instructions (allowing 128 directly accessible registers).
So this is 24 or 28 bits of code, still not as compact as the 16 bits for that extensive list of register machines: RISC-V, Arm Thumb, 8086, M68k, PDP-11, Super-H, MSP430.
This comes straight from Algol if I'm not mistaken. It seems weird to us now (tho bourne shell / bash kinda has this in spots) but it was in the air in the 60s/70s.
When I've looked at it in the past I definitely got the sense that Action was very much inspired by Algol-68, but with some accomodations for the niche of 6502.
6502 is a terrible target for C (and even Pascal) compilation, I have often wondered if it made sense for someone to try and revive Action for the 21st century as a general purpose 6502 high level PL.
It’s like HTML hah.
https://ataripodcast.libsyn.com/antic-interview-111-clinton-...
https://github.com/greggman/rockfall/blob/main/ROCK4.ACT
I've since ported that code multiple times. Once to Gameboy (unshipped, it was just for learning when I was hired to make a gameboy game). Once to C. Once to Java for a feature phone software interview. A couple of years ago to JavaScript
Definitely leads to a feeling of velocity. I don't like the language that much but I do get the fun from that feeling!
However I love the ease of cross compiling and distributing cross compiled binaries that much that Go is still the best tool for some jobs
https://www.atariarchives.org/APX/showdocs.php?cat=20166
There were some other weird 8-bit programming languages: PLM and MPL. These are PL/I clones for 8080 and 6800. I used MPL long ago and wrote an article about it:
https://github.com/jhallen/exorsim/tree/master/mpl
PLM was much more popular and better:
The Action! language may not have been as advanced as C or Pascal, but because it was designed with the 6502 CPU in mind, compiling the language was astonishingly fast.
The original Atari Pascal system from APX needed multiple disk drives and could take several minutes to compile a small program. The only C package available in 1983 (Deep Blue C) was at least as limited as Action!, but also not an integrated package and compiled slowly. Draper Pascal only compiled to pseudo-code.
Action! compiled your program to machine code in memory and in seconds. Typing C (to compile) and then R (to run) was hardly slower than just typing RUN in BASIC.
So less about the language itself (unless it had some particular properties that facilitated compiling it quickly) and more about the tooling. BEGIN
INTEGER A:=0, B, C:=1;
PROCEDURE N(X,Y,Z);
INTEGER X,Y,Z;
X:=X*(Y+Z);
FOR B:=1 UNTIL 20 DO
N(A,B,C);
END.
The "Action!" code samples don't look like a dialect of this, but of a different branch of the Algol tree: PROC hello()
; This is a comment.
DO
PrintE("Goto 10")
OD
RETURN
PROC timestwo()
CARD i=[0],j
DO ;start of DO - OD loop
i==+1 ;add 1 to
j=i*2 ;set 'j' equal to i*2
PrintC(i) ;**** See the following
Print(" times 2 equals ") ;PROGRAMMING NOTE for
PrintCE(j) ;an explanation
OD ;end of DO - OD loop
RETURN
PROC hithere()
BYTE ctr ;counter used in FOR loop
FOR ctr=1 TO S ;this FOR loop has no 'STEP', so
DO ;an increment of 1 is assumed.
PrintE("Hi there")
OD
RETURN
The divergences I see that gave me this idea:1. SPL used PROCEDURE to define procedures, and "Action!" uses PROC.
2. SPL used semicolon to separate statements, and "Action!" evidently used newlines or some kind of Lua-like rule, instead using semicolon for comments.
3. SPL used := for assignment statements (and FOR loops), and "Action!" used =.
4. In SPL, as in Algol 60 or Pascal, the body of a FOR loop was evidently a statement, using DO as a keyword to separate the loop setup from the body, so if you wanted a multi-statement loop you would presumably use a BEGIN END block, while "Action!" used Algol-68-style DO-OD (and IF-THEN-ELSEIF-ELSE-FI).
5. SPL used the UNTIL keyword to introduce upward-counting loop limits, while "Action!" used TO.
6. "Action!" seems to have used =[0] to initialize a variable to zero, while SPL used =0; in "Action!", =0 would have put the variable at address 0. Most versions of Algol, annoyingly including Pascal, had no way to initialize variables at their declaration. Presumably, if "Action!" were derived from SPL, it would have used the SPL syntax to initialize variables, and some other syntax to specify their location, like =@0 or =^0 or =[0] or something.
7. Also, not seen here, "Action!" evidently spelled INTEGER as INT.
There are a number of other differences on view here, such as the use of BYTE and CARD (unsigned) variables, the absence of the program-terminating period character, the presence of an augmented assignment operator ==+, and having the top-level syntactic construct be a procedure rather than a block, but those seem to me like the kinds of things you might change because you were building an IDE or because you were running on a tiny microcomputer. By contrast, these purely syntactic choices seem to me to be freely chosen on the basis of aesthetics or tradition, with almost no technical advantages or disadvantages accruing to any possible choice.
However, the Micro-SPL manual is at http://www.bitsavers.org/pdf/xerox/alto/micro-spl/Micro-SPL_.... On points of difference 1, 2, 4, and 7, it is similar to "Action!" rather than SPL, though it does follow SPL on point 3. As for points 5 and 6, Micro-SPL didn't have FOR loops or variable initialization, explaining the syntactic divergence. Also the Micro-SPL manual is by Henry Baker and "Action!"'s author Clinton Parker, who apparently confirmed the identity on a podcast in 02015: https://ataripodcast.libsyn.com/antic-interview-111-clinton-...
So I guess you're right that "Action!" is indeed Micro-SPL in a trenchcoat. Sure fooled me!
As for compilation speed, I don't think there's anything particularly special about "Action!"'s syntax or semantics that would make it especially fast to compile. Not using a textual preprocessor like C, maybe. And it's probably an advantage that most statements are introduced by a keyword so that simple recursive-descent parsing (with a separate tokenizer) is all you need, but that's more about the ease of writing the compiler than about its performance.
This was the same time Baker was working on COMFY-65, a more interesting language that got less adoption because it was not released for decades.