830 likes | 964 Views
Troisième Partie Chapitre 4 Un Processeur à Pile. A Generic Processor. T. B. . . . 5 4 3 2 1 0. . . . 5 4 3 2 1 0. I. P. code. data. Code memory is addressable, random access. During program execution, it is “read only”
E N D
Troisième Partie Chapitre 4 Un Processeur à Pile
A Generic Processor T B . . . 5 4 3 2 1 0 . . . 5 4 3 2 1 0 I P code data
Code memory is addressable, random access. During program execution, it is “read only” The “Program counter” (P) contains the address in code memory of the next instruction to be fetched When an instruction is fetched it is kept in the Instruction register (I) to be executed by the control unit. The operation of this stack machine will be described by means of an executable C++ program that interprets the instructions just as the actual hardware would do. Stack processorCode Memory Usage
Data memory is addressable, random access. Part of it can be managed as a LIFO stack Base register (B) Top of Stack register (T) Data memory is used to store: Variables of active blocks (= activation record) Temporary variables Return addresses of subroutines Static and dynamic data links Procedure parameters Stack processorData Memory Usage
Data handling : CHS : Change sign of top of stack (tos) EXC : Exchange two topmost elements of stack ADD : Add two topmost elements of stack SUB : Subtract tos from underlying element MUL : Multiply two topmost elements of stack DIV : Divide underlying element by tos EQ? : Are two topmost elements equal ? NE? : Are two topmost elements different ? LT? : Is tos less than underlying element ? LE? : Is tos less than or equal to underlying element ? GT? : Is tos greater than underlying element ? GE? : Is tos greater than or equal to underlying element ? Stack processorInstructions
Data transfer : LIT,a : load a constant on tos. LOD,l,a : copy value from specified address to tos STO,l,a : move value from tos to specified address. LDI,l,a : copy value from indirect address to tos STI,l,a : move value from tos to indirect address. Miscellaneous : INT,a : Increment stack pointer by constant value. LAD,l,a : Load specified address on tos. Stack processorInstructions
Control : JMP,a : Jump to specified address in code JPT,a : Jump if the value TRUE is on tos. JPF,a : Jump if the value FALSE is on tos JSR,a : Call subroutine at specified address in code RET : Return from subroutine HLT : Stop execution Stack processorInstructions
Interpreter : Data Types enum Opcode {EXC,CHS,ADD,SUB,MUL,DIV, EQ?,NE?,LT?,LE?,GT?,GE?, LIT,LOD,STO,LDI,STI,LIT,LAD, JMP,JPT,JPF,JSR,RET,HLT }; struct Instr {Opcode opc; int l,a; }
Interpreter : Variables instr Code[1000]; int D[1000]; instr I; int P,B,T;
Main Loop Running = true; do { I = Code[P]; P = P + 1; switch (I.opc) {case CHS : ... break; case EXC : ... break; ... case HLT : Running = false; } } while Running
Data memory is addressable, random access. Part of it can be managed as a LIFO stack Base register (B) Top of Stack register (T) Data memory is used to store: Variables of active blocks (= activation record) Temporary variables Return addresses of subroutines Static and dynamic data links Procedure parameters Stack processorData Memory Usage
Instruction Interpretation (1) // CHS : Change sign of top of stack (tos) case CHS : D[T] = - D[T]; break; // EXC : Exchange two topmost elements of stack case EXC : D[T+1] = D[T-1]; D[T-1] = D[T]; D[T] = D[T+1]; break; // ADD : Add two topmost elements of stack case ADD : T = T-1; D[T] = D[T] + D[T+1]; break;
Instruction Interpretation (2) // EQ? : Are two topmost elements equal ? case EQ? : T = T-1; D[T] = D[T+1] == D[T];break; // NE? : Are two topmost elements different ? case NE? : T = T-1; D[T] = D[T+1] != D[T];break; // LT? : Is tos less than underlying element ? case LT? : T = T-1; D[T] = D[T+1] < D[T];break;
Instruction Interpretation (3) // JMP,a : Jump to specified address in code case JMP : P = I.a; break; // JPT,a : Jump if the value TRUE is on tos. case JPT : T = T-1; if( D[T+1])P = I.a; break; // JPF,a : Jump if the value FALSE is on tos case JPF : T = T-1; if(!D[T+1])P = I.a; break;
Evaluate B FALSE TRUE B L1 S2 S1 L2 Compilation Pattern : IF if (B){S1}; else {S2}; Evaluate B JPF L1 Code for S1 JMP L2 L1 :Code for S2 L2: ...
Compilation Pattern : Loop while (B) do {S} ; L1 :Evaluate B JPF L2 Code for S JMP L1 L2 : . . . L1 Evaluate B FALSE B TRUE S L2
L1 S Evaluate B TRUE B FALSE Compilation Pattern : Loop do {S} while (B); L1 :Code for S Evaluate B JPT L1 . . .
Data memory is addressable, random access. Part of it can be managed as a LIFO stack Base register (B) Top of Stack register (T) Data memory is used to store: Variables of active blocks (= activation record) Temporary variables Return addresses of subroutines Static and dynamic data links Procedure parameters Stack processorData Memory Usage
Data Transfer instructionsfor local variables T B . . . 5 4 3 2 1 0 . . . 5 4 3 2 1 0 0.a I P code data
Instruction Interpretation (4)Restricted to local variables // LOD,0,a : copy value from specified address to tos case LOD : T = T+1; D[T] = D[B + I.a]; break; // STO,0,a : move value from tos to specified address. case STO : D[B+I.a] = D[T]; T = T-1; break;
Instruction Interpretation (5) // LIT,a : load a constant on tos. case LIT : T = T + 1; D[T] = I.a; break; // INT,a : modify stack pointer ( a pos. or neg.) case INT : T = T + I.a; break;
3 b a a+3 Compilation Pattern : expression x = (a+3) * b // a,b,x local variables Reverse polish notation : a 3 + b *, LOD,0,a LIT,3 ADD LOD,0,b MUL STO,0,x 1 2 3 4 1 a+3 (a+3)*b 2 3 4
Evaluate N and M a = N L2 FALSE a < M S a = a+1 L1 Compilation Pattern : for loop for (a = N;a <= M; a++) S;
Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 N T M B a:
Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 N T M B a: N
Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 N T M B a: N
Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 M T N B a: N
Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 M T false B a: N
Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 M T false B a: N
Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 M T false B a: N
Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 false T M B a: N
Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 false T M B a: N
Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 false T M B a: N
Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 N T M B a: N
Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 1 N T M B a: N
Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 1 N+1 T M B a: N
Compilation Pattern : for loop for (a = N;a <= M; a++) S; Evaluate N and M a = N Evaluate M Evaluate N L2STO 0,a INT 1 restore access to a EXC GT? M < N => skip loop JPT L1 go to next statement INT 2 save M for future use EXC code for S INT -1 LAD 0,a put value of a on tos LIT 1 put a 1 above it ADD JMP L2 restart the loop L2 FALSE a <= M S a = a+1 L1 N+1 T M B a: N+1
Data memory is addressable, random access. Part of it can be managed as a LIFO stack Base register (B) Top of Stack register (T) Data memory is used to store: Variables of active blocks (= activation record) Temporary variables Return addresses of subroutines Static and dynamic data links Procedure parameters Stack processorData Memory Usage
Data Memory Usage (1) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Record BEGIN…;YELLOW; PURPLE;... END BLUE.
Data Memory Usage (2) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; YELLOW BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.
Data Memory Usage (3) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; ORANGE YELLOW BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.
Data Memory Usage (4) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; YELLOW BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.
Data Memory Usage (5) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.
Data Memory Usage (6) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; PURPLE BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.
Data Memory Usage (7) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; RED PURPLE BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.
Data Memory Usage (8) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; PURPLE BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.
Data Memory Usage (9) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; GREEN PURPLE BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.
Data Memory Usage (10) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; PURPLE BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Records BEGIN…;YELLOW; PURPLE;... END BLUE.
Data Memory Usage (11) MODULE BLUE PROCEDURE YELLOW; PROCEDURE ORANGE; BEGIN … END ORANGE; Data Memory : BEGIN … ORANGE ... END YELLOW; PROCEDURE PURPLE; PROCEDURE RED; BEGIN …END RED; PROCEDURE GREEN; BEGIN ... END GREEN; BLUE BEGIN … RED; GREEN;... END PURPLE; Activation Record BEGIN…;YELLOW; PURPLE;... END BLUE.
Data memory is addressable, random access. Part of it can be managed as a LIFO stack Base register (B) Top of Stack register (T) Data memory is used to store: Variables of active blocks (= activation record) Temporary variables Return addresses of subroutines Static and dynamic data links Procedure parameters Stack processorData Memory Usage