컴파일러구성 제 10 강 중간언어 / 인터프리터
Motivation rapid development of machine architectures proliferation of programming languages portable & adaptable compiler design --- P_CODE porting --- rewriting only back-end compiler building system --- EM_CODE M front-ends + M compilers for N target machine N back-ends
source program front -end interface back -end target code target machine abstract machine code abstract machine interpreter
Pascal P Compiler --- portable compiler producing P_CODE for an abstract machine(p_machine). P_Machine ----- hypothetical stack machine designed for Pascal language. (1) Instruction --- closely related to the PASCAL language. (2) Registers PC --- program counter NP --- new pointer SP --- stack pointer MP --- mark pointer (3) Memory CODE --- instruction part STORE --- data part(constant area, stack, heap)
CODE PC STORE stack MP current activation record SP heap constant area NP
ACK(Amsterdam Compiler Kit) 알골형태의소스언어와바이트기계형태의목적기계에적합 EM Machine 중간언어는 EM(Encoding Machine) EM-code 의사명령어와 EM 명령어로구분 각명령어는상수, 명칭등과같은오퍼랜드를가짐 의사명령어는기억장소를확보, 상수정의 분리컴파일이나링킹기능을수행
EM 기억공간구조 SB //// Unimplemented memroy //// Stack and Local area // Implementation dependent // Heap area LB SP HP EB EB 0 Global area Program text //////// Undefined //////// PC
Ucode the intermediate form used by the Stanford Portable Pascal compiler. stack-based and is defined in terms of a hypothetical stack machine. Ucode Interpreter : Appendix B. Addressing stack addressing ===> a tuple : (B, O) B : the block number containing the address O : the offset in words from the beginning of the block, offsets start at 1. label to label any Ucode instruction with a label field. All targets of jumps and procedures must be labeled. All labels must be unique for the entire program.
Example : Consider the following skeleton : int x; void main() { int i; int j; //... } block number - 전역변수 : 1 - 함수내지역변수 : 2 variable addressing - x : (1,1) - i : (2,1) - j : (2,2)
Ucode Operations(39 개 ) Unary --- notop, neg, inc, dec, dup Binary --- add, sub, mult, div, mod, swp and, or, gt, lt, ge, le, eq, ne Stack Operations --- lod, str, ldc, lda Control Flow --- ujp, tjp, fjp Range Checking --- chkh, chkl Indirect Addressing --- ldi (load indirect), sti (store indirect) Procedure --- cal, ret, retv, ldp, proc, end Etc. --- nop, bgn, sym
Example : x = a + b * c; lod 1 1 /* a */ lod 1 2 /* b */ lod 1 3 /* c */ mult add str 1 4 /* x */ if (a>b) a = a + b; lod 1 1 /* a */ lod 1 2 /* b */ gt fjp next lod 1 1 /* a */ lod 1 2 /* b */ add str 1 1 /* a */ next...
Indirect Addressing is used to access the array elements. ldi --- indirect load replace stacktop by the value of the item at location stacktop. to retrieve A[i] : lod i // actually (Bi, Oi)) lda A // also (block number, offset) add ldi // effective address // indirect load gets contents of A[i]
sti --- indirect store sti stores stacktop into the address at stack[stacktop-1], both items are popped. A[i] = j; lod lda add lod Sti i A j
Procedure Calling Sequence function definition : void func(int x, int array[]) { } function call : func(a, list); calling sequence : ldp // load parameter lod a // load the value of actual parameter lda list // load the address of actual parameter call func // call func
Ucode Interpreter The Ucode interpreter is called ucodei, it s source is in the appendix B. (You can get it from plac.dongguk.ac.kr). The interpreter uses the following files : *.ucode : file containing the Ucode program. *.lst : Ucode listing and output from the program. Ucode format (fixed format) label-field op-code operand-field 1-10 12-m m+2 m is exactly enough to hold opcode. label field a 10 character label(make sure its 10 characters pad with blanks) op-code --- starts at 12 column.
Bytecode Java Virtual machine (JVM) 의명령어집합 Stack-based virtual machine code 이기종간의실행환경에적합 : 이식성, 유연성인터프리터방식과 JIT (Just-In-Time) compiler 방식에의해실행명령어개수는 230개 Compilation & Execution of Bytecode JAVA Program JAVA Compiler Bytecode JVM Result
.NET IL (MSIL).NET 환경에서언어통합을위해설계된중간언어 JIT (Just-In-Time) compiler 방식에의해실행언어독립적으로설계되어포괄적인프로그래밍가능 소스프로그램의기능및구조변화에잘적응하는중간언어 명령어개수는 236 개 CLR (Common Language Runtime) 에의해실행
Compilation & Execution of.net IL C# Managed C++ VB.NET ETC. C# Compiler Managed C++ Compiler VB.NET Compiler Language Compilers Compilation level PE File (exe/dll).net IL +Metadata Execution level CLR JIT Compilation Native Code
부록 B 에수록된 Ucode 인터프리터를각자 PC 에 설치하고 100 이하의소수 (prime number) 를구하는 프로그램을 Ucode 로작성하시오. 다른문제의프로그램을작성해서제출해도됨. Ucode 인터프리터출력리스트를제출. 참고 : #1 : recursive-decent parser #2 : MiniC LR parser
IL criteria intermediate level input language --- high level output machine --- low level efficient processing translation --- source to IL, IL to target interpretation optimization extensibility external representation clean separation language dependence & machine dependence
A : 좋다 B : 보통이다 C : 나쁘다