1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
#+title: TODOs
#+author: Aryadev Chavali
#+date: 2023-11-02
* TODO Standard library
I should start considering this and how a user may use it. Should it
be an option in the VM and/or assembler binaries (i.e. a flag) or
something the user has to specify in their source files?
Something to consider is /static/ and /dynamic/ "linking" i.e.:
+ Static linking: assembler inserts all used library definitions into
the bytecode output directly
+ We could insert all of it at the start of the bytecode file, and
with [[*Start points][Start points]] this won't interfere with
user code
+ Furthermore library code will have fixed program addresses (always
at the start) so we'll know at start of assembler runtime where to
resolve standard library subroutine calls
+ Virtual machine needs no changes to do this
+ Virtual machine has fixed program storage for library code, and
assembler makes jump references specifically for this program
storage (dynamic linking)
+ When assembling subroutine calls, just need to put references to
this library storage (some kind of shared state between VM and
assembler to know what these references are)
+ VM needs to manage a ROM of some kind for library code
+ How do we ensure assembled links to subroutine calls don't
conflict with user code jumps?
+ Possibility: most significant bit of a program address is
reserved such that if 0 it refers to user code and if 1 it
refers to library code
+ 63 bit references user code (not a lot of loss in precision)
+ Easy to check if a reference is a library reference or a user
code reference by checking "sign bit" (negativity)
* Completed
** DONE Write a label/jump system :ASM:
Essentially a user should be able to write arbitrary labels (maybe
through ~label x~ or ~x:~ syntax) which can be referred to by ~jump~.
It'll purely be on the assembler side as a processing step, where the
emitted bytecode purely refers to absolute addresses; the VM should
just be dealing with absolute addresses here.
** DONE Allow relative addresses in jumps :ASM:
As requested, a special syntax for relative address jumps. Sometimes
it's a bit nicer than a label.
** DONE Calling and returning control flow :VM: :ASM:
When writing library code we won't know the addresses of where
callers are jumping from. However, most library functions want to
return control flow back to where the user had called them: we want
the code to act almost like an inline function.
There are two ways I can think of achieving this:
+ Some extra syntax around labels (something like ~@inline <label>:~)
which tells the assembly processor to inline the label when a "jump"
to that label is given
+ This requires no changes to the VM, which keeps it simple, but a
major change to the assembler to be able to inline code. However,
the work on writing a label system and relative addresses should
provide some insight into how this could be possible.
+ A /call stack/ and two new syntactic constructs ~call~ and ~ret~
which work like so:
+ When ~call <label>~ is encountered, the next program address is
pushed onto the call stack and control flow is set to the label
+ During execution of the ~<label>~, when a ~ret~ is encountered,
pop an address off the call stack and set control flow to that
address
+ This simulates the notion of "calling" and "returning from" a
function in classical languages, but requires more machinery on
the VM side.
** DONE Start points
You know how in standard assembly you can write
#+begin_src asm
global _start
_start:
...
#+end_src
and that means the label ~_start~ is the point the program should
start from. This means the user can define other code anywhere in the
program and specify something similar to "main" in C programs.
Proposed syntax:
#+begin_src asm
init <label>
#+end_src
|