13.07.2015 Views

EBC Complier Presentation - UEFI

EBC Complier Presentation - UEFI

EBC Complier Presentation - UEFI

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Source Language• Run on various platform– 64-bit Clean• Small image– No floating point types/operations– No C++ (runtime library, exception handling)• Debuggability/Maintenance– No inline asm4


Source Language -64bitness• Natural Types– Pointer– INTN/UINTN• No static initialization using sizeof onnatural types5


Language Examples(1)main(){int *xp,, x, y;}y = 8;xp = &y;x = *xp*xp;6


Language Examples(2)int PASS;long f(long x){return x+sizeof(int);}main(){long x, y;}x=4;y = f(x);if ((y+f(xy+f(x))==16)PASS=1;else PASS=0;7


Virtual Machine• Registers• Instruction encoding• Operand Register encoding• Natural indexing• Instruction Set8


Registers• General purpose Registers–8 - 64 bit registers (R0 – R7)• Special Registers– 64 bit registers– Currently only 2 defined9


Registers cont.• General purpose RegistersIndexRegister0 R01-3 R1-R3R34-7 R4-R7R7DescriptionPoints to the top of the stackPreserved across callsScratch, not preserved acrosscalls• Special RegistersIndexRegisterDescription0 FLAGS 0 : Condition Code, 1..63Reserved1 IPPoints to current instruction2..7 ReservedNot defined10


Instruction Encoding• Opcode operand1 operand2BitSym0-5 OpDescriptionThe opcode of theinstruction6 W Width7 I Immediate data Present11


Operand Register Encoding• [@]RnBitDescription0..2 Operand 1 Register3 0 = Operand 1 is direct1 = Operand 1 is indirect4..6 Operand 2 register7 0 = Operand 2 is direct1 = Operand 2 is indirect12


Natural Indexing• Immediate data for indirect operands• Index = C + N * (Size of pointer in bytes)• Indexes can be 16, 32 or 64 bit wideBitDescriptionx+4 Sign bit, most significant bitx+1..x+3Bits assigned to natural units (w)a+1..x Constant units (C )0..aNatural units (N)13


Instruction Set• Program Flow• Compare• Data Manipulation• Data Movement14


Program Flow Instructions• BREAK• JMP• CALL• RETURN15


Program Flow Instructions• Break break_codeByte0DescriptionBitDescription5..7 Reserved, must be 00..4 Opcode=01 Break Code16


Program Flow Instructions• Break CodeCodeDescription0 Runaway program break1 VM Revision number2 Skip3 Debug Break.4 System Call.5 Create Thunk6 Set Compiler Version7..255 Reserved17


Program Flow Instructions• JMP64 [cs|cc[cs|cc] ] Imm64• JMP32 [cs|cc[cs|cc], [@]R1 [Imm32]• JMP8 [cs|cc[cs|cc] ] Imm818


Program Flow Instructions• JMPByteDescription0BitDescription7 1=Immediate data present6 0=32 bit immediate data 1 = 64 bit immediate data0..5 Opcode 11BitDescription7 0= unconditional 1= conditional6 0= CC 1=CS5 Reserved4 0= Absolute address 1= Relative address3 0 = Operand1 direct 1= Operand1 indirect0..2 Operand12..5 Optional 32 bit immediate data2..7 Optional 64 bit immediate data19


Program Flow Instructions• JMP8Byte0DescriptionBitDescription7 0= Unconditional1=Conditional6 0=CC 1=CS0..5 Opcode 21 8 bit offset20


Program Flow Instructions• CALL32 [EX] [a] [@] R1 [Imm32]• CALL64 [EX] [a] Imm6421


Program Flow Instructions• CALLByteDescription0BitDescription7 1=Immediate data present6 0=Call32 1 = Call640..5 Opcode 31BitDescription6-7 Reserved, must be 05 0= Call to <strong>EBC</strong> code 1= Call to Native code4 0= Absolute address 1= Relative address3 0 = Operand1 direct 1= Operand1 indirect0..2 Operand12..3 Optional 32 bit data2..7 Optional 64 bit immediate data22


Program Flow Instructions• RETByte0DescriptionBitDescription7..4 Reserved, must be 00..5 Opcode=41 Reserved23


CompareOpcodecc5/45 eq6/46 lte7/47 gte8/48 ulte9/49 ugteDescriptionCompare Signed Equal/Not EqualCompare Signed Less Than or Equal/GreaterThanCompare Signed Greater Than or Equal/LessThanCompare Unsigned Less Than or Equal/GreaterThanCompare Unsigned Greater Than or Equal/LessThan24


Compare• CMP[32|64]cc R1,[@]R2[Imm16]Byte0DescriptionBitDescription7 0= No immediate data1=Immediate data present6 0=32 bit operand width1=64 bit operand width0..5 CMP opcodes1 Operand2..3 Optional 16 bit immediate data25


Compare• CMPI[32|64]cc R1,[Imm16|32]Byte0DescriptionBitDescription7 0= 16 bit immediate data1= 32 bit Immediate data6 0=32 bit operand width1=64 bit operand width0..5 CMP opcodes1 Operand2..3 16 bit immediate data2..5 32 bit immediate data26


Data ManipulationOpcodeDescription10 NOT[64|32] R1,[@]R2[Imm16]11 NEG[64|32] R1,[@]R2[Imm16]12 ADD[64|32] R1,[@]R2[Imm16]13 SUB[64|32] R1,[@]R2[Imm16]14 MUL[64|32] R1,[@]R2[Imm16]15 MULU[64|32] R1,[@]R2[Imm16]16 DIV [64|32] R1,[@]R2[Imm16]17 DIVU [64|32] R1,[@]R2[Imm16]18 MOD [64|32] R1,[@]R2[Imm16]19 MODU [64|32] R1,[@]R2[Imm16]20 AND [64|32] R1,[@]R2[Imm16]21 OR [64|32] R1,[@]R2[Imm16]R1 = NOT R2R1 = NEG R2R1 = R1+R2R1 = R1-R2R2R1 = R1*R2R1 = R1*R2R1 = R1/R2R1 = R1/R2R1 = R1 mod R2R1 = R1 mod R2R1 = R1 and R2R1 = R1 or R227


Data ManipulationOpcodeDescription22 XOR[64|32] R1,[@]R2[Imm16]23 SHL[64|32] R1,[@]R2[Imm16]24 SHR[64|32] R1,[@]R2[Imm16]25 ASHR[64|32] R1,[@]R2[Imm16]R1 = R1 xor R2R1 = R1 shl R2R1 = R1 shr R2R1 = R1 ashr R226 EXTNDB[64|32] R1,[@]R2[Imm16] Extract Byte R2, sign extend to 64bits and store back in R127 EXTNDW[64|32] R1,[@]R2[Imm16]28 EXTNDD [64|32] R1,[@]R2[Imm16]Extract Word R2, sign extend to64 bits and store back in R1Extract Dword R2, sign extend to64 bits and store back in R128


Data ManipulationByteDescription0BitDescription7 0= No immediate data1= Immediate data present6 0=32 bit operand width1=64 bit operand width0..5 Data Manipulation opcodes1 Operand2..3 Optional 16 bit immediate data29


Data Movement• MOV[s][n][B|W|D|Q][W|D|Q] ] [@]R1,[@]R2[Imm16|32|64]• MOV[s][n][B|W|D|Q][W|D|Q] ] [@]R1[Imm16|32|64],[@]R2• MOVI[B|W|D|Q][W|D|Q] [@]R1[Imm16],Imm16|32|64• MOVIn[W|D|Q] ] [@]R1[Imm16],Imm16|32|64• MOVREL{W|D|Q] [@]R1[Imm16],Imm16|32|6430


