Specimen programs Case study 2 - a stack-oriented computer
4.4.3 Specimen programs
As before, some samples of program code for the machine may help to clarify various points. Example 4.4 To illustrate how the memory is allocated, consider a simple section of program that corresponds to high-level code of the form X := 8; WriteY = , Y; ; Example 4.4 0 DSP 2 ; X is at Mem[CPU.BP-1], Y is at Mem[CPU.BP-2] 2 ADR -1 ; push address of X 4 LIT 8 ; push 8 6 STO ; X := 8 7 STK ; dump stack to look at it 8 PRS ’Y = ’ ; Write string Y = 10 ADR -2 ; push address of Y 12 VAL ; dereference 13 PRN ; Write integer Y 14 HLT ; terminate execution This would be stored in memory as DSP 2 ADR -1 LIT 8 STO STK PRS 510 ADR -2 VAL PRN HLT 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ... Y X 0 ’ ’ ’=’ ’ ’ ’Y’ 0 504 505 506 507 508 509 510 511 Immediately after loading this program and before executing the DSP instruction, the program counter PC would have the value 0, while the base register BP and stack pointer SP would each have the value 506. Example 4.5 Example 4.4 scarcely represents the epitome of the programmer’s art A more ambitious program follows, as a translation of the simple algorithm BEGIN Y := 0; REPEAT READX; Y := X + Y UNTIL X = 0; WRITE’Total is ’, Y; END This would require a stack frame of size two to contain the variables X and Y . The machine code might read ; Example 4.5 0 DSP 2 ; X is at Mem[CPU.BP-1], Y is at Mem[CPU.BP-2] 2 ADR -2 ; push address of Y CPU.BP-2 on stack 4 LIT 0 ; push 0 on stack 6 STO ; store 0 as value of Y 7 ADR -1 ; push address of X CPU.BP-1 on stack 9 INN ; read value, store on X 10 ADR -2 ; push address of Y on stack 12 ADR -1 ; push address of X on stack 14 VAL ; dereference - value of X now on stack 15 ADR -2 ; push address of Y on stack 17 VAL ; dereference - value of Y now on stack 18 ADD ; add X to Y 19 STO ; store result as new value of Y 20 ADR -1 ; push address of X on stack 22 VAL ; dereference - value of X now on stack 23 LIT 0 ; push constant 0 onto stack 25 EQL ; check equality 26 BZE 7 ; branch if X 0 28 PRS ’Total is’ ; label output 30 ADR -2 ; push address of Y on stack 32 VAL ; dereference - value of Y now on stack 33 PRN ; write result 34 HLT ; terminate execution Exercises 4.17 Would you write code anything like that given in Example 4.5 if you had to translate the corresponding algorithm into a familiar ASSEMBLER language directly? 4.18 How difficult would it be to hand translate programs written in this stack machine code into your favourite ASSEMBLER ? 4.19 Use the stack language and, in due course, its interpreter to write and test the simple programs suggested in Exercises 4.6.4.4.4 An emulator for the stack machine
Parts
» compilers and compiler generators 1996
» Objectives compilers and compiler generators 1996
» Systems programs and translators
» The relationship between high-level languages and translators
» T-diagrams Classes of translator
» Phases in translation compilers and compiler generators 1996
» Multi-stage translators compilers and compiler generators 1996
» Interpreters, interpretive compilers, and emulators
» Using a high-level host language Porting a high level translator
» Bootstrapping Self-compiling compilers compilers and compiler generators 1996
» The half bootstrap compilers and compiler generators 1996
» Bootstrapping from a portable interpretive compiler A P-code assembler
» Simple machine architecture compilers and compiler generators 1996
» Addressing modes compilers and compiler generators 1996
» Machine architecture Case study 1 - A single-accumulator machine
» Instruction set Case study 1 - A single-accumulator machine
» A specimen program Case study 1 - A single-accumulator machine
» An emulator for the single-accumulator machine
» A minimal assembler for the machine
» Machine architecture Case study 2 - a stack-oriented computer
» Instruction set Case study 2 - a stack-oriented computer
» Specimen programs Case study 2 - a stack-oriented computer
» An emulator for the stack machine
» Syntax, semantics, and pragmatics
» Languages, symbols, alphabets and strings Regular expressions
» Grammars and productions compilers and compiler generators 1996
» Classic BNF notation for productions
» Simple examples compilers and compiler generators 1996
» Phrase structure and lexical structure -productions
» Semantic overtones The British Standard for EBNF Lexical and phrase structure emphasis
» One- and two-pass assemblers, and symbol tables
» Source handling Towards the construction of an assembler
» Lexical analysis Towards the construction of an assembler
» Syntax analysis Towards the construction of an assembler
» Symbol table structures The first pass - static semantic analysis
» The second pass - code generation
» Symbol table structures The first pass - analysis and code generation
» Error detection compilers and compiler generators 1996
» Simple expressions as addresses
» Improved symbol table handling - hash tables
» Macro processing facilities compilers and compiler generators 1996
» Conditional assembly compilers and compiler generators 1996
» Relocatable code compilers and compiler generators 1996
» Further projects compilers and compiler generators 1996
» Equivalent grammars Case study - equivalent grammars for describing expressions
» Useless productions and reduced grammars -free grammars
» Ambiguous grammars compilers and compiler generators 1996
» Type 1 Grammars Context-sensitive The Chomsky hierarchy
» The relationship between grammar type and language type
» EBNF description of Clang A sample program
» Context sensitivity Deterministic top-down parsing
» Terminal successors, the FOLLOW function, and LL1 conditions for non -free grammars
» Further observations Restrictions on grammars so as to allow LL1 parsing
» Alternative formulations of the LL1 conditions
» The effect of the LL1 conditions on language design
» Construction of simple recursive descent parsers
» Case studies compilers and compiler generators 1996
» Syntax error detection and recovery
» Construction of simple scanners
» LR parsing compilers and compiler generators 1996
» Automated construction of scanners and parsers
» Embedding semantic actions into syntax rules
» Attribute grammars compilers and compiler generators 1996
» Synthesized and inherited attributes
» Classes of attribute grammars
» Case study - a small student database
» Installation Input file preparation
» Execution Output from CocoR Assembling the generated system
» Case study - a simple adding machine
» Character sets Comments and ignorable characters
» Pragmas User names The scanner interface
» Syntax error recovery Parser specification
» Grammar checks Semantic errors
» Interfacing to support modules The parser interface
» Essentials of the driver program Customizing the driver frame file
» Case study - Understanding C declarations
» Case study - Generating one-address code from expressions
» Case study - Generating one-address code from an AST
» Case study - How do parser generators work?
» Project suggestions compilers and compiler generators 1996
» Overall compiler structure compilers and compiler generators 1996
» A hand-crafted source handler
» A hand-crafted scanner Lexical analysis
» A CocoR generated scanner Efficient keyword recognition
» A hand-crafted parser A CocoR generated parser
» Syntax error handling in hand-crafted parsers
» Error reporting The symbol table handler
» Other aspects of symbol table management - further types
» The code generation interface
» Code generation for a simple stack machine
» Machine tyranny Other aspects of code generation
» Abstract syntax trees as intermediate representations
» Simple optimizations - constant folding
» Simple optimizations - removal of redundant code
» Generation of assembler code
» Source handling, lexical analysis and error reporting Syntax
» The scope and extent of identifiers
» Symbol table support for the scope rules
» Parsing declarations and procedure calls
» Hypothetical machine support for the static link model
» Code generation for the static link model
» Hypothetical machine support for the display model
» Code generation for the display model
» Relative merits of the static link and display models
» Syntax and semantics compilers and compiler generators 1996
» Symbol table support for context-sensitive features
» Actual parameters and stack frames
» Hypothetical stack machine support for parameter passing
» Semantic analysis and code generation in a hand-crafted compiler
» Semantic analysis and code generation in a CocoR generated compiler
» Scope rules Language design issues
» Function return mechanisms Language design issues
» Context sensitivity and LL1 conflict resolution Fundamental concepts
» Parallel processes, exclusion and synchronization
Show more