Presentation is loading. Please wait.

Presentation is loading. Please wait.

©SoftMoore ConsultingSlide 1 The CPRL Virtual Machine.

Similar presentations


Presentation on theme: "©SoftMoore ConsultingSlide 1 The CPRL Virtual Machine."— Presentation transcript:

1 ©SoftMoore ConsultingSlide 1 The CPRL Virtual Machine

2 ©SoftMoore ConsultingSlide 2 CPRLVM (Compiler PRoject Language Virtual Machine) CPRLVM –is a hypothetical computer designed to simplify the code generation phase of a compiler for CPRL –uses a stack architecture; i.e., most instructions either expect values on the stack, place results on the stack, or both –has 4 internal or special purpose registers, but no general purpose registers Memory is organized into 8-bit bytes. Each byte is directly addressable. A word is a logical grouping of 4 consecutive bytes in memory. The address of a word is the address of its first (low) byte.

3 ©SoftMoore ConsultingSlide 3 Representing Primitive Types Boolean values are represented in a single byte. –0 means false. –Any nonzero value is interpreted as true. Character values (Unicode) use 2 bytes. Integer values use a word (32-bit 2’s complement)

4 ©SoftMoore ConsultingSlide 4 CPRLVM Instructions All CPRLVM instruction operation codes (opcodes) occupy one byte of memory. Some instructions take one or two one-word arguments, which are always located in the words immediately following the instruction in memory. Most instructions get their operands from the stack. In general, the operands are removed from the stack whenever the instruction is executed, and any results are left on the top of the stack.

5 ©SoftMoore ConsultingSlide 5 Examples: CPRLVM Instructions ADD : Remove two integers from the top of the stack and push their sum back onto the stack. INC : Add 1 to the integer at the top of the stack. LOADW (load word): Load/push a word (four consecutive bytes) onto the stack. The address of the first byte of the word is obtained by popping it off the top of the stack. LDMEM (load memory address): Load/push the contents of a memory address onto the top of the stack.

6 ©SoftMoore ConsultingSlide 6 Registers Four 32-bit internal registers (no general-purpose registers) PC (program counter; a.k.a. instruction pointer) - holds the address of the next instruction to be executed. SP (stack pointer) - holds the address of the top of the stack. The stack grows from low-numbered memory addresses to high-numbered memory addresses. SB (stack base) – holds the address of the bottom of the stack. When a program is loaded, SB is initialized to the address of the first free byte in memory. BP (base pointer) - holds the base address of the subprogram currently being executed.

7 ©SoftMoore ConsultingSlide 7 Program Loaded in Memory Program Code Variable Stack Free Space (Unused Memory) PC BP SP SB Low-numbered memory addresses High-numbered memory addresses Stack grows in this direction

8 Variable Addressing Using the SB and BP Registers Variables declared at program scope are addressed relative to the SB register. Variables declared at subprogram scope are addressed relative to the BP register. Example: If SB has the value 112, and program scoped variable x has the relative address 8, then the actual address of x is [SB] + relAddr(x) or 120. When preparing for code generation, the compiler needs to determine the relative address of every variable. For programs that don’t have subprograms, both SB and BP will point to the same memory location. ©SoftMoore ConsultingSlide 8

9 Opcodes LDGADDR and LDADDR LDGADDR n –load global address for variable at offset n –pushes SB + n onto the stack –used for variables declared at program scope LDADDR n –load address for variable at offset n –pushes BP + n onto the stack –used for variables declared at subprogram scope ©SoftMoore ConsultingSlide 9

10 ©SoftMoore ConsultingSlide 10 CPRL Example declare var m, n : Integer; var c : Char; const five := 5; begin m := 7; n := five*m; c := 'X'; writeln "n = ", n; writeln "c = ", c; end.

11 CPRL Example Disassembled 0: PROGRAM 10 5: LDGADDR 0 10: LDCINT 7 15: STOREW 16: LDGADDR 4 21: LDCINT 5 26: LDGADDR 0 31: LOADW 32: MUL 33: STOREW 34: LDGADDR 8 39: LDCCH 'X' 42: STORE2B 43: LDCSTR "n = " 56: PUTSTR 57: LDGADDR 4 62: LOADW 63: PUTINT 64: PUTEOL 65: LDCSTR "c = " 78: PUTSTR 79: LDGADDR 8 84: LOAD2B 85: PUTCH 86: PUTEOL 87: HALT ©SoftMoore ConsultingSlide 11 m: relative address = 0, absolute address = 88 n: relative address = 4, absolute address = 92 c: relative address = 8, absolute address = 96

12 ©SoftMoore ConsultingSlide 12 Loading a Program The object code is loaded into the beginning of memory Register PC is initialized to the address of the first instruction Registers SB and BP are initialized to the address following the last instruction. Register SP is initialized to BP - 1. The first instruction, program n, allocates n bytes on the top of the stack for local variables

13 ©SoftMoore ConsultingSlide 13 CPRL Example in Memory (after execution of first instruction) m: relative address = 0 0 BP SP … 87 88 SB 97 machine instructions for the program n: relative address = 4 c: relative address = 8 … unused memory 92 96 PC PROGRAM LDADDR HALT 10


Download ppt "©SoftMoore ConsultingSlide 1 The CPRL Virtual Machine."

Similar presentations


Ads by Google