Download Runtime Support in Modern Programming Languages and more Slides Advanced Computer Programming in PDF only on Docsity!
Language Systems
Chapter Four Modern Programming Languages, 2nd ed. (^) Docsity.com 1
Outline
The classical sequence
Variations on the classical sequence
Binding times
Debuggers
Runtime support
Chapter Four Modern Programming Languages, 2nd ed. (^) Docsity.com 2
Creating
The programmer uses an editor to create a text file
containing the program
A high-level language: machine independent
This C-like example program calls fred 100
times, passing each i from 1 to 100:
Chapter Four Modern Programming Languages, 2nd ed. 4
int i;
void main() {
for (i=1; i<=100; i++)
fred(i);
Docsity.com
Compiling
Compiler translates to assembly language
Machine-specific
Each line represents either a piece of data,
or a single machine-level instruction
Programs used to be written directly in
assembly language, before Fortran (1957)
Now used directly only when the compiler
does not do what you want, which is rare
Chapter Four Modern Programming Languages, 2nd ed. (^) Docsity.com 5
Assembling
Assembly language is still not directly
executable
– Still text format, readable by people
– Still has names, not memory addresses
Assembler converts each assembly-
language instruction into the machine’s
binary format: its machine language
Resulting object file not readable by people
Chapter Four Modern Programming Languages, 2nd ed. (^) Docsity.com 7
Chapter Four Modern Programming Languages, 2nd ed. 8
i: data word 0
main: move 1 to i
t1: compare i with 100
jump to t2 if greater
push i
call fred
add 1 to i
go to t
t2: return
assembler
xxxx i
xx i x xxxxxx xxxx i x fred xxxx i xxxxxx xxxxxx
i:
main:
Docsity.com
Chapter Four Modern Programming Languages, 2nd ed. 10
linker
xxxx i
xx i x xxxxxx xxxx i x fred xxxx i xxxxxx xxxxxx
i:
main: xxxx i
xx i x xxxxxx xxxx i x fred xxxx i xxxxxx xxxxxx
i:
main:
fred: xxxxxx xxxxxx xxxxxx
Docsity.com
Loading
“Executable” file still not directly
executable
– Still has some names
– Mostly machine language, but not entirely
Final step: when the program is run, the
loader loads it into memory and replaces
names with addresses
Chapter Four Modern Programming Languages, 2nd ed. (^) Docsity.com 11
Chapter Four Modern Programming Languages, 2nd ed. 13
loader
xxxx i
xx i x xxxxxx xxxx i x fred xxxx i xxxxxx xxxxxx
i:
main:
fred: xxxxxx xxxxxx xxxxxx
xxxx 80
xx 80 x xxxxxx xxxx 80 x 60 xxxx 80 xxxxxx xxxxxx
60: xxxxxx xxxxxx xxxxxx
(main)
(fred)
(i)
Docsity.com
Running
After loading, the program is entirely
machine language
– All names have been replaced with memory
addresses
Processor begins executing its instructions,
and the program runs
Chapter Four Modern Programming Languages, 2nd ed. (^) Docsity.com 14
About Optimization
Code generated by a compiler is usually
optimized to make it faster, smaller, or both
Other optimizations may be done by the
assembler, linker, and/or loader
A misnomer: the resulting code is better,
but not guaranteed to be optimal
Chapter Four Modern Programming Languages, 2nd ed. (^) Docsity.com 16
Example
Original code:
Improved code, with loop invariant moved:
Chapter Four Modern Programming Languages, 2nd ed. 17
int i = 0;
while (i < 100) {
a[i++] = xxx;
int i = 0;
int temp = xxx;
while (i < 100) {
a[i++] = temp;
Docsity.com
Other Optimizations
Some, like LIR, add variables
Others remove variables, remove code, add
code, move code around, etc.
All make the connection between source
code and object code more complicated
A simple question, such as “What assembly
language code was generated for this
statement?” may have a complicated answer
Chapter Four Modern Programming Languages, 2nd ed. (^) Docsity.com 19
Outline
The classical sequence
Variations on the classical sequence
Binding times
Debuggers
Runtime support
Chapter Four Modern Programming Languages, 2nd ed. (^) Docsity.com 20