Data MovementOpcodeDescription29 MOVbw [@]R1[Imm16],[@]R2[Imm16]30 MOVww [@]R1[Imm16],[@]R2[Imm16]31 MOVdw [@]R1[Imm16],[@]R2[Imm16]32 MOVqw [@]R1[Imm16,[@]R2[Imm16]33 MOVbd [@]R1[Imm32],[@]R2[Imm32]34 MOVwd [@]R1 [Imm32],[@]R2[Imm32]35 MOVdd [@]R1 [Imm32],[@]R2[Imm32]36 MOVqd [@]R1 [Imm32],[@]R2[Imm32]31


Data MovementOpcodeDescription37 MOVsnw [@]R1[Imm16],[@]R2[Imm16]38 MOVsnd [@]R1[Imm32],[@]R2[Imm32]40 MOVqq [@]R1[Imm64],[@]R2[Imm64]50 MOVnw [@]R1[Imm16],[@]R2[Imm16]51 MOVnd [@]R1[Imm32,[@]R2[Imm32]32


Data MovementMOV/MOVnMOVnByteDescription0BitDescription7 0= Operand 1 index absent1= Operand 1 index present6 0= Operand 2 index absent1= Operand 2 index present0..5 Mov opcodes1 Operand2..3 Optional 16 bit immediate data2..5 Optional 32 bit immediate data2..9 Optional 64 bit immediate data33


Data MovementByteMOVI/MOVInMOVInDescription0BitDescription6..7 0 = Reserved, 1=16bits, 2=32 bits 3=64bits0..5 Opcode 55=MOVI 56=MOVIn1BitDescription7 Reserved, must be 06 0 = Optional immediate absent,3 0=Operand 1 direct, 1= Operand 1 indirect0..2 Operand 11= Optional immediate present4..5 0=8bit move, 1=16bit mov, , 2=32bit move3=64bit move2..3 16 bit immediate index (Optional)4..5 16 bit immediate data4..7 32 bit immediate data4..11 64 bit immediate data34


Data MovementPUSH/POPOpcodeDescription43 PUSH [@] R1 [Imm16]44 POP [@] R1 [Imm16]53 PUSHn [@] R1 [Imm16]54 POPn [@] R1 [Imm16]35


Data MovementByteDescription0BitDescription7 0= No immediate data1= Immediate data present6 0=32 bit operand width1=64 bit operand width0..5 Push or Pop Opcodes36


Special Register MoveLOADSP SP1,R2Byte01DescriptionBitDescription6..7 Reserved, must be 00..5 Opcode =41BitDescription7 Reserved, must be 04..6 Operand-2. General purposeregister3 Reserved, must be 00..2 Operand1. Special purpose register37


Special Register MoveSTORESP R1,SP1Byte0DescriptionBitDescription6..7 Reserved, must be 00..5 Opcode =421BitDescription7 Reserved, must be 04..6 Operand1. Special purpose register3 Reserved, must be 00..2 Operand2. General purpose register38


Development Environment• Windows* 32– Intel® C Compiler for EFI Byte Code, Version 1.2 Build20040123– Use "C:\Program Files\IntelIntel\<strong>EBC</strong>\bin\iecvars.bat" " to launch<strong>EBC</strong> compilation and linking environment• Linker– Microsoft* linker Version 7.10.3077 and above*Other names and brands may be claimed as the property of others.39


Calling convention• CDECL only– R0: Stack Pointer, R7: return value– Like IA32.– All parameters are passed through stack including 8-byte 8structure/longlong.– Return value is passed through stack if larger than 8-byte8• Between <strong>EBC</strong> and native– <strong>EBC</strong> <strong>EBC</strong>– Call/Ret– May imply <strong>EBC</strong> native<strong>EBC</strong>– <strong>EBC</strong> native code– CallEx, , VM to handle calling convention– Native code <strong>EBC</strong>– Break 5, to create thunk for address taken <strong>EBC</strong> functions when <strong>EBC</strong> imageloading– One level indirect assignment for <strong>EBC</strong> function pointer due to the e existenceof thunking entry40


Object/Image format• Object Format– COFF• Executable Format– MS PE32 format• Segments– TEXT, DATA, BSS– .CRT$xxx– _VARBSS_INIT41


Language Examples(1)main(){}int *xp,, x, y;y = 8;xp = &y;x = *xp*xp;MOVqw R0, R0(+0,-16)MOVIdw @R0(+0,+8), +8MOVnw @R0, R0(+0,+8)MOVnw R7, @R0MOVdw @R0(+0, +12), @R7MOVqd R7, R6MOVqw R0, R0(+0,+16)RET42


Language Examples(2)int PASS;long f(long x){return x+sizeof(int);}main(){long x, y;x=4;y = f(x);if ((y+f(xy+f(x))==16) PASS=1;else PASS=0;}f:MOVsnwMOVIqw R4, 4ADD R7, R4RETR7, @R0(+0,+16)main:$B2$5:$B2$6:MOVqwR0, R0(+0,-48)MOVInw @R0(0,8), (0,4)MOVsnwCALL fMOVnwMOVsnwMOVsnwCALL fMOVnwMOVsnwMOVnwADD R7, R4MOVsnw@R0, @R0(+0,+8)@R0(+0,+24), R7@R0(+0,+16), @R0(+0,+24)@R0, @R0(+0,+8)@R0(+0,+32), R7R7, @R0(+0,+16)R4, @R0(+0,+32)R7, R7CMPIeq R7, 16JMP8cc $B2$5MOVreldR7, PASSMOVIdw @R7, +1JMP8 $B2$6MOVreldMOVdwMOVqdMOVqwRETR7, PASS@R7, R6R7, R6R0, R0(+0,+48)43


Demo

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!