Sie sind auf Seite 1von 29

Code Generation

Today

Assembly Language

Cool programs in assembly

Runtime interface
Overview (already covered)

ARM (power-efficiency = 💰)

Raspberry Pi

RISC (ARM)

Raspbian
Tooling

gcc - compiler, linker, assembler

as - assembler

make - glue

nm - extract symbols from binary object

gdb - gnu debugger


Assembly features
Many registers (that we don't use)

r0-r3 => scratch

r4-r7 => procedure must save and restore

lr (link register)

pc (program counter)

sp alignment is of 8 (not 4) bytes


Assembly features
<label>: - declares symbols (address by name)

.global <label> - can be shared between object files

.word <data> - 32 bit space

.asciz <data> - string 0 terminated

.balign <n> - inserts padding so the next instruction


starts at multiple of n
Assembly features

#<n> - immediate value

[rn] - content of register n

=<label> - address of label (used because of segment


restrictions)

ldr - load str - store


Assembly features

Arithmetic - only works between register

cmp - compare (its just a substraction)

b<suffix> - many branches to check condition

bl, bx, blx - procedures 😍


Dynamic activation

f (1, 2, 3)

“activates" the procedure

1, 2, 3 => some temporal storage

parameters in registers => FAST


COOL programs in
assembly
Data Segment
General declarations

Literal objects

Structure tables

Names of classes

Dispatch tables (pointers to methods)

Prototypes
Data Segment: to solve

Structural analysis

Names of clases

Names of literals

Features of a class
Features of a class
Methods with inheritance

Dispatch table

Offset from base (dispatch table) address

Attributes

Prototype objects

Offset from base (current object) address


Current object must be always at r0
Text segment
Constructors

Default initialization of base classes

Optional initialization

Methods

Every method returns its only expression

Nested expression => visitor


String templates
String templates

Separation of concerns

out.println is messy

String.format is better but uhm messy too (no multiline)

Terence Parr is cool


Runtime interface
Base Methods
Object.copy

in => r0, object to copy

out => r0, fresh object

Object.abort

in => r0, offending object

out => 😵
Base Methods
Object.type_name

in => r0, some object

out => r0, string object of the name of input object

IO.out_string

in => r0, string to print

out => nothing


Base Methods
IO.out_int

in => r0, int object to print

out => nothing

IO.in_string

in => nothing

out => r0, string read from terminal


Base Methods
IO.in_int

in => nothing

out => r0, int object read from terminal

String.length

in => r0, string object

out => r0, int object of the length of the string


Base Methods
String.concat

in => r0, r1, strings objects to concatenate (r0+r1)

out => r0, fresh string with the input concatenated

String.substr

in => r0, string object, r1=index, r2=length

out => r0, fresh object built from the input


Base assembly utilities
equality_test (only for base clases, else false)

in => r0, output value when true, r1, output value when
false, r2, r3 objects to compare

out => input r0, r1 depending on wether r2==r3 based on:

void not allowed

types are equal

content is equal
Base assembly utilities

dispatch_abort

in => r0, filename, r1, number of line

out => prints message and 😵


Base assembly utilities
dispatch_abort - should be called when dispatch is attempted on
void object

in => r0, filename, r1, number of line

out => prints message and 😵

case_abort - Should be called when a case statement has no match

in => r0, name of the class that had no match

out => prints message and 😵


Base assembly utilities

case_abort2 - Should be called when a case is


attempted on void object

in => r0, filename, r1, number of line

out => prints message and 😵


Calling protocol

Pass first 4 parameters in registers (r0 must always be


the current class)

Preserve fp, lr, r4-r7 (as needed)

At the end recover fp, pc, r4-r7 (as needed)

More than 4 parameters? => stack, but be sure to


restore state
Starting a program

new Main object is created

Main_init gets called

Control transfer to Main.main

voilá!

Das könnte Ihnen auch gefallen