mutan icon indicating copy to clipboard operation
mutan copied to clipboard

scoping

Open geraldstanje opened this issue 9 years ago • 2 comments

hi, i try to understand how you implemented scoping... why is scopes a *list.List? -> https://github.com/obscuren/mutan/blob/master/front/int_gen.go

whats the difference between PushScope, PushStack, Push?

geraldstanje avatar Dec 17 '14 09:12 geraldstanje

A couple of things you should know about the VM:

  • VM is stack based (http://markfaction.wordpress.com/2012/07/15/stack-based-vs-register-based-virtual-machine-architecture-and-the-dalvik-vm/)
  • VM stack has a theoretical infinite size
  • Words in memory are 256 bits
whats the difference between PushScope, PushStack, Push?

Push or PUSH = 0x60 is a VM instruction. It pushes the next byte of the program to stack. The program: 0x6001600102 would do PUSH 1, PUSH 1, ADD (which would leave 2 on the VM stack)

PushStack Creates a new stack-frame (!= VM stack) in order to do scoping similar to standard C).

PushScope Pushes a compiler scope which helps identifying variable and their offset (for offsets see below).

Stack frames

Stack (see example below) and the stack frames in mutan are kept in check by the first word in memory, the stack pointer. The stack pointer points to the current stack-frame (i.e., scope) in order to find the variables currently in scope. A stack consists of the return position (for functions' return statements) and the stack size. This allows you to (something like this, it's been a while since I wrote this):

  • MLOAD the stack pointer
  • PUSH 2 because variable declaration start at offset 2 (__retPos & __stackSize)
  • ADD
  • PUSH the variable offset (e.g., var "b" would have an offset of 1, it's the second variable)
  • ADD in order to find the correct mem word that holds our local variable b
000: Stack Ptr ->-->-->-+
--- Stack frame 1       |
001: "___retPos"        |
002: "___stackSize"     |
003: Stack var "a"      |
004: Stack var "b"      |
005: Stack var "c"      |
--- Stack frame 2  <----+
006: "___retPos"
007: "___stackSize"
008: Stack var "a"
009: Stack var "b"
00a: Stack var "c"

Hope that explains it a bit. Also please note that I'm no longer actively developing this language.

obscuren avatar Dec 17 '14 11:12 obscuren

I found some other, old notes I had about this "feature" https://gist.github.com/obscuren/107d18ac9a1b6e7ea05c

obscuren avatar Dec 17 '14 11:12 obscuren