Modified stdlib to use stack rotations to timeshare heap registers.
[vvhitespace] / stdlib / stdio.pvvs
CommitLineData
8bed3ccd
AT
1#ifndef VVS_STDLIB_STDIO
2#define VVS_STDLIB_STDIO
3
4@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
bb21580a 5@ Name:
b8b65c17 6@ printstackstring (1000100)
8bed3ccd 7@ Description:
bb21580a 8@ Prints a null-terminated string from the stack.
8bed3ccd
AT
9@ Call Stack:
10@ null-terminator (ASCII '\0')
11@ char n
12@ ...
13@ char 2
14@ char 1 <-- TOS
15@ Return Stack:
16@ <empty>
17@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
b8b65c17 18NSSVTSSSTSSN | Mark: 1000100 (print string from stack)
32c440bf 19SNS | DUP
b8b65c17 20NTSSTSSSTSSSSSSSSSTN | BRZ > 01000100 00000001
32c440bf 21TNSS | Print character
b8b65c17
AT
22NSNTSSSTSSN | JMP > 1000100
23NSSVSTSSSTSSSSSSSSSTN | Mark: 01000100 00000001
32c440bf
AT
24SNN | DROP
25NTN | RTS
26
8bed3ccd 27@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
bb21580a 28@ Name:
b8b65c17 29@ printheapstring (1000101)
8bed3ccd 30@ Description:
bb21580a 31@ Prints a null-terminated string from the heap.
8bed3ccd
AT
32@ Call Stack:
33@ pointer to first character <-- TOS
34@ Return Stack:
35@ <empty>
36@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
b8b65c17 37NSSVTSSSTSTN | Mark: 1000101 (print string from heap)
32c440bf
AT
38SNS | DUP
39TTT | LOAD
40SNS | DUP
b8b65c17 41NTSSTSSSTSTSSSSSSSTN | BRZ > 01000101 00000001
32c440bf
AT
42TNSS | Print character
43SSSTN | Push +1
44TSSS | ADD
b8b65c17
AT
45NSNTSSSTSTN | JMP > 1000101
46NSSVSTSSSTSTSSSSSSSTN | Mark: 01000101 00000001
32c440bf
AT
47SNN | DROP
48SNN | DROP
49NTN | RTS
8bed3ccd 50
3625ff3a 51@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
bb21580a 52@ Name:
23d17247 53@ printstacknumber (1001)
3625ff3a 54@ Description:
bb21580a 55@ Prints 'number' from the stack in sign-magnitude format.
3625ff3a
AT
56@ Leading zeros are suppressed.
57@ Call Stack:
bb21580a 58@ number <-- TOS
3625ff3a
AT
59@ Return Stack:
60@ <empty>
61@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
23d17247 62NSSVTSSTN | Mark: 1001 (print number from stack)
3625ff3a 63SNS | DUP
ae1f85a1
AT
64NSTTSSSSTSN | JSR > 1000010
65NSTTSSSSTTN | JSR > 1000011
3625ff3a
AT
66NTN | RTS
67
68@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
bb21580a 69@ Name:
ae1f85a1 70@ printstacknumbersign (1000010)
3625ff3a 71@ Description:
bb21580a 72@ Prints the sign of 'number' from the stack.
3625ff3a 73@ Call Stack:
bb21580a 74@ number <-- TOS
3625ff3a
AT
75@ Return Stack:
76@ <empty>
77@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ae1f85a1
AT
78NSSVTSSSSTSN | Mark: 1000010 (print sign of number from stack)
79NTTSTSSSSTSSSSSSSSTN | BMI > 01000010 00000001
3625ff3a 80SSSTSTSTTN | PUSH ASCII '+'
ae1f85a1
AT
81NSNSTSSSSTSSSSSSSTSN | JMP > 01000010 00000010
82NSSVSTSSSSTSSSSSSSSTN | Mark: 01000010 00000001
3625ff3a 83SSSTSTTSTN | PUSH ASCII '-'
ae1f85a1 84NSSVSTSSSSTSSSSSSSTSN | Mark: 01000010 00000010
3625ff3a
AT
85TNSS | PUTC
86NTN | RTS
87
88@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
bb21580a 89@ Name:
ae1f85a1 90@ printstacknumbermagnitude (1000011)
3625ff3a 91@ Description:
bb21580a 92@ Prints the magnitude of 'number' from the stack.
3625ff3a 93@ Call Stack:
bb21580a 94@ number <-- TOS
3625ff3a
AT
95@ Return Stack:
96@ <empty>
97@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
bb21580a 98#include <math.pvvs>
ae1f85a1 99NSSVTSSSSTTN | Mark: 1000011 (print magnitude of number from stack)
3625ff3a
AT
100NSTTSSSTN | JSR > 10001 (absolute value)
101
102SSSSN | PUSH ASCII '\0'
103SNT | SWAP
104
105@ Pick off one digit on each pass through this loop.
ae1f85a1 106NSSVSTSSSSTTSSSSSSSSN | Mark: 01000011 00000000
3625ff3a
AT
107SNS | DUP
108
109@ Mod-off a digit, convert to ASCII, store on stack as part of the string.
110SSSTSTSN | PUSH +10
111TSTT | MODULO
112SSSTTSSSSN | PUSH ASCII '0'
113TSSS | ADD
114SNT | SWAP
115
116@ Divide down to next digit and keep looping if number != 0 yet.
117SSSTSTSN | PUSH +10
118TSTS | DIVIDE
119SNS | DUP
ae1f85a1
AT
120NTSSTSSSSTTSSSSSSSTN | BRZ > 01000011 00000001
121NSNSTSSSSTTSSSSSSSSN | JMP > 01000011 00000000
3625ff3a
AT
122
123@ Print the string we have built on the stack.
ae1f85a1 124NSSVSTSSSSTTSSSSSSSTN | Mark: 01000011 00000001
3625ff3a 125SNN | DROP
45abb94e 126NSTTSSSTSSN | JSR > 1000100 (print string from stack)
3625ff3a
AT
127NTN | RTS
128
23d17247
AT
129@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
130@ Name:
131@ printf
132@ Description:
133@ If printing a static string (i.e. no substitutions), pass
134@ 'number of substitutions' as 0, immediately followed by 'string word 1'.
135@ If printing a string from the heap instead of stack, pass an empty string
136@ on the stack followed by a pointer to the first word of the
137@ null-terminated string on the heap.
138@ For example:
139@ pointer
140@ ASCII '\0'
141@ substitution n
142@ <remainder of call stack is unchanged>
6cb31a3e
AT
143@ Maximum substitutions determined by upper heap limit in stackrotate and
144@ stackrotatereverse subroutines.
23d17247
AT
145@ Call Stack:
146@ ACSII '\0'
147@ string word n
148@ ...
149@ string word 1
150@ substitution n
151@ ...
152@ substitution 1
153@ number of substitutions <-- TOS
154@ Return Stack:
155@ <empty>
156@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
157#include <heap.pvvs>
158#include <string.pvvs>
159NSSVTSSSN | Mark: 1000 (printf)
160
23d17247
AT
161@ If the stack contains an empty string (i.e. just an ASCII '\0'), the next
162@ word is a pointer we must use to load the string from the heap.
6cb31a3e 163@ This will leave the stack looking exactly like the example call stack above.
23d17247
AT
164@ Do the test this way so we can keep the code inline.
165SNS | DUP
6cb31a3e
AT
166SSSTSN | PUSH 2
167TSSS | ADD
168NSTTTSSN | JSR > 1100 (deepdup)
23d17247
AT
169SSTTN | PUSH -1
170TSSN | MULTIPLY
171NTTSSSSTSSSSSSSSSSTN | BMI > 00001000 00000001
6cb31a3e
AT
172SNS | DUP
173SSSTSN | PUSH 2
174TSSS | ADD
175NSTTSTTN | JSR > 1011 (stackrotatereverse)
23d17247
AT
176SNN | DROP
177SNS | DUP
6cb31a3e
AT
178SSSTSN | PUSH 2
179TSSS | ADD
180NSTTSTTN | JSR > 1011 (stackrotatereverse)
181NSTSSSSTSSSTSSSTTSSN | JSR > 00001000 10001100 (printf_deepslurp)
23d17247
AT
182
183@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
184@ The rest of printf parses a string according to the following information.
185@ ASCII '\ ':
186@ ASCII '\ ': putchar '\ '
187@ ASCII '%': putchar '%'
188@ ASCII 'n': putchar '\n'
189@ ASCII 't': putchar '\t'
190@ ASCII '%':
191@ ASCII 'c': (print character)
192@ ASCII 's': (print string)
193@ ASCII 'd': (print decimal digit)
194@ ASCII 'u': (print abs(integer), w/o sign)
195@ ASCII 'i': (print integer w/sign)
196@ ASCII '\0':
197@ cleanup and exit
198@ default:
199@ putchar
200@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
201
202@@@@@@@@@@@@@@@@@@@@
6cb31a3e 203@ The next block tests the char against all possible level 1 branches (see above).
23d17247
AT
204@ If there is a match, execution jumps to the appropriate level 2 branch label.
205@ If no match is found, print the character and move on.
206@@@@@@@@@@@@@@@@@@@@
207NSSVSSSSTSSSSSSSSSSTN | Mark: 00001000 00000001
6cb31a3e
AT
208@ Move the next character of the string to TOS.
209NSTSSSSTSSSTSSSTSTTN | JSR > 00001000 10001011 (next char to TOS)
23d17247
AT
210@ TOS is an ASCII '\ '. Jump to process the possible level 2 branches.
211SNS | DUP
212SSSTSTTTSSN | PUSH ASCII slash
213TSST | SUBTRACT
214NTSSSSSTSSSSSSSSSTSN | BRZ > 00001000 00000010
215@ TOS is an ASCII '%'. Jump to process the possible level 2 branches.
216SNS | DUP
217SSSTSSTSTN | PUSH ASCII '%'
218TSST | SUBTRACT
219NTSSSSSTSSSSSSSSSTTN | BRZ > 00001000 00000011
220@ TOS is an ASCII "\0". Jump to clean-up-and-exit.
221SNS | DUP
222NTSSSSSTSSSSSSSSSSSN | BRZ > 00001000 00000000
223@ TOS is a normal character. Print it and loop again.
224TNSS | PUTC
225NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
226
227@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
228@ Level 2 - ASCII '\ ' - Escapes
229@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
230
231@@@@@@@@@@@@@@@@@@@@
232@ The level 1 match was an ASCII '\ '.
233@ Now look for level 2 matches that trigger a character substitution (n -> newline, etc).
234@ If no matches are found, print the character directly (e.g. "\%" -> '%')
235@ When finished, loop back to testing level 1 branches.
236@@@@@@@@@@@@@@@@@@@@
237NSSVSSSSTSSSSSSSSSTSN | Mark: 00001000 00000010
238SNN | DROP
6cb31a3e
AT
239@ Move the next character of the string to TOS.
240NSTSSSSTSSSTSSSTSTTN | JSR > 00001000 10001011 (next char to TOS)
23d17247
AT
241@ Check for ASCII '\n'
242SNS | DUP
243SSSTTSTTTSN | PUSH ASCII 'n'
244TSST | SUBTRACT
245NTSSSSSTSSSSSSSSTSSN | BRZ > 00001000 00000100
246@ Check for ASCII '\t'
247SNS | DUP
248SSSTSSTN | PUSH ASCII '\t'
249TSST | SUBTRACT
250NTSSSSSTSSSSSSSSTSTN | BRZ > 00001000 00000101
251@ No substitution necessary. Print literally.
252TNSS | PUTC
253NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
254
255@@@@@@@@@@@@@@@@@@@@
256@ These are utility labels to output the appropriate non-printable ASCII character.
257@ After output, they loop back to testing level 1 branches.
258@@@@@@@@@@@@@@@@@@@@
259
260@ Print a newline and loop for the next character.
261NSSVSSSSTSSSSSSSSTSSN | Mark: 00001000 00000100
262SNN | DROP
263SSSTSTSN | PUSH ASCII '\n'
264TNSS | PUTC
265NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
266
267@ Print a horizontal tab and loop for the next character.
268NSSVSSSSTSSSSSSSSTSTN | Mark: 00001000 00000101
269SNN | DROP
270SSSTSSTN | PUSH ASCII '\t'
271TNSS | PUTC
272NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
273
274@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
275@ Level 2 - ASCII '%' - Substitutions
276@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
277
278@@@@@@@@@@@@@@@@@@@@
279@ The level 1 match was an ASCII '%'.
280@ Now look for level 2 matches that trigger a substitution.
281@ When finished, loop back to testing level 1 branches.
282@@@@@@@@@@@@@@@@@@@@
283NSSVSSSSTSSSSSSSSSTTN | Mark: 00001000 00000011
284SNN | DROP
6cb31a3e
AT
285@ Move the next character of the string to TOS.
286NSTSSSSTSSSTSSSTSTTN | JSR > 00001000 10001011 (next char to TOS)
23d17247
AT
287@ Check for ASCII 'c' - Print character
288SNS | DUP
289SSSTTSSSTTN | PUSH ASCII 'c'
290TSST | SUBTRACT
291NTSSSSSTSSSSSSSSTTSN | BRZ > 00001000 00000110
292@ Check for ASCII 's' - Print string
293SNS | DUP
294SSSTTTSSTTN | PUSH ASCII 's'
295TSST | SUBTRACT
296NTSSSSSTSSSSSSSSTTTN | BRZ > 00001000 00000111
297@ Check for ASCII 'd' - Print decimal digit
298SNS | DUP
299SSSTTSSTSSN | PUSH ASCII 'd'
300TSST | SUBTRACT
301NTSSSSSTSSSSSSSTSSSN | BRZ > 00001000 00001000
302@ Check for ASCII 'u' - Print unsigned number
303SNS | DUP
304SSSTTTSTSTN | PUSH ASCII 'u'
305TSST | SUBTRACT
306NTSSSSSTSSSSSSSTSSTN | BRZ > 00001000 00001001
307@ Check for ASCII 'i' - Print signed number
308SNS | DUP
309SSSTTSTSSTN | PUSH ASCII 'i'
310TSST | SUBTRACT
311NTSSSSSTSSSSSSSTSTSN | BRZ > 00001000 00001010
312@ Unrecognized substitution specifier.
313@ For now, silently consume it and continue.
314@ Do not increment the substitution counter.
315@ TODO: Is this really what I want to do here?
316SNN | DROP
317NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
318
319
320@@@@@@@@@@@@@@@@@@@@
321@ These are utility labels to call the appropriate type of output subroutine.
6cb31a3e
AT
322@ After output, they decrement the substition counter and loop back to testing
323@ level 1 branches.
23d17247
AT
324@@@@@@@@@@@@@@@@@@@@
325
326@ Print a character
327NSSVSSSSTSSSSSSSSTTSN | Mark: 00001000 00000110
328SNN | DROP
6cb31a3e 329SNT | SWAP
23d17247 330TNSS | PUTC
6cb31a3e
AT
331SSSTN | PUSH 1
332TSST | SUBTRACT
23d17247
AT
333NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
334@ Print a string
335NSSVSSSSTSSSSSSSSTTTN | Mark: 00001000 00000111
336SNN | DROP
6cb31a3e 337SNT | SWAP
23d17247 338NSTTSSSTSTN | JSR > 1000101 (print string from heap)
6cb31a3e
AT
339SSSTN | PUSH 1
340TSST | SUBTRACT
23d17247
AT
341NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
342@ Print a decimal digit
343NSSVSSSSTSSSSSSSTSSSN | Mark: 00001000 00001000
344SNN | DROP
6cb31a3e 345SNT | SWAP
23d17247 346TNST | PUTDIGIT
6cb31a3e
AT
347SSSTN | PUSH 1
348TSST | SUBTRACT
23d17247
AT
349NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
350@ Print an unsigned integer
351NSSVSSSSTSSSSSSSTSSTN | Mark: 00001000 00001001
352SNN | DROP
6cb31a3e 353SNT | SWAP
23d17247 354NSTTSSSSTTN | JSR > 1000011 (print magnitude of number from stack)
6cb31a3e
AT
355SSSTN | PUSH 1
356TSST | SUBTRACT
23d17247
AT
357NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
358@ Print a signed integer
359NSSVSSSSTSSSSSSSTSTSN | Mark: 00001000 00001010
360SNN | DROP
6cb31a3e 361SNT | SWAP
23d17247 362NSTTSSTN | JSR > 1001 (print number from stack)
6cb31a3e
AT
363SSSTN | PUSH 1
364TSST | SUBTRACT
23d17247
AT
365NSNSSSSTSSSSSSSSSSTN | JMP > 00001000 00000001
366
367@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
368@ These are misc labels associated with the printf function.
369@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
370
6cb31a3e
AT
371@ Found an ASCII "\0" when processing the format string. Clean up and exit.
372NSSVSSSSTSSSSSSSSSSSN | Mark: 00001000 00000000
373SNN | DROP
374NTN | RTS
375
376@ Move the next string character to TOS.
377@ Stack should look like the printf call stack, with num-of-subs at TOS.
378NSSVSSSSTSSSTSSSTSTTN | Mark: 00001000 10001011 (next char to TOS)
379SNS | DUP
380SSSTSN | PUSH 2
23d17247 381TSSS | ADD
6cb31a3e 382NSTTSTTN | JSR > 1011 (stackrotatereverse)
23d17247
AT
383NTN | RTS
384
6cb31a3e
AT
385@ Slurps a string from the heap to the stack, storing it behind the substitutions.
386@ Call Stack:
387@ substitution n
388@ ...
389@ substitution 1
390@ number of substitutions <-- TOS
391@ pointer to string
392@ Return Stack:
393@ ACSII '\0'
394@ string word n
395@ ...
396@ string word 1
397@ substitution n
398@ ...
399@ substitution 1
400@ number of substitutions <-- TOS
401@ TODO: This, along with a deepspew, should probably be stdlib routines.
402NSSVSSSSTSSSTSSSTTSSN | Mark: 00001000 10001100 (printf_deepslurp)
403SNS | DUP
404@ Advance a duplicate copy of the pointer until it points to the null-terminator.
405NSSVSSSSTSSSTSSSTTSTN | Mark: 00001000 10001101
406SNS | DUP
407TTT | LOAD
408NTSSSSSTSSSTSSSTTTSN | BRZ > 00001000 10001110
409SSSTN | PUSH 1
410TSSS | ADD
411NSNSSSSTSSSTSSSTTSTN | JMP > 00001000 10001101
412@ Load a character to the stack on each pass through this loop.
413NSSVSSSSTSSSTSSSTTTSN | Mark: 00001000 10001110
414SNS | DUP
415TTT | LOAD
416SSSTSSN | PUSH 4
417NSTTTSSN | JSR > 1100 (deepdup)
418SSSTSSN | PUSH 4
419TSSS | ADD
420NSTTSTSN | JSR > 1010 (stackrotate)
421@ Test for end of loop.
422SNS | DUP
423SSSTTN | PUSH 3
424NSTTTSSN | JSR > 1100 (deepdup)
425TSST | SUBTRACT
426NTSSSSSTSSSTSSSTTTTN | BRZ > 00001000 10001111
427@ Decrement pointer to end of string, loop again.
428SSSTN | PUSH 1
429TSST | SUBTRACT
430NSNSSSSTSSSTSSSTTTSN | JMP > 00001000 10001110
431@ Clean up and return.
432NSSVSSSSTSSSTSSSTTTTN | Mark: 00001000 10001111
433SNN | DROP
23d17247
AT
434SNN | DROP
435NTN | RTS
436
8bed3ccd 437#endif