FoxAMD64Assembler.Mod 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709
  1. MODULE FoxAMD64Assembler; (** AUTHOR "fn & fof"; PURPOSE "Oberon Compiler:AMD 64 Assembler"; **)
  2. (* (c) fof ETH Zürich, 2008 *)
  3. (*
  4. this module has in great portions been taken over from Florian Negele's PCAAMD64.Mod
  5. *)
  6. IMPORT
  7. Basic := FoxBasic, Scanner := FoxScanner, SyntaxTree := FoxSyntaxTree, Global := FoxGlobal, InstructionSet := FoxAMD64InstructionSet, Sections := FoxSections,
  8. BinaryCode := FoxBinaryCode, SYSTEM, Streams, Strings, Commands, KernelLog, Diagnostics, IntermediateCode := FoxIntermediateCode, ObjectFile
  9. ;
  10. CONST
  11. Trace= FALSE;
  12. none* = InstructionSet.none;
  13. (* rex prefix bit positions *)
  14. rexB = 0;
  15. rexX = 1;
  16. rexR = 2;
  17. rexW= 3;
  18. rex = 4;
  19. (* register indices, the numbers have a meaning in instruction encoding, do not modify *)
  20. RAX* = 0; EAX*=0; AX*=0; AL*=0;
  21. RCX* = 1; ECX*=1; CX*=1; CL*=1;
  22. RDX* = 2;EDX*=2; DX*=2; DL*=2;
  23. RBX* = 3;EBX*=3; BX*=3; BL*=3;
  24. RSP* = 4; ESP*=4; SP*=5; SPL*=4; AH*=4;
  25. RBP* = 5; EBP*=5; BP*=5; BPL*=5; CH*=5;
  26. RSI* = 6; ESI*=6; SI*=6; SIL*=6; DH*=6;
  27. RDI* = 7;EDI*=7; DI*=7; DIL*=7; BH*=7;
  28. R8*= 8; R8D*=8; R8W*=8; R8B*=8;
  29. R9* = 9;R9D*=9; R9W*=9; R9B*=9;
  30. R10* = 10;R10D*=10; R10W*=10; R10B*=10;
  31. R11* = 11;R11D*=11; R11W*=11; R11B*=11;
  32. R12* = 12;R12D*=12; R12W*=12; R12B*=12;
  33. R13* = 13;R13D*=13; R13W*=13; R13B*=13;
  34. R14* = 14;R14D*=14; R14W*=14; R14B*=14;
  35. R15* = 15;R15D*=15; R15W*=15; R15B*=15;
  36. RIP* = 16;
  37. (* segment registers *)
  38. segES = 0;
  39. segCS = 1;
  40. segSS = 2;
  41. segDS = 3;
  42. segFS = 4;
  43. segGS = 5;
  44. (* sizes *)
  45. bitsDefault* = 0;
  46. bits8* = 1;
  47. bits16* = 2;
  48. bits32* = 4;
  49. bits64* = 8;
  50. bits128* = 16;
  51. (** constants from InstructionSet **)
  52. (* instruction encoding *)
  53. opCode = InstructionSet.opCode;
  54. modRMExtension= InstructionSet.modRMExtension; modRMBoth= InstructionSet.modRMBoth;
  55. cb= InstructionSet.cb; cw= InstructionSet.cw; cd= InstructionSet.cd; cp= InstructionSet.cp;
  56. ib= InstructionSet.ib; iw= InstructionSet.iw; id= InstructionSet.id; iq= InstructionSet.iq;
  57. rb= InstructionSet.rb; rw= InstructionSet.rw; rd= InstructionSet.rd; rq= InstructionSet.rq;
  58. mem64Operand= InstructionSet.mem64Operand; mem128Operand= InstructionSet.mem128Operand;
  59. fpStackOperand= InstructionSet.fpStackOperand; directMemoryOffset= InstructionSet.directMemoryOffset;
  60. (* limits *)
  61. maxNumberOperands = InstructionSet.maxNumberOperands;
  62. (* operand types, values have no meaning but do coincide with symbols in the instruction set module *)
  63. reg8*= InstructionSet.reg8;
  64. reg16*= InstructionSet.reg16;
  65. reg32*= InstructionSet.reg32;
  66. reg64*= InstructionSet.reg64;
  67. CRn*= InstructionSet.CRn;
  68. DRn*= InstructionSet.DRn;
  69. segReg*= InstructionSet.segReg;
  70. mmx*= InstructionSet.mmx;
  71. xmm*= InstructionSet.xmm;
  72. mem*=InstructionSet.mem;
  73. sti*= InstructionSet.sti;
  74. imm *= InstructionSet.imm;
  75. ioffset *=InstructionSet.ioffset;
  76. pntr1616*= InstructionSet.pntr1616;
  77. pntr1632*=InstructionSet.pntr1632;
  78. (* scanner codes *)
  79. TAB = 09X;
  80. LF = 0AX;
  81. CR = 0DX;
  82. SPACE = 20X;
  83. (* symbol values *)
  84. symNone = 0;
  85. symIdent = 1;
  86. symLabel = 2;
  87. symNumber = 3;
  88. symSemicolon = 4;
  89. symColon = 5;
  90. symLn = 6;
  91. symComma = 7;
  92. symString = 8;
  93. symPlus = 9;
  94. symMinus = 10;
  95. symTimes = 11;
  96. symDiv = 12;
  97. symLParen = 13;
  98. symRParen = 14;
  99. symLBrace = 15;
  100. symRBrace = 16;
  101. symLBraket = 17;
  102. symRBraket = 18;
  103. symPC = 19;
  104. symPCOffset = 20;
  105. symNegate = 21;
  106. symMod = 22;
  107. symPeriod = 23;
  108. symAt = 24;
  109. symEnd = 25;
  110. TYPE
  111. Name = Scanner.IdentifierString;
  112. Size = SHORTINT;
  113. Register* = LONGINT; (* index for InstructionSet.registers *)
  114. (*
  115. an implementation of Operands as objects is very elegant but unfortunately also very costly in terms of number of allocations
  116. *)
  117. Operand* = RECORD
  118. type-: SHORTINT; (* reg8..reg64, CRn,DRn, segReg, sti, mmx, xmm, mem, imm, moffset, pntr1616, pntr1632 *)
  119. (* assembler examples:
  120. reg8: AL => register = InstructionSet.regAL
  121. reg16: CX => register = InstructionSet.regCX
  122. reg32: EBX => register = InstructionSet.regEBX
  123. reg64: RCX => register = InstructionSet.regRCX
  124. mem: BYTE [EAX+EBX*4+16] => register = EAX, index = EBX, scale = 4, displacement = 16, size = 8
  125. imm: DWORD 256 => val = 256, size = 32
  126. *)
  127. register-: Register; (* for registers and mem *)
  128. sizeInBytes-: Size; (* for mem and imm and moffset *)
  129. segment-,index-: Register; (* registers for mem *)
  130. scale-, displacement-: LONGINT; (* for mem *)
  131. symbol- : ObjectFile.Identifier; (* for imm and mem *)
  132. symbolOffset-: LONGINT; (* offset in immediate code (source) for a fixup *)
  133. val-: HUGEINT; (* for imm and moffset *)
  134. pc-: LONGINT;
  135. selector-, offset-: LONGINT; (* for pntr1616 / pntr1632 *)
  136. END;
  137. Code* = BinaryCode.Section;
  138. NamedLabel*= OBJECT
  139. VAR
  140. offset: LONGINT;
  141. name-: SyntaxTree.IdentifierString;
  142. nextNamedLabel-: NamedLabel;
  143. index-: LONGINT;
  144. PROCEDURE &InitNamedLabel(offset: LONGINT; CONST name: ARRAY OF CHAR);
  145. BEGIN
  146. SELF.offset := offset;
  147. COPY(name,SELF.name);
  148. nextNamedLabel := NIL;
  149. END InitNamedLabel;
  150. PROCEDURE SetOffset*(ofs: LONGINT);
  151. BEGIN SELF.offset := ofs;
  152. END SetOffset;
  153. END NamedLabel;
  154. NamedLabelList*=OBJECT
  155. VAR first-,last-: NamedLabel; number-: LONGINT;
  156. PROCEDURE & InitNamedLabelList;
  157. BEGIN first := NIL; last := NIL; number := 0;
  158. END InitNamedLabelList;
  159. PROCEDURE Add*(n: NamedLabel);
  160. BEGIN
  161. IF first = NIL THEN first := n ELSE last.nextNamedLabel := n; last.nextNamedLabel := n; END; last := n; INC(number);
  162. n.index := number;
  163. END Add;
  164. PROCEDURE Find*(CONST name: ARRAY OF CHAR): NamedLabel;
  165. VAR label: NamedLabel;
  166. BEGIN
  167. label := first;
  168. WHILE (label # NIL) & (label.name # name) DO
  169. label := label.nextNamedLabel;
  170. END;
  171. RETURN label
  172. END Find;
  173. END NamedLabelList;
  174. Emitter*=OBJECT
  175. VAR
  176. code-: Code;
  177. error-: BOOLEAN;
  178. diagnostics: Diagnostics.Diagnostics;
  179. (* overal state *)
  180. cpuBits: Size; (* supported bit width for this cpu / target *)
  181. cpuOptions: InstructionSet.CPUOptions;
  182. dump: Streams.Writer;
  183. PROCEDURE & InitEmitter*(diagnostics: Diagnostics.Diagnostics);
  184. BEGIN
  185. SELF.diagnostics := diagnostics;
  186. cpuBits := bits32; cpuOptions := {0..31};
  187. error := FALSE;
  188. END InitEmitter;
  189. PROCEDURE SetCode*(code: BinaryCode.Section);
  190. BEGIN SELF.code := code;
  191. dump := code.comments
  192. END SetCode;
  193. PROCEDURE SetBits* (numberBits: LONGINT): BOOLEAN;
  194. BEGIN
  195. CASE numberBits OF
  196. 16: cpuBits := bits16;
  197. | 32: cpuBits := bits32;
  198. | 64: cpuBits := bits64;
  199. ELSE
  200. Error("number bits not supported");
  201. RETURN FALSE;
  202. END;
  203. RETURN TRUE;
  204. END SetBits;
  205. PROCEDURE Error(CONST message: ARRAY OF CHAR);
  206. VAR msg,name: ARRAY 256 OF CHAR;
  207. BEGIN
  208. COPY(message,msg);
  209. Strings.Append(msg," in ");
  210. ObjectFile.SegmentedNameToString(code.os.identifier.name,name);
  211. Strings.Append(msg, name);
  212. IF diagnostics # NIL THEN
  213. diagnostics.Error("",Diagnostics.Invalid,Diagnostics.Invalid,msg);
  214. END;
  215. error := TRUE;
  216. IF dump # NIL THEN dump.Update; END;
  217. END Error;
  218. PROCEDURE ErrorSS(CONST msg1,msg2: ARRAY OF CHAR);
  219. VAR message: ARRAY 256 OF CHAR;
  220. BEGIN
  221. COPY(msg1,message);
  222. Strings.Append(message," : ");
  223. Strings.Append(message, msg2);
  224. Error(message);
  225. END ErrorSS;
  226. PROCEDURE ErrorSI(CONST msg1: ARRAY OF CHAR; mnemonic: LONGINT; CONST operands: ARRAY OF Operand);
  227. VAR s: Streams.StringWriter; msg: Basic.MessageString;
  228. BEGIN
  229. NEW(s,LEN(msg));
  230. DumpInstruction(s,mnemonic,operands);
  231. s.String(" @");
  232. s.Int(code.pc,1);
  233. s.Get(msg);
  234. ErrorSS(msg1,msg);
  235. END ErrorSI;
  236. PROCEDURE AddFixup (mode: SHORTINT; size: SHORTINT; pc: LONGINT; symbol: ObjectFile.Identifier; symbolOffset, displacement: LONGINT);
  237. VAR fixup: BinaryCode.Fixup; format: BinaryCode.FixupPatterns; id: ObjectFile.Identifier;
  238. BEGIN
  239. NEW(format,1);
  240. format[0].bits:= size*8;
  241. format[0].offset := 0;
  242. fixup := BinaryCode.NewFixup(mode,pc,symbol,symbolOffset,displacement,0,format);
  243. code.fixupList.AddFixup(fixup);
  244. END AddFixup;
  245. PROCEDURE EmitInstruction (mnem: LONGINT; VAR operands: ARRAY OF Operand; lastPass: BOOLEAN): BOOLEAN;
  246. VAR instr, i, oppos, op: LONGINT;
  247. val: LONGINT;
  248. regOperand: LONGINT;
  249. addressOperand: LONGINT;
  250. regField, modField, rmField: LONGINT;
  251. scaleField, indexField, baseField: LONGINT;
  252. free: ARRAY maxNumberOperands OF BOOLEAN;
  253. byte: LONGINT;
  254. offset: LONGINT;
  255. opPrefix, adrPrefix: BOOLEAN;
  256. segPrefix: LONGINT; rexPrefix: SET;
  257. bitwidthOptions: SET;
  258. opcode: ARRAY InstructionSet.maxCodeLength OF InstructionSet.Code;
  259. pc0: LONGINT;
  260. debug,temp: LONGINT;
  261. PROCEDURE FindInstruction(mnem: LONGINT; CONST operands: ARRAY OF Operand): LONGINT;
  262. VAR instr: LONGINT;
  263. PROCEDURE MatchesInstruction (): BOOLEAN;
  264. VAR i: LONGINT;
  265. BEGIN
  266. FOR i := 0 TO maxNumberOperands - 1 DO
  267. IF (i>=LEN(operands)) OR (operands[i].type = none) THEN (* no operand -> check if instruction has no operand here *)
  268. IF InstructionSet.instructions[instr].operands[i] # none THEN
  269. RETURN FALSE END;
  270. ELSIF ~Matches(operands[i],InstructionSet.instructions[instr].operands[i]) THEN (* instruction operand type and this operand do not match *)
  271. RETURN FALSE
  272. ELSIF (cpuBits = bits64) & (InstructionSet.optNot64 IN InstructionSet.instructions[instr].bitwidthOptions) THEN (* instruction is invalid in 64 bit mode *)
  273. RETURN FALSE;
  274. END;
  275. END;
  276. RETURN TRUE;
  277. END MatchesInstruction;
  278. BEGIN
  279. instr := InstructionSet.mnemonics[mnem].firstInstruction;
  280. WHILE (instr <= InstructionSet.mnemonics[mnem].lastInstruction) & (~MatchesInstruction ()) DO INC (instr); END;
  281. IF instr > InstructionSet.mnemonics[mnem].lastInstruction THEN
  282. ErrorSI("invalid combination of opcode and operands", mnem,operands); RETURN none;
  283. ELSIF InstructionSet.instructions[instr].cpuOptions * cpuOptions # InstructionSet.instructions[instr].cpuOptions THEN
  284. ErrorSI("invalid instruction for current target", mnem,operands); RETURN none;
  285. END;
  286. RETURN instr
  287. END FindInstruction;
  288. PROCEDURE GetRegOperand (): LONGINT;
  289. VAR i: LONGINT;
  290. BEGIN
  291. FOR i := 0 TO maxNumberOperands -1 DO
  292. CASE InstructionSet.instructions[instr].operands[i] OF
  293. InstructionSet.reg8, InstructionSet.reg16, InstructionSet.reg32, InstructionSet.reg64, InstructionSet.xmm, InstructionSet.mmx: RETURN i;
  294. ELSE
  295. END;
  296. END;
  297. RETURN none;
  298. END GetRegOperand;
  299. PROCEDURE GetAddressOperand (): LONGINT;
  300. VAR i: LONGINT;
  301. BEGIN
  302. FOR i := 0 TO maxNumberOperands -1 DO
  303. CASE InstructionSet.instructions[instr].operands[i] OF
  304. InstructionSet.mem,
  305. InstructionSet.mem8, InstructionSet.mem16, InstructionSet.mem32, InstructionSet.mem64, InstructionSet.mem128,
  306. InstructionSet.regmem8, InstructionSet.regmem16, InstructionSet.regmem32, InstructionSet.regmem64,
  307. InstructionSet.mmxmem32, InstructionSet.mmxmem64,
  308. InstructionSet.xmmmem32, InstructionSet.xmmmem64, InstructionSet.xmmmem128:
  309. RETURN i;
  310. ELSE
  311. END;
  312. END;
  313. RETURN none;
  314. END GetAddressOperand;
  315. PROCEDURE GetSpecialOperand (): LONGINT;
  316. VAR i: LONGINT;
  317. BEGIN
  318. FOR i := 0 TO maxNumberOperands -1 DO
  319. CASE InstructionSet.instructions[instr].operands[i] OF
  320. InstructionSet.segReg, InstructionSet.mmx, InstructionSet.xmm, InstructionSet.CRn, InstructionSet.DRn:
  321. RETURN i;
  322. ELSE
  323. END;
  324. END;
  325. RETURN none;
  326. END GetSpecialOperand;
  327. PROCEDURE ModRM (mod, reg, rm: LONGINT);
  328. BEGIN
  329. IF Trace THEN KernelLog.String("ModRM"); KernelLog.Int(mod,1); KernelLog.String(","); KernelLog.Int(reg,1);
  330. KernelLog.String(","); KernelLog.Int(rm,1); KernelLog.Ln;
  331. END;
  332. code.PutByte (mod MOD 4 * 40H + reg MOD 8 * 8H + rm MOD 8);
  333. END ModRM;
  334. PROCEDURE SIB (scale, index, base: LONGINT);
  335. BEGIN code.PutByte (scale MOD 4 * 40H + index MOD 8 * 8H + base MOD 8);
  336. END SIB;
  337. PROCEDURE FPOrSSEOperation(instr: LONGINT): BOOLEAN;
  338. BEGIN
  339. RETURN {InstructionSet.cpuFPU, InstructionSet.cpuSSE, InstructionSet.cpuSSE2, InstructionSet.cpuSSE3} * InstructionSet.instructions[instr].cpuOptions # {}
  340. END FPOrSSEOperation;
  341. BEGIN
  342. IF (dump # NIL) & (lastPass) THEN
  343. pc0 := code.pc;
  344. DumpInstruction(dump,mnem,operands);
  345. dump.Update;
  346. END;
  347. IF Trace THEN
  348. DumpInstruction(kernelWriter,mnem,operands);
  349. kernelWriter.Update;
  350. END;
  351. instr := FindInstruction(mnem,operands);
  352. IF instr = none THEN RETURN FALSE END;
  353. bitwidthOptions := InstructionSet.instructions[instr].bitwidthOptions;
  354. FOR i := 0 TO InstructionSet.maxCodeLength-1 DO opcode[i] := InstructionSet.instructions[instr].code[i] END;
  355. opPrefix := FALSE;
  356. adrPrefix := FALSE;
  357. segPrefix := none;
  358. rexPrefix := {};
  359. IF (InstructionSet.optO16 IN bitwidthOptions) & (cpuBits # bits16) THEN
  360. IF Trace THEN KernelLog.String(" optO16 "); KernelLog.Ln; END;
  361. opPrefix := TRUE;
  362. END;
  363. IF (InstructionSet.optO32 IN bitwidthOptions) & (cpuBits = bits16) THEN
  364. IF Trace THEN KernelLog.String(" optO32 "); KernelLog.Ln; END;
  365. opPrefix := TRUE;
  366. END;
  367. IF (InstructionSet.optO64 IN bitwidthOptions) & (cpuBits = bits64) THEN
  368. IF Trace THEN KernelLog.String(" optO64 "); KernelLog.Ln; END;
  369. INCL (rexPrefix, rexW)
  370. END;
  371. IF InstructionSet.optPOP IN bitwidthOptions THEN
  372. IF Trace THEN KernelLog.String(" optPOP "); KernelLog.Ln; END;
  373. opPrefix := TRUE;
  374. END;
  375. regOperand := GetSpecialOperand ();
  376. addressOperand := GetAddressOperand ();
  377. IF regOperand = none THEN
  378. regOperand := GetRegOperand ();
  379. END;
  380. IF addressOperand = none THEN
  381. addressOperand := GetRegOperand ();
  382. IF regOperand # none THEN
  383. temp := InstructionSet.instructions[instr].operands[regOperand];
  384. IF (temp = xmm) OR (temp = mmx) THEN (* patch case such as PEXTRW EDX, XMM3, 0 *)
  385. temp := addressOperand; addressOperand := regOperand; regOperand := temp;
  386. END;
  387. ELSE
  388. END;
  389. END;
  390. IF mnem = InstructionSet.opMOVQ2DQ THEN (* patch *)
  391. regOperand := 0; addressOperand :=1;
  392. END;
  393. (* KernelLog.String (InstructionSet.mnemonics[mnem].name); KernelLog.Int (regOperand, 10); KernelLog.Int (addressOperand, 10); KernelLog.Ln; *)
  394. FOR i := 0 TO maxNumberOperands - 1 DO
  395. IF operands[i].type # none THEN
  396. IF operands[i].type = mem THEN
  397. IF Trace THEN KernelLog.String("mem"); KernelLog.Ln; END;
  398. IF operands[i].segment# none THEN
  399. IF Trace THEN KernelLog.String(" segment "); KernelLog.Ln; END;
  400. segPrefix := InstructionSet.RegisterIndex(operands[i].segment);
  401. END;
  402. IF operands[i].register# none THEN
  403. IF Trace THEN KernelLog.String(" register "); KernelLog.Int(operands[i].register,1); KernelLog.Ln; END;
  404. IF (InstructionSet.RegisterIndex(operands[i].register) >= 8) THEN
  405. IF Trace THEN KernelLog.String(" rexprefix "); KernelLog.Ln; END;
  406. INCL (rexPrefix, rexB)
  407. END;
  408. IF (InstructionSet.RegisterType(operands[i].register) = reg32) & (cpuBits # bits32) THEN
  409. IF Trace THEN KernelLog.String(" adr prefix "); KernelLog.Ln; END;
  410. adrPrefix := TRUE;
  411. END;
  412. IF InstructionSet.RegisterType(operands[i].register)=reg16 THEN
  413. IF cpuBits = bits64 THEN
  414. ErrorSI("invalid effective address (1)", mnem,operands);
  415. RETURN FALSE;
  416. ELSIF cpuBits = bits32 THEN
  417. IF Trace THEN KernelLog.String(" adr prefix (2) "); KernelLog.Ln; END;
  418. adrPrefix := TRUE;
  419. END;
  420. END;
  421. END;
  422. IF operands[i].index # none THEN
  423. IF Trace THEN KernelLog.String(" mem index "); KernelLog.Int(operands[i].index,1); KernelLog.Ln; END;
  424. IF (InstructionSet.RegisterType(operands[i].index)=reg64) & (InstructionSet.RegisterIndex(operands[i].index) >= 8) THEN
  425. INCL (rexPrefix, rexX)
  426. END
  427. END;
  428. IF (operands[i].sizeInBytes = bits64) & ~(InstructionSet.optD64 IN bitwidthOptions) & ~FPOrSSEOperation(instr) THEN
  429. IF Trace THEN KernelLog.String(" bits64 "); KernelLog.Ln; END;
  430. INCL (rexPrefix, rexW)
  431. END;
  432. IF InstructionSet.instructions[instr].operands[i] = InstructionSet.moffset64 THEN
  433. IF Trace THEN KernelLog.String(" moffset64 "); KernelLog.Ln; END;
  434. adrPrefix := TRUE;
  435. END;
  436. ELSIF IsRegisterOperand(operands[i]) (* is register *) THEN
  437. IF Trace THEN KernelLog.String("register"); KernelLog.Ln; END;
  438. IF (operands[i].type = reg64) & ~(InstructionSet.optD64 IN bitwidthOptions) THEN
  439. IF Trace THEN KernelLog.String(" reg64 "); KernelLog.Ln; END;
  440. INCL (rexPrefix, rexW)
  441. END;
  442. IF InstructionSet.RegisterIndex(operands[i].register) >= 8 THEN
  443. IF i = addressOperand THEN
  444. INCL (rexPrefix, rexB)
  445. ELSIF i = regOperand THEN
  446. INCL (rexPrefix, rexR)
  447. END;
  448. ELSIF (cpuBits = bits64) & (operands[i].type = reg8) & (InstructionSet.RegisterIndex(operands[i].register) >= 4) THEN
  449. INCL (rexPrefix, rex);
  450. END;
  451. END;
  452. END;
  453. free[i] := operands[i].type # none;
  454. END;
  455. CASE segPrefix OF
  456. none:
  457. | segES: code.PutByte (InstructionSet.prfES);
  458. | segCS: code.PutByte (InstructionSet.prfCS);
  459. | segSS: code.PutByte (InstructionSet.prfSS);
  460. | segDS: code.PutByte (InstructionSet.prfDS);
  461. | segFS: code.PutByte (InstructionSet.prfFS);
  462. | segGS: code.PutByte (InstructionSet.prfGS);
  463. END;
  464. IF opPrefix THEN code.PutByte (InstructionSet.prfOP) END;
  465. IF adrPrefix THEN code.PutByte (InstructionSet.prfADR) END;
  466. IF InstructionSet.optPLOCK IN bitwidthOptions THEN code.PutByte (InstructionSet.prfLOCK) END;
  467. IF InstructionSet.optPREP IN bitwidthOptions THEN code.PutByte (InstructionSet.prfREP) END;
  468. IF InstructionSet.optPREPN IN bitwidthOptions THEN code.PutByte (InstructionSet.prfREPNE) END;
  469. IF rexPrefix # {} THEN
  470. ASSERT(cpuBits = bits64);
  471. byte := 40H;
  472. IF rexB IN rexPrefix THEN byte := byte + 1H END;
  473. IF rexX IN rexPrefix THEN byte := byte + 2H END;
  474. IF rexR IN rexPrefix THEN byte := byte + 4H END;
  475. IF rexW IN rexPrefix THEN byte := byte + 8H END;
  476. code.PutByte (byte);
  477. END;
  478. op := 0;
  479. oppos := 0;
  480. val := -1;
  481. WHILE (oppos < LEN(opcode)) & (opcode[oppos] # CHR(none)) DO
  482. IF opcode[oppos] = CHR(opCode) THEN
  483. IF Trace THEN KernelLog.String("opcode "); KernelLog.Hex(ORD(opcode[oppos+1]),-2); END;
  484. IF val # -1 THEN code.PutByte (val) END;
  485. INC(oppos);
  486. val := ORD(opcode[oppos]);
  487. ELSE
  488. CASE ORD(opcode[oppos]) OF
  489. | modRMExtension, modRMBoth:
  490. IF Trace THEN KernelLog.String(" modRMExtension/Both "); END;
  491. IF val # -1 THEN code.PutByte (val); val := -1 END;
  492. IF opcode[oppos] = CHR(modRMBoth) (* /r *) THEN
  493. regField := InstructionSet.RegisterIndex(operands[regOperand].register) MOD 8;
  494. ELSE (* /digit *)
  495. INC(oppos);
  496. regField := ORD(opcode[oppos]);
  497. IF Trace THEN KernelLog.String(" digit: "); KernelLog.Int(regField,1); KernelLog.Ln; END;
  498. END;
  499. IF IsRegisterOperand(operands[addressOperand]) THEN
  500. IF Trace THEN KernelLog.String(" isRegisterOperand "); END;
  501. ModRM (3, regField, InstructionSet.RegisterIndex(operands[addressOperand].register) MOD 8);
  502. ELSIF (cpuBits = bits16) & (InstructionSet.RegisterType(operands[addressOperand].register) # reg32) THEN
  503. IF Trace THEN KernelLog.String(" cpuBits=16 "); END;
  504. IF (operands[addressOperand].scale # 1) OR (operands[addressOperand].symbol.name # "") THEN
  505. ErrorSI("invalid effective address (2)", mnem,operands);
  506. RETURN FALSE;
  507. ELSIF operands[addressOperand].register= none THEN
  508. IF operands[addressOperand].index =none THEN
  509. ErrorSI("invalid effective address (3)", mnem,operands);
  510. RETURN FALSE;
  511. END;
  512. ModRM (0, regField, 6);
  513. code.PutWord (operands[addressOperand].displacement);
  514. ELSIF InstructionSet.RegisterType(operands[addressOperand].register) = reg16 THEN
  515. IF operands[addressOperand].displacement = 0 THEN
  516. modField := 0;
  517. ELSIF (operands[addressOperand].displacement >= -80H) & (operands[addressOperand].displacement < 80H) THEN
  518. modField := 1;
  519. ELSIF (operands[addressOperand].displacement >= -8000H) & (operands[addressOperand].displacement < 8000H) THEN
  520. modField := 2;
  521. ELSE
  522. Error("value exceeds bounds");
  523. RETURN FALSE;
  524. END;
  525. CASE InstructionSet.RegisterIndex(operands[addressOperand].register) OF
  526. | RBX:
  527. IF operands[addressOperand].index = none THEN
  528. rmField := 7;
  529. ELSIF InstructionSet.RegisterIndex(operands[addressOperand].index) = RSI THEN
  530. rmField := 0;
  531. ELSIF InstructionSet.RegisterIndex(operands[addressOperand].index) = RDI THEN
  532. rmField := 1;
  533. ELSE
  534. ErrorSI("invalid effective address (4)", mnem,operands); RETURN FALSE;
  535. END
  536. | RBP:
  537. IF operands[addressOperand].index = none THEN
  538. rmField := 6;
  539. IF modField = 0 THEN modField := 1 END;
  540. ELSIF InstructionSet.RegisterIndex(operands[addressOperand].index) = RSI THEN
  541. rmField := 2;
  542. ELSIF InstructionSet.RegisterIndex(operands[addressOperand].index) = RDI THEN
  543. rmField := 3;
  544. ELSE
  545. ErrorSI("invalid effective address (5)", mnem,operands); RETURN FALSE;
  546. END
  547. | RSI:
  548. IF operands[addressOperand].index = none THEN
  549. rmField := 4;
  550. ELSIF InstructionSet.RegisterIndex(operands[addressOperand].index) = RBX THEN
  551. rmField := 0;
  552. ELSIF InstructionSet.RegisterIndex(operands[addressOperand].index) = RBP THEN
  553. rmField := 2;
  554. ELSE
  555. ErrorSI("invalid effective address (6)", mnem,operands); RETURN FALSE;
  556. END;
  557. | RDI:
  558. IF operands[addressOperand].index = none THEN
  559. rmField := 5;
  560. ELSIF InstructionSet.RegisterIndex(operands[addressOperand].index) = RBX THEN
  561. rmField := 1;
  562. ELSIF InstructionSet.RegisterIndex(operands[addressOperand].index) = RBP THEN
  563. rmField := 3;
  564. ELSE
  565. ErrorSI("invalid effective address (7)", mnem,operands); RETURN FALSE;
  566. END;
  567. ELSE
  568. ErrorSI("invalid effective address (8)", mnem,operands); RETURN FALSE;
  569. END;
  570. ModRM (modField, regField, rmField);
  571. IF modField = 1 THEN
  572. code.PutByte (operands[addressOperand].displacement);
  573. ELSIF modField = 2 THEN
  574. code.PutWord (operands[addressOperand].displacement);
  575. END;
  576. END;
  577. ELSE (* cpuBits # 16 *)
  578. ASSERT(operands[addressOperand].type = mem);
  579. IF Trace THEN KernelLog.String(" cpuBits # 16 "); END;
  580. IF (operands[addressOperand].register= none) & (operands[addressOperand].index = none) THEN
  581. IF Trace THEN KernelLog.String(" no register, no index "); END;
  582. IF operands[addressOperand].scale # 1 THEN
  583. ErrorSI("invalid effective address (9)", mnem,operands); RETURN FALSE;
  584. END;
  585. IF cpuBits = bits64 THEN
  586. ModRM (0, regField, 4);
  587. SIB (0, 4, 5);
  588. ELSE
  589. ModRM (0, regField, 5);
  590. END;
  591. (* fixup must be 8bit wide for linker!
  592. IF lastPass & (operands[addressOperand].fixup # NIL) THEN
  593. AddFixup (operands[addressOperand].fixup, pc);
  594. END;
  595. *)
  596. IF lastPass & (operands[addressOperand].symbol.name # "") THEN
  597. AddFixup(BinaryCode.Absolute,4,code.pc,operands[addressOperand].symbol, operands[addressOperand].symbolOffset,operands[addressOperand].displacement)
  598. END;
  599. code.PutDWord (operands[addressOperand].displacement);
  600. ELSE
  601. IF (operands[addressOperand].index # none) THEN
  602. (* index register available: must use SIB memory reference *)
  603. IF Trace THEN KernelLog.String(" index "); END;
  604. IF (InstructionSet.RegisterIndex(operands[addressOperand].index) = RSP) OR (InstructionSet.RegisterIndex(operands[addressOperand].index) = RIP) THEN
  605. ErrorSI("invalid effective address: unsupported stack / instruction pointer index", mnem,operands); RETURN FALSE;
  606. END;
  607. IF (operands[addressOperand].register# none) & (InstructionSet.RegisterIndex(operands[addressOperand].register) = RIP) THEN
  608. ErrorSI("invalid effective address: unsupported instruction base pointer with index", mnem,operands); RETURN FALSE;
  609. END;
  610. CASE operands[addressOperand].scale OF
  611. 1: scaleField := 0;
  612. | 2: scaleField := 1;
  613. | 4: scaleField := 2;
  614. | 8: scaleField := 3;
  615. ELSE
  616. ErrorSI("invalid effective address (12)", mnem,operands); RETURN FALSE;
  617. END;
  618. rmField := 4; (* indicates usage of SIB byte *)
  619. ELSE
  620. (* no index register available *)
  621. IF Trace THEN KernelLog.String(" no index ") END;
  622. IF (operands[addressOperand].scale # 1) THEN
  623. ErrorSI("invalid effective address: scale without index register", mnem,operands); RETURN FALSE;
  624. END;
  625. IF operands[addressOperand].register = none THEN (* no index, no base *)
  626. rmField := 4; (* indicates usage of SIB byte *)
  627. ELSIF InstructionSet.RegisterIndex(operands[addressOperand].register) = RIP THEN
  628. rmField := 5; (* indicates usage of instruction pointer, must be followed by 32 bit displacement, modField must be 0 *)
  629. ELSIF InstructionSet.RegisterIndex(operands[addressOperand].register) MOD 8 = RSP THEN
  630. rmField := 4; (* indicates usage of SIB byte => stack pointer must be referenced in SIB byte *)
  631. ELSE
  632. rmField := InstructionSet.RegisterIndex(operands[addressOperand].register) MOD 8; (* any other register can be encoded via modRM field *)
  633. END;
  634. END;
  635. (* IF operands[addressOperand].fixup # NIL THEN
  636. modField := 2;
  637. mem fixups only for local variables and parameters
  638. *)
  639. IF operands[addressOperand].displacement = 0 THEN
  640. (* no displacement => modRM = 0 except for base pointer, which must be encoded with (zero) displacement *)
  641. IF Trace THEN KernelLog.String(" no displacement "); END;
  642. IF (operands[addressOperand].register # none) & (InstructionSet.RegisterIndex(operands[addressOperand].register) = RBP) THEN
  643. modField := 1;
  644. ELSIF (operands[addressOperand].register # none) & (InstructionSet.RegisterIndex(operands[addressOperand].register) = R13) THEN
  645. modField := 1;
  646. ELSE
  647. modField := 0;
  648. END;
  649. ELSIF (operands[addressOperand].register = none) & (operands[addressOperand].index # none) THEN
  650. modField := 0; (* 32 bit displacement without base register encoded via SIB byte *)
  651. ELSIF (operands[addressOperand].register # none) & (InstructionSet.RegisterIndex(operands[addressOperand].register) = RIP) THEN
  652. (* if there is displacement on RIP, we still have to use the modRM = 0 case *)
  653. IF cpuBits = 64 THEN
  654. modField := 0;
  655. ELSE
  656. Error("invalid effective address: instruction pointer relative addressing only in 64 bit mode")
  657. END;
  658. ELSIF (operands[addressOperand].displacement >= -80H) & (operands[addressOperand].displacement < 80H) THEN
  659. (* 8 bit displacement *)
  660. modField := 1;
  661. ELSE
  662. (* 32 bit displacement *)
  663. modField := 2;
  664. END;
  665. ModRM (modField, regField, rmField);
  666. IF (rmField = 4) THEN (* must emit SIB encoding scale, index and base (operand.register --> base) *)
  667. IF operands[addressOperand].index # none THEN
  668. (* index register present *)
  669. indexField := InstructionSet.RegisterIndex(operands[addressOperand].index) MOD 8;
  670. ELSE
  671. (* no index register *)
  672. indexField := 4;
  673. END;
  674. IF operands[addressOperand].register# none THEN
  675. (* base register present, can also be the base pointer (5) *)
  676. baseField := InstructionSet.RegisterIndex(operands[addressOperand].register) MOD 8;
  677. ELSE
  678. (* no register present *)
  679. debug := operands[addressOperand].register;
  680. ASSERT(modField = 0);
  681. baseField := 5;
  682. END;
  683. SIB (scaleField, indexField, baseField);
  684. END;
  685. IF modField = 0 THEN
  686. IF rmField = 5 THEN
  687. IF lastPass & (operands[addressOperand].symbol.name # "") THEN AddFixup(BinaryCode.Absolute,4,code.pc,operands[addressOperand].symbol,operands[addressOperand].symbolOffset,operands[addressOperand].displacement) END;
  688. code.PutDWord(operands[addressOperand].displacement);
  689. ELSIF (rmField = 4) & (baseField = 5) THEN (* special case: SIB without base register: mandatory displacement *)
  690. IF lastPass & (operands[addressOperand].symbol.name # "") THEN AddFixup(BinaryCode.Absolute,4,code.pc,operands[addressOperand].symbol,operands[addressOperand].symbolOffset,operands[addressOperand].displacement) END;
  691. code.PutDWord(operands[addressOperand].displacement);
  692. END;
  693. ELSIF modField = 1 THEN
  694. IF lastPass & (operands[addressOperand].symbol.name # "") THEN AddFixup(BinaryCode.Absolute,1,code.pc,operands[addressOperand].symbol,operands[addressOperand].symbolOffset,operands[addressOperand].displacement) END;
  695. code.PutByte(operands[addressOperand].displacement);
  696. ELSIF modField = 2 THEN
  697. IF lastPass & (operands[addressOperand].symbol.name # "") THEN AddFixup(BinaryCode.Absolute,4,code.pc,operands[addressOperand].symbol,operands[addressOperand].symbolOffset,operands[addressOperand].displacement) END;
  698. code.PutDWord (operands[addressOperand].displacement);
  699. END;
  700. END;
  701. END;
  702. | cb:
  703. IF Trace THEN KernelLog.String(" cb "); END;
  704. IF val # -1 THEN code.PutByte (val); val := -1 END;
  705. FOR i := 0 TO maxNumberOperands - 1 DO
  706. IF (free[i]) & (operands[i].type = ioffset) THEN
  707. IF Trace THEN KernelLog.String(" ioffset "); END;
  708. offset := SHORT(operands[i].val - code.pc - 1);
  709. IF lastPass & ~ValueInByteRange (offset) THEN
  710. Error( "value exceeds bounds");
  711. RETURN FALSE;
  712. END;
  713. operands[i].pc := code.pc;
  714. code.PutByte (offset);
  715. free[i] := FALSE; i:= maxNumberOperands;
  716. ELSIF (free[i]) & (operands[i].type = imm) THEN
  717. IF Trace THEN KernelLog.String(" imm "); END;
  718. offset := SHORT (operands[i].val);
  719. IF lastPass & ~ValueInByteRange (offset) THEN
  720. Error( "value exceeds bounds");
  721. RETURN FALSE;
  722. END;
  723. operands[i].pc := code.pc;
  724. code.PutByte (offset);
  725. free[i] := FALSE; i:= maxNumberOperands;
  726. END
  727. END;
  728. | cw:
  729. IF Trace THEN KernelLog.String(" cw "); END;
  730. IF val # -1 THEN code.PutByte (val); val := -1 END;
  731. FOR i := 0 TO maxNumberOperands - 1 DO
  732. IF (free[i]) & (InstructionSet.instructions[instr].operands[i] = InstructionSet.rel16off) THEN
  733. offset := SHORT(operands[i].val - code.pc - 2);
  734. IF lastPass & ~ValueInWordRange (offset) THEN
  735. Error( "value exceeds bounds");
  736. END;
  737. operands[i].pc := code.pc;
  738. code.PutWord (offset);
  739. free[i] := FALSE; i:= maxNumberOperands;
  740. ELSIF (free[i]) & InstructionSet.IsImmediate16(InstructionSet.instructions[instr].operands[i]) THEN
  741. offset := SHORT (operands[i].val);
  742. IF lastPass & ~ValueInWordRange (offset) THEN
  743. Error( "value exceeds bounds");
  744. RETURN FALSE;
  745. END;
  746. operands[i].pc := code.pc;
  747. code.PutWord (offset);
  748. free[i] := FALSE; i:= maxNumberOperands;
  749. END
  750. END;
  751. | cd:
  752. IF Trace THEN KernelLog.String(" cd "); END;
  753. IF val # -1 THEN code.PutByte (val); val := -1 END;
  754. FOR i := 0 TO maxNumberOperands - 1 DO
  755. IF (free[i]) & (InstructionSet.instructions[instr].operands[i] = InstructionSet.rel32off) THEN
  756. operands[i].pc := code.pc;
  757. IF lastPass & (operands[i].symbol.name # "") THEN
  758. AddFixup(BinaryCode.Relative,4,code.pc,operands[i].symbol,operands[i].symbolOffset,operands[i].displacement-4);
  759. code.PutDWord(SHORT(operands[i].val));
  760. ELSE
  761. code.PutDWord (SHORT (operands[i].val - code.pc - 4));
  762. END;
  763. free[i] := FALSE; i:= maxNumberOperands;
  764. ELSIF (free[i]) & InstructionSet.IsImmediate32(InstructionSet.instructions[instr].operands[i]) THEN
  765. operands[i].pc := code.pc;
  766. IF lastPass & (operands[i].symbol.name # "") THEN
  767. AddFixup(BinaryCode.Absolute,4,code.pc,operands[i].symbol,operands[i].symbolOffset,operands[i].displacement);
  768. END;
  769. code.PutDWord (SHORT (operands[i].val));
  770. free[i] := FALSE; i:= maxNumberOperands;
  771. END
  772. END;
  773. | cp:
  774. IF Trace THEN KernelLog.String(" cp "); END;
  775. IF val # -1 THEN code.PutByte (val); val := -1 END;
  776. | ib:
  777. IF Trace THEN KernelLog.String(" ib "); END;
  778. IF val # -1 THEN code.PutByte (val); val := -1 END;
  779. FOR i := 0 TO maxNumberOperands - 1 DO
  780. IF (free[i]) & (operands[i].type = imm) OR (operands[i].type = ioffset) THEN
  781. offset := SHORT (operands[i].val);
  782. IF FALSE & lastPass & ~ValueInByteRange (offset) THEN
  783. Error( "value exceeds bounds");
  784. RETURN FALSE;
  785. END;
  786. operands[i].pc := code.pc;
  787. IF lastPass & (operands[i].symbol.name # "") THEN AddFixup(BinaryCode.Absolute,1,code.pc,operands[i].symbol,operands[i].symbolOffset,operands[i].displacement) END;
  788. code.PutByte (SHORT (operands[i].val));
  789. free[i] := FALSE; i:= maxNumberOperands;
  790. END
  791. END;
  792. | iw:
  793. IF Trace THEN KernelLog.String(" iw "); END;
  794. IF val # -1 THEN code.PutByte (val); val := -1 END;
  795. FOR i := 0 TO maxNumberOperands - 1 DO
  796. IF (free[i]) & (operands[i].type = imm) OR (operands[i].type = ioffset) THEN
  797. operands[i].pc := code.pc;
  798. code.PutWord (SHORT (operands[i].val));
  799. free[i] := FALSE; i:= maxNumberOperands;
  800. END
  801. END;
  802. | id:
  803. IF Trace THEN KernelLog.String(" id "); END;
  804. IF val # -1 THEN code.PutByte (val); val := -1 END;
  805. FOR i := 0 TO maxNumberOperands - 1 DO
  806. IF (free[i]) & (InstructionSet.instructions[instr].operands[i] = InstructionSet.rel32off) THEN
  807. operands[i].pc := code.pc;
  808. IF lastPass & (operands[i].symbol.name # "") THEN AddFixup(BinaryCode.Relative,4,code.pc,operands[i].symbol,operands[i].symbolOffset,operands[i].displacement-4) END;
  809. code.PutDWord (SHORT (operands[i].val - code.pc - 4));
  810. free[i] := FALSE; i:= maxNumberOperands;
  811. ELSIF (free[i]) & InstructionSet.IsImmediate32(InstructionSet.instructions[instr].operands[i]) THEN
  812. operands[i].pc := code.pc;
  813. IF lastPass & (operands[i].symbol.name # "") THEN AddFixup(BinaryCode.Absolute,4,code.pc,operands[i].symbol,operands[i].symbolOffset,operands[i].displacement) END;
  814. code.PutDWord (SHORT (operands[i].val));
  815. free[i] := FALSE; i:= maxNumberOperands;
  816. END
  817. END;
  818. | iq:
  819. IF Trace THEN KernelLog.String(" iq "); END;
  820. IF val # -1 THEN code.PutByte (val); val := -1 END;
  821. FOR i := 0 TO maxNumberOperands - 1 DO
  822. IF (free[i]) & InstructionSet.IsImmediate64(InstructionSet.instructions[instr].operands[i]) THEN
  823. operands[i].pc := code.pc;
  824. IF lastPass & (operands[i].symbol.name # "") THEN
  825. AddFixup(BinaryCode.Absolute,8,code.pc,operands[i].symbol,operands[i].symbolOffset,operands[i].displacement)
  826. END;
  827. code.PutQWord (operands[i].val);
  828. free[i] := FALSE; i:= maxNumberOperands;
  829. END
  830. END;
  831. | rb, rw, rd, rq:
  832. IF Trace THEN KernelLog.String(" r* "); END;
  833. regOperand := GetRegOperand ();
  834. val := val + InstructionSet.RegisterIndex(operands[regOperand].register) MOD 8;
  835. code.PutByte (val); val := -1;
  836. free[regOperand] := FALSE;
  837. | fpStackOperand:
  838. IF Trace THEN KernelLog.String(" fp "); END;
  839. FOR i := 0 TO maxNumberOperands - 1 DO
  840. IF (free[i]) & (operands[i].type = sti) & (InstructionSet.instructions[instr].operands[i] # InstructionSet.st0) THEN
  841. val := val + InstructionSet.RegisterIndex(operands[i].register);
  842. code.PutByte (val); val := -1;
  843. free[i] := FALSE; i:= maxNumberOperands;
  844. END;
  845. END;
  846. | directMemoryOffset:
  847. IF Trace THEN KernelLog.String(" memoffset "); END;
  848. IF val # -1 THEN code.PutByte (val); val := -1 END;
  849. FOR i := 0 TO maxNumberOperands - 1 DO
  850. IF (free[i]) & (operands[i].type = mem) THEN
  851. IF cpuBits = bits16 THEN
  852. code.PutWord (operands[i].displacement);
  853. ELSE
  854. IF lastPass & (operands[i].symbol.name # "") THEN
  855. AddFixup(BinaryCode.Absolute,4,code.pc,operands[i].symbol,operands[i].symbolOffset,operands[i].displacement)
  856. END;
  857. code.PutDWord (operands[i].displacement);
  858. END;
  859. free[i] := FALSE; i:= maxNumberOperands;
  860. END;
  861. END;
  862. | mem64Operand, mem128Operand: (* ignored *)
  863. IF Trace THEN KernelLog.String(" mem64/mem128 "); END;
  864. ELSE HALT(100) (* decoding error *)
  865. END;
  866. END;
  867. INC(oppos);
  868. IF Trace THEN KernelLog.Ln; END;
  869. END;
  870. IF val # -1 THEN code.PutByte (val) END;
  871. ASSERT(oppos < LEN(opcode)); (* decoding or representation error otherwise *)
  872. RETURN TRUE;
  873. END EmitInstruction;
  874. PROCEDURE EmitPrefix* (prefix: LONGINT);
  875. BEGIN code.PutByte (prefix);
  876. END EmitPrefix;
  877. PROCEDURE Emit*(mnem: LONGINT; VAR op1,op2,op3: Operand);
  878. VAR operands: ARRAY maxNumberOperands OF Operand; res: BOOLEAN;
  879. BEGIN
  880. operands[0] := op1;
  881. operands[1] := op2;
  882. operands[2] := op3;
  883. res := EmitInstruction(mnem,operands,TRUE);
  884. op1 := operands[0];
  885. op2 := operands[1];
  886. op3 := operands[2];
  887. END Emit;
  888. PROCEDURE EmitAt*(pc: LONGINT;mnem: LONGINT; VAR op1,op2,op3: Operand);
  889. VAR prevPC: LONGINT; prevDump: Streams.Writer;
  890. BEGIN
  891. prevDump := dump;
  892. dump := NIL;
  893. prevPC := code.pc;
  894. code.SetPC(pc);
  895. Emit(mnem,op1,op2,op3);
  896. code.SetPC(prevPC);
  897. dump := prevDump;
  898. END EmitAt;
  899. PROCEDURE StartEmitAt*(VAR pc: LONGINT): LONGINT;
  900. VAR prevPC: LONGINT;
  901. BEGIN
  902. prevPC := code.pc;
  903. dump := NIL;
  904. code.SetPC(pc);
  905. RETURN prevPC;
  906. END StartEmitAt;
  907. PROCEDURE EndEmitAt*(pc: LONGINT);
  908. BEGIN
  909. code.SetPC(pc);
  910. SELF.dump := code.comments;
  911. END EndEmitAt;
  912. PROCEDURE Emit0* (mnem: LONGINT);
  913. VAR noOperand: Operand;
  914. BEGIN
  915. noOperand.type := none;
  916. Emit(mnem,noOperand,noOperand,noOperand);
  917. END Emit0;
  918. PROCEDURE Emit1* (mnem: LONGINT; VAR op1: Operand);
  919. VAR noOperand: Operand;
  920. BEGIN
  921. noOperand.type := none;
  922. Emit(mnem,op1,noOperand,noOperand);
  923. END Emit1;
  924. PROCEDURE Emit2* (mnem: LONGINT; VAR op1, op2: Operand);
  925. VAR noOperand: Operand;
  926. BEGIN
  927. noOperand.type := none;
  928. Emit(mnem,op1,op2,noOperand);
  929. END Emit2;
  930. PROCEDURE Emit3* (mnem: LONGINT; VAR op1, op2, op3: Operand);
  931. BEGIN
  932. Emit(mnem,op1,op2,op3);
  933. END Emit3;
  934. END Emitter;
  935. RegisterMapEntry*= POINTER TO RECORD
  936. name-: Strings.String;
  937. register-: LONGINT;
  938. next: RegisterMapEntry;
  939. END;
  940. RegisterMap*= OBJECT
  941. VAR first: RegisterMapEntry;
  942. PROCEDURE & Init *;
  943. BEGIN
  944. first := NIL
  945. END Init;
  946. PROCEDURE Find*(CONST name: ARRAY OF CHAR): LONGINT;
  947. VAR map: RegisterMapEntry;
  948. BEGIN
  949. map := first;
  950. WHILE (map # NIL) & (map.name^#name) DO map := map.next END;
  951. IF map = NIL THEN RETURN InstructionSet.none ELSE RETURN map.register END;
  952. END Find;
  953. PROCEDURE Add*(name: Strings.String; register: LONGINT);
  954. VAR map: RegisterMapEntry;
  955. BEGIN
  956. NEW(map); map.name := name; map.register := register;
  957. map.next := first; first := map;
  958. END Add;
  959. END RegisterMap;
  960. Assembly* = OBJECT
  961. VAR
  962. (* output *)
  963. errPos: LONGINT;
  964. error-: BOOLEAN;
  965. emitter: Emitter;
  966. (* overal state *)
  967. diagnostics: Diagnostics.Diagnostics;
  968. dump: Streams.Writer;
  969. (* temporaries *)
  970. fixup: BinaryCode.Fixup;
  971. type: SHORTINT;
  972. currentFixup: Sections.SectionName;
  973. currentLabel: NamedLabel;
  974. sourceName: Basic.FileName;
  975. PROCEDURE & InitAssembly*(diagnostics: Diagnostics.Diagnostics; emit: Emitter);
  976. BEGIN
  977. SELF.diagnostics := diagnostics;
  978. errPos := Diagnostics.Invalid;
  979. error := FALSE;
  980. SELF.emitter := emit;
  981. sourceName := "";
  982. END InitAssembly;
  983. PROCEDURE Error( CONST message: ARRAY OF CHAR);
  984. VAR pos: LONGINT; msg,name: ARRAY 256 OF CHAR;
  985. BEGIN
  986. pos := errPos;
  987. IF (pos = Diagnostics.Invalid) OR (sourceName = "") THEN
  988. COPY(message,msg);
  989. Strings.Append(msg," in ");
  990. ObjectFile.SegmentedNameToString(emitter.code.os.identifier.name, name);
  991. Strings.Append(msg, name);
  992. diagnostics.Error(sourceName,errPos,Diagnostics.Invalid,msg);
  993. ELSE
  994. diagnostics.Error(sourceName,errPos,Diagnostics.Invalid,message);
  995. END;
  996. error := TRUE;
  997. IF dump # NIL THEN dump.Update; END;
  998. END Error;
  999. PROCEDURE ErrorSS(CONST msg1,msg2: ARRAY OF CHAR);
  1000. VAR message: ARRAY 256 OF CHAR;
  1001. BEGIN
  1002. COPY(msg1,message);
  1003. Strings.Append(message," : ");
  1004. Strings.Append(message, msg2);
  1005. Error(message);
  1006. END ErrorSS;
  1007. PROCEDURE Assemble* (reader: Streams.Reader; orgPos: LONGINT; scope: SyntaxTree.Scope; in: IntermediateCode.Section; out: IntermediateCode.Section; module: Sections.Module; exported, inlined: BOOLEAN;
  1008. map: RegisterMap
  1009. );
  1010. CONST maxPasses = 2;
  1011. VAR
  1012. symbol, reg: LONGINT;
  1013. ident, idents: Name;
  1014. val, times, val2, val3: LONGINT;
  1015. currentLabel: NamedLabel;
  1016. labels: NamedLabelList;
  1017. prevPC: LONGINT;
  1018. pass: LONGINT;
  1019. absoluteMode: BOOLEAN;
  1020. absoluteOffset: LONGINT;
  1021. alignment: LONGINT;
  1022. orgOffset: LONGINT;
  1023. char: CHAR;
  1024. orgReaderPos: LONGINT;
  1025. orgCodePos: LONGINT;
  1026. prevSourceName: Basic.FileName;
  1027. position: LONGINT;
  1028. prevCpuBits: Size;
  1029. prevCpuOptions: InstructionSet.CPUOptions;
  1030. PROCEDURE NextChar;
  1031. BEGIN
  1032. (*
  1033. IF (dump # NIL) & (pass = maxPasses) THEN dump.Char (char) END;
  1034. *)
  1035. reader.Char(char); INC(position);
  1036. END NextChar;
  1037. PROCEDURE SkipBlanks;
  1038. BEGIN
  1039. (* tf returns 01X when an embedded object is encountered *)
  1040. WHILE (char = SPACE) OR (char = TAB) OR (char = 01X) DO NextChar END;
  1041. IF char = ";" THEN
  1042. WHILE (char # CR) & (char # LF) & (char # 0X) DO NextChar END (* Skip comments *)
  1043. END;
  1044. END SkipBlanks;
  1045. PROCEDURE GetNumber (VAR intval: LONGINT);
  1046. VAR i, m, n: INTEGER; dig: ARRAY 24 OF CHAR;
  1047. BEGIN
  1048. i := 0; m := 0; n := 0;
  1049. WHILE ('0' <= char) & (char <= '9') OR ('A' <= CAP (char)) & (CAP (char) <= 'F') DO
  1050. IF (m > 0) OR (char # "0") THEN (* ignore leading zeros *)
  1051. IF n < LEN(dig) THEN dig[n] := char; INC(n) END;
  1052. INC(m)
  1053. END;
  1054. NextChar; INC(i)
  1055. END;
  1056. IF n = m THEN intval := 0; i := 0;
  1057. IF (CAP (char) = "H") OR (char = "X") THEN NextChar;
  1058. IF (n = Scanner.MaxHexDigits) & (dig[0] > "7") THEN (* prevent overflow *) intval := -1 END;
  1059. WHILE i < n DO intval := intval * 10H + HexOrd (dig[i]); INC(i) END;
  1060. ELSE
  1061. IF (n = Scanner.MaxHugeHexDigits) & (dig[0] > "7") THEN (* prevent overflow *) intval := -1 END;
  1062. WHILE i < n DO intval := intval * 10 + Ord (dig[i]); INC(i) END
  1063. END
  1064. END;
  1065. END GetNumber;
  1066. PROCEDURE GetIdentifier;
  1067. VAR i: LONGINT;
  1068. BEGIN
  1069. i := 0;
  1070. REPEAT
  1071. IF i < Scanner.MaxIdentifierLength - 1 THEN
  1072. IF ('0' <= char) & (char <= '9') THEN
  1073. ident[i] := char; idents[i] := char;
  1074. ELSE
  1075. ident[i] := (* CAP *) (char); idents[i] := char; END;
  1076. INC (i);
  1077. END;
  1078. NextChar
  1079. UNTIL ~((('A' <= CAP(char)) & (CAP(char) <= 'Z')) OR (('0' <= char) & (char <= '9')));
  1080. ident[i] := 0X; idents[i] := 0X;
  1081. END GetIdentifier;
  1082. PROCEDURE GetString;
  1083. VAR i: LONGINT;
  1084. BEGIN
  1085. i := 0;
  1086. NextChar;
  1087. WHILE (char # "'") & (i < Scanner.MaxIdentifierLength - 1) DO
  1088. ident[i] := char; INC (i);
  1089. NextChar;
  1090. END;
  1091. ident[i] := 0X;
  1092. NextChar;
  1093. END GetString;
  1094. PROCEDURE NextSymbol;
  1095. BEGIN
  1096. SkipBlanks;
  1097. errPos := position- 1;
  1098. CASE char OF
  1099. 'A' .. 'Z', 'a' .. 'z' :
  1100. GetIdentifier;
  1101. SkipBlanks;
  1102. IF char = ':' THEN
  1103. NextChar; symbol := symLabel;
  1104. ELSE
  1105. symbol := symIdent;
  1106. END;
  1107. | '0' .. '9':
  1108. GetNumber (val);
  1109. symbol := symNumber;
  1110. | "'": GetString;
  1111. symbol := symString;
  1112. | '.': symbol := symPeriod;
  1113. NextChar;
  1114. | ';': symbol := symSemicolon;
  1115. NextChar;
  1116. | ':': symbol := symColon;
  1117. NextChar;
  1118. | CR, LF: symbol := symLn;
  1119. NextChar;
  1120. | ',': symbol := symComma;
  1121. NextChar;
  1122. | '+': symbol := symPlus;
  1123. NextChar;
  1124. | '-': symbol := symMinus;
  1125. NextChar;
  1126. | '*': symbol := symTimes;
  1127. NextChar;
  1128. | '/': symbol := symDiv;
  1129. NextChar;
  1130. | '%': symbol := symMod;
  1131. NextChar;
  1132. | '~': symbol := symNegate;
  1133. NextChar;
  1134. | '(': symbol := symLParen;
  1135. NextChar;
  1136. | ')': symbol := symRParen;
  1137. NextChar;
  1138. | '[': symbol := symLBraket;
  1139. NextChar;
  1140. | ']': symbol := symRBraket;
  1141. NextChar;
  1142. | '{': symbol := symLBrace;
  1143. NextChar;
  1144. | '}': symbol := symRBrace;
  1145. NextChar;
  1146. | '@': symbol := symAt;
  1147. NextChar;
  1148. | '$': NextChar;
  1149. IF char = '$' THEN
  1150. symbol := symPCOffset; NextChar;
  1151. ELSE
  1152. symbol := symPC;
  1153. END
  1154. | 0X: symbol := symEnd;
  1155. ELSE
  1156. symbol := symNone;
  1157. NextChar;
  1158. END;
  1159. END NextSymbol;
  1160. PROCEDURE SkipLine;
  1161. BEGIN
  1162. WHILE (symbol # symLn) & (symbol # symNone) DO
  1163. NextSymbol;
  1164. END;
  1165. END SkipLine;
  1166. PROCEDURE Ensure (desiredSymbol, errNumber : LONGINT) : BOOLEAN;
  1167. VAR temp: LONGINT;
  1168. BEGIN
  1169. temp := symbol;
  1170. IF symbol = desiredSymbol THEN
  1171. NextSymbol;
  1172. RETURN TRUE;
  1173. ELSE
  1174. Error("other symbol expected");
  1175. RETURN FALSE;
  1176. END;
  1177. END Ensure;
  1178. PROCEDURE GetCPU (cumulateOptions: BOOLEAN): BOOLEAN;
  1179. VAR i: LONGINT;
  1180. BEGIN
  1181. SkipBlanks;
  1182. GetIdentifier;
  1183. Strings.UpperCase(ident);
  1184. i := InstructionSet.FindCPU (ident);
  1185. IF i # InstructionSet.none THEN
  1186. IF cumulateOptions THEN
  1187. emitter.cpuOptions := emitter.cpuOptions + InstructionSet.cpus[i].cpuOptions;
  1188. ELSE
  1189. emitter.cpuOptions := InstructionSet.cpus[i].cpuOptions + InstructionSet.cpuOptions;
  1190. END;
  1191. NextSymbol;
  1192. RETURN TRUE;
  1193. ELSE
  1194. ErrorSS ("cpu unknown",ident);
  1195. emitter.cpuOptions := prevCpuOptions;
  1196. RETURN FALSE;
  1197. END;
  1198. END GetCPU;
  1199. PROCEDURE Factor (VAR x: LONGINT; critical: BOOLEAN; VAR type: SHORTINT): BOOLEAN;
  1200. VAR label: NamedLabel; l: LONGINT;
  1201. BEGIN
  1202. IF symbol = symNumber THEN
  1203. x := val; NextSymbol; RETURN TRUE;
  1204. ELSIF symbol = symPC THEN
  1205. x := (orgOffset + emitter.code.pc ); NextSymbol; RETURN TRUE;
  1206. ELSIF symbol = symPCOffset THEN
  1207. x := orgOffset; NextSymbol; RETURN TRUE;
  1208. ELSIF symbol = symString THEN
  1209. x := 0; l := Strings.Length (ident);
  1210. IF l > 0 THEN INC (x, ORD (ident [0])) END;
  1211. IF l > 1 THEN INC (x, ORD (ident [1])*100H) END;
  1212. IF l > 2 THEN INC (x, ORD (ident [2])*10000H) END;
  1213. IF l > 3 THEN INC (x, ORD (ident [3])*1000000H) END;
  1214. NextSymbol; RETURN TRUE;
  1215. ELSIF symbol = symIdent THEN
  1216. label := labels.Find (idents);
  1217. NextSymbol;
  1218. IF label # NIL THEN
  1219. x := (label.offset );
  1220. type := ioffset;
  1221. currentLabel := label;
  1222. (*
  1223. IF x = MAX(LONGINT) THEN
  1224. x := -label.index;
  1225. currentFixup := in;
  1226. END;
  1227. *)
  1228. RETURN TRUE;
  1229. ELSIF scope # NIL THEN
  1230. IF ~GetValue(idents,x) THEN
  1231. IF (pass = maxPasses) THEN
  1232. Error("constant expected");
  1233. END;
  1234. RETURN FALSE;
  1235. ELSE
  1236. RETURN TRUE;
  1237. END
  1238. END;
  1239. IF (~critical) & (pass # maxPasses) THEN
  1240. x := 0;
  1241. RETURN TRUE
  1242. END;
  1243. Error("undefined symbol");
  1244. RETURN FALSE;
  1245. ELSIF symbol = symLParen THEN
  1246. NextSymbol;
  1247. RETURN Expression (x, critical,type) & Ensure (symRParen, 555);
  1248. END;
  1249. Error("parse error in expression");
  1250. RETURN FALSE
  1251. END Factor;
  1252. PROCEDURE Term (VAR x: LONGINT; critical: BOOLEAN; VAR type: SHORTINT): BOOLEAN;
  1253. VAR y, op : LONGINT;
  1254. BEGIN
  1255. IF Factor (x, critical,type) THEN
  1256. WHILE (symbol = symTimes) OR (symbol = symDiv) OR (symbol = symMod) DO
  1257. op := symbol; NextSymbol;
  1258. IF Factor (y, critical,type) THEN
  1259. IF op = symTimes THEN x := x * y
  1260. ELSIF op = symDiv THEN x := x DIV y
  1261. ELSE x := x MOD y
  1262. END;
  1263. ELSE
  1264. RETURN FALSE;
  1265. END;
  1266. END;
  1267. RETURN TRUE;
  1268. ELSE
  1269. RETURN FALSE;
  1270. END;
  1271. END Term;
  1272. PROCEDURE Expression (VAR x: LONGINT; critical: BOOLEAN; VAR type: SHORTINT): BOOLEAN;
  1273. VAR y, op : LONGINT;
  1274. BEGIN
  1275. IF symbol = symMinus THEN
  1276. op := symbol; NextSymbol;
  1277. IF Term (x, critical,type) THEN
  1278. x := -x
  1279. ELSE
  1280. RETURN FALSE;
  1281. END;
  1282. ELSIF symbol = symPlus THEN
  1283. op := symbol; NextSymbol;
  1284. IF ~Term (x, critical,type) THEN
  1285. RETURN FALSE;
  1286. END;
  1287. ELSIF symbol = symNegate THEN
  1288. op := symbol; NextSymbol;
  1289. IF Term (x, critical,type) THEN
  1290. x := -x - 1
  1291. ELSE
  1292. RETURN FALSE;
  1293. END;
  1294. ELSIF ~Term (x, critical,type) THEN
  1295. RETURN FALSE;
  1296. END;
  1297. WHILE (symbol = symPlus) OR (symbol = symMinus) DO
  1298. op := symbol; NextSymbol;
  1299. IF Term (y, critical,type) THEN
  1300. IF op = symPlus THEN x := x + y ELSE x := x - y END;
  1301. ELSE
  1302. RETURN FALSE;
  1303. END;
  1304. END;
  1305. RETURN TRUE;
  1306. END Expression;
  1307. PROCEDURE Align(size: LONGINT);
  1308. VAR pc: LONGINT;
  1309. BEGIN
  1310. IF size <= 0 THEN Error("invalid alignment size"); RETURN END;
  1311. pc := emitter.code.pc DIV 8; (* bytes *)
  1312. WHILE pc MOD size # 0 DO
  1313. emitter.code.PutByte(0);
  1314. INC(pc);
  1315. END;
  1316. END Align;
  1317. PROCEDURE PutData (size: Size): BOOLEAN;
  1318. VAR i: LONGINT; type:SHORTINT; ofs: Operand;
  1319. BEGIN
  1320. NextSymbol;
  1321. WHILE symbol # symLn DO
  1322. IF symbol = symString THEN
  1323. i := 0;
  1324. WHILE ident[i] # 0X DO
  1325. emitter.code.PutByte (ORD (ident[i]));
  1326. INC (i);
  1327. END;
  1328. IF size # bits8 THEN
  1329. i := (size ) - i MOD (size );
  1330. WHILE i # 0 DO emitter.code.PutByte (0); DEC (i) END;
  1331. END;
  1332. NextSymbol;
  1333. ELSIF (scope # NIL) & (symbol = symAt) THEN
  1334. NextSymbol;
  1335. IF symbol # symIdent THEN Error("identifier missing") END;
  1336. GetOffsetFixup (idents, ofs);
  1337. NextSymbol;
  1338. IF symbol = symPlus THEN
  1339. NextSymbol;
  1340. IF Expression(i, FALSE, type) THEN
  1341. ofs.displacement := i
  1342. END;
  1343. ELSIF symbol = symMinus THEN
  1344. NextSymbol;
  1345. IF Expression(i, FALSE, type) THEN
  1346. ofs.displacement := - i
  1347. END;
  1348. END;
  1349. IF pass = maxPasses THEN
  1350. emitter.AddFixup(BinaryCode.Absolute, ofs.sizeInBytes, emitter.code.pc, ofs.symbol, ofs.symbolOffset,ofs.displacement);
  1351. END;
  1352. emitter.code.PutBytes (0, size );
  1353. ELSIF Expression (i, FALSE,type) THEN
  1354. emitter.code.PutBytes (i, size );
  1355. ELSE
  1356. RETURN FALSE;
  1357. END;
  1358. IF symbol = symComma THEN
  1359. NextSymbol;
  1360. ELSIF symbol # symLn THEN
  1361. Error("operand missing");
  1362. END
  1363. END;
  1364. Duplicate ((emitter.code.pc - prevPC) , NIL);
  1365. RETURN TRUE;
  1366. END PutData;
  1367. PROCEDURE Duplicate (size: LONGINT; fixup: BinaryCode.Fixup);
  1368. VAR i: LONGINT; buffer: ARRAY 100 OF CHAR; pc: LONGINT;
  1369. BEGIN
  1370. IF times = 1 THEN RETURN END;
  1371. pc := (prevPC );
  1372. IF (dump # NIL) & (pass = maxPasses) THEN dump.Hex (emitter.code.pc, 1); dump.Char (' ') END;
  1373. FOR i := 0 TO size - 1 DO
  1374. buffer[i] := emitter.code.GetByte (pc); INC(pc);
  1375. IF (dump # NIL) & (pass = maxPasses) THEN dump.Hex (ORD (buffer[i]), -2); END;
  1376. END;
  1377. pc := (prevPC );
  1378. IF times > 1 THEN
  1379. WHILE times # 1 DO
  1380. IF fixup # NIL THEN
  1381. HALT(200);
  1382. (*!!
  1383. AddFixup (fixup.adr, pc + fixup.offset - prevPC);
  1384. *)
  1385. END;
  1386. FOR i := 0 TO size - 1 DO
  1387. emitter.code.PutByteAt (pc, ORD (buffer[i])); INC(pc);
  1388. IF (dump # NIL) & (pass = maxPasses) THEN dump.Hex (ORD (buffer[i]), -2); END;
  1389. END;
  1390. DEC (times);
  1391. END;
  1392. ELSE
  1393. times := 1;
  1394. END;
  1395. IF (dump # NIL) & (pass = maxPasses) THEN dump.Ln END;
  1396. END Duplicate;
  1397. PROCEDURE Reserve (size: Size) : BOOLEAN;
  1398. VAR type : SHORTINT;
  1399. BEGIN
  1400. IF Expression (val2, TRUE, type) THEN
  1401. absoluteOffset := absoluteOffset + val2 * size;
  1402. RETURN TRUE;
  1403. ELSE
  1404. RETURN FALSE;
  1405. END;
  1406. END Reserve;
  1407. PROCEDURE GetScopeSymbol (CONST ident: ARRAY OF CHAR): SyntaxTree.Symbol;
  1408. VAR sym: SyntaxTree.Symbol; localScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  1409. BEGIN
  1410. localScope := scope;
  1411. identifier := SyntaxTree.NewIdentifier(ident);
  1412. IF Trace THEN KernelLog.String("GetScopeSymbol:"); KernelLog.String(ident); KernelLog.Ln; END;
  1413. WHILE (sym = NIL) & (localScope # NIL) DO
  1414. sym := localScope.FindSymbol(identifier);
  1415. localScope := localScope.outerScope
  1416. END;
  1417. IF (sym # NIL) & (sym IS SyntaxTree.Import) THEN
  1418. NextSymbol;
  1419. IF Ensure(symPeriod,0) & (symbol = symIdent) THEN
  1420. identifier := SyntaxTree.NewIdentifier(idents);
  1421. IF Trace THEN KernelLog.String("GetScopeSymbol :"); KernelLog.String(idents); KernelLog.Ln; END;
  1422. localScope := sym(SyntaxTree.Import).module.moduleScope;
  1423. sym := NIL;
  1424. WHILE (sym = NIL) & (localScope # NIL) DO
  1425. sym := localScope.FindSymbol(identifier);
  1426. localScope := localScope.outerScope
  1427. END;
  1428. END;
  1429. END;
  1430. IF Trace THEN IF sym = NIL THEN KernelLog.String("not found") ELSE KernelLog.String("found"); END; KernelLog.Ln; END;
  1431. RETURN sym
  1432. END GetScopeSymbol;
  1433. PROCEDURE GetValue(CONST ident: ARRAY OF CHAR; VAR x: LONGINT): BOOLEAN;
  1434. VAR scopeSymbol:SyntaxTree.Symbol;
  1435. BEGIN
  1436. scopeSymbol := GetScopeSymbol (ident);
  1437. IF scopeSymbol = NIL THEN RETURN FALSE
  1438. ELSIF ~(scopeSymbol IS SyntaxTree.Constant) THEN RETURN FALSE
  1439. ELSE
  1440. IF (scopeSymbol.type.resolved IS SyntaxTree.CharacterType) & (scopeSymbol.type.resolved.sizeInBits=8) THEN
  1441. x := ORD(scopeSymbol(SyntaxTree.Constant).value.resolved(SyntaxTree.CharacterValue).value)
  1442. ELSIF scopeSymbol.type.resolved IS SyntaxTree.IntegerType THEN
  1443. x := scopeSymbol(SyntaxTree.Constant).value.resolved(SyntaxTree.IntegerValue).value
  1444. ELSE
  1445. Error("number expected");
  1446. RETURN FALSE;
  1447. END;
  1448. RETURN TRUE;
  1449. END;
  1450. END GetValue;
  1451. PROCEDURE GetMemFixup (CONST ident: ARRAY OF CHAR; VAR operand: Operand);
  1452. VAR scopeSymbol:SyntaxTree.Symbol;
  1453. BEGIN
  1454. scopeSymbol := GetScopeSymbol (ident);
  1455. IF scopeSymbol = NIL THEN RETURN END;
  1456. IF scopeSymbol IS SyntaxTree.Constant THEN
  1457. RETURN
  1458. END;
  1459. IF inlined & exported THEN
  1460. Error("no symbols may be accessed in exported and inlined procedures");
  1461. END;
  1462. IF (scopeSymbol IS SyntaxTree.Variable) & (scopeSymbol.scope = module.module.moduleScope) THEN (* global variable. offset not supported *)
  1463. Error("global variables cannot be accessed as memory operands");
  1464. ELSIF (scopeSymbol IS SyntaxTree.Variable) THEN (* local variable *)
  1465. operand.displacement := (scopeSymbol.offsetInBits DIV 8)
  1466. ELSIF (scopeSymbol IS SyntaxTree.Parameter) THEN (* local parameter *)
  1467. operand.displacement := (scopeSymbol.offsetInBits DIV 8)
  1468. ELSE
  1469. RETURN (* ? *)
  1470. END;
  1471. (*! mem.fixup := scopeSymbol.adr; *)
  1472. NextSymbol;
  1473. END GetMemFixup;
  1474. PROCEDURE GetOffsetFixup (CONST ident: ARRAY OF CHAR; VAR operand: Operand);
  1475. VAR scopeSymbol: SyntaxTree.Symbol;name: Basic.SegmentedName; symbol: IntermediateCode.Section;
  1476. BEGIN
  1477. IF labels.Find(ident) # NIL THEN RETURN END;
  1478. scopeSymbol := GetScopeSymbol (ident);
  1479. IF (scopeSymbol = NIL) OR (scopeSymbol IS SyntaxTree.Constant) THEN RETURN END;
  1480. IF inlined & exported THEN
  1481. Error("no symbols may be accessed in exported and inlined procedures");
  1482. END;
  1483. Global.GetSymbolSegmentedName(scopeSymbol,name);
  1484. IF scopeSymbol.scope IS SyntaxTree.ModuleScope THEN
  1485. IF (scopeSymbol IS SyntaxTree.Variable) THEN
  1486. InitMem(operand,IntermediateCode.Bits32,none,0); (* or immediate ?? *)
  1487. ELSIF (scopeSymbol IS SyntaxTree.Procedure) & (scopeSymbol.scope = module.module.moduleScope) THEN
  1488. IF scopeSymbol(SyntaxTree.Procedure).isInline THEN
  1489. Error("fobidden reference to inline call");
  1490. ELSE
  1491. InitOffset32(operand,0); (* or immediate ?? *)
  1492. END;
  1493. ELSIF (scopeSymbol IS SyntaxTree.Procedure) THEN
  1494. InitOffset32(operand,0); (* or immediate ?? *)
  1495. END;
  1496. SetSymbol(operand,name,0,0,0);
  1497. ELSE
  1498. Error("direct access to local variable offset forbidden");
  1499. END;
  1500. operand.sizeInBytes := emitter.cpuBits;
  1501. END GetOffsetFixup;
  1502. (* the following procedure is used to adapt sizes for relative jumps *)
  1503. PROCEDURE AdaptOperandSizes(VAR operands: ARRAY OF Operand);
  1504. VAR i: LONGINT;
  1505. PROCEDURE OffsetSize(val: HUGEINT): SHORTINT;
  1506. BEGIN
  1507. DEC(val,emitter.code.pc);
  1508. IF (val > MIN(SHORTINT)+2) & (val < MAX(SHORTINT)) THEN
  1509. RETURN bits8
  1510. (* We do not support word (16-bit) displacement jumps
  1511. (i.e. prefixing the jump instruction with the `addr16' opcode prefix),
  1512. since the 80386 insists upon masking `%eip' to 16 bits after the word
  1513. displacement is added. *)
  1514. ELSIF (val > MIN(LONGINT)+2) & (val < MAX(LONGINT)-2) THEN
  1515. RETURN bits32
  1516. ELSE
  1517. RETURN bits64
  1518. END;
  1519. END OffsetSize;
  1520. BEGIN
  1521. i := 0;
  1522. WHILE (i< LEN(operands)) & (operands[i].type # none) DO
  1523. IF (operands[i].type = ioffset) & (operands[i].sizeInBytes = bitsDefault)
  1524. THEN
  1525. IF operands[i].symbol.name = "" THEN
  1526. operands[i].sizeInBytes := OffsetSize(operands[i].val);
  1527. ELSE
  1528. operands[i].sizeInBytes := bits32
  1529. END;
  1530. END;
  1531. INC(i)
  1532. END;
  1533. END AdaptOperandSizes;
  1534. PROCEDURE GetInstruction (): BOOLEAN;
  1535. VAR
  1536. mnem, opCount: LONGINT;
  1537. size: Size;
  1538. operands: ARRAY InstructionSet.maxNumberOperands OF Operand;
  1539. prevFixup: BinaryCode.Fixup;
  1540. mem: Operand;
  1541. offset: Operand;
  1542. i: LONGINT;
  1543. type: SHORTINT;
  1544. BEGIN
  1545. mnem := InstructionSet.FindMnemonic (ident);
  1546. IF mnem = InstructionSet.none THEN
  1547. ErrorSS("unkown instruction",idents);
  1548. RETURN FALSE;
  1549. END;
  1550. opCount := 0;
  1551. NextSymbol;
  1552. FOR i := 0 TO LEN(operands)-1 DO
  1553. InitOperand(operands[i]);
  1554. END;
  1555. WHILE (symbol # symLn) & (symbol # symNone) & (symbol # symEnd) DO
  1556. IF symbol = symIdent THEN
  1557. IF (ident = "BYTE") OR (ident = "SHORT") THEN
  1558. size := bits8; NextSymbol;
  1559. ELSIF (ident = "WORD") OR (ident = "NEAR") THEN
  1560. size := bits16; NextSymbol;
  1561. ELSIF ident = "DWORD" THEN
  1562. size := bits32; NextSymbol;
  1563. ELSIF ident = "QWORD" THEN
  1564. size := bits64; NextSymbol;
  1565. ELSIF ident = "TWORD" THEN
  1566. size := bits128; NextSymbol;
  1567. ELSE
  1568. size := bitsDefault;
  1569. END;
  1570. ELSE
  1571. size := bitsDefault;
  1572. END;
  1573. IF symbol = symIdent THEN (* register ?, for example EAX *)
  1574. reg := InstructionSet.FindRegister (ident);
  1575. IF reg = InstructionSet.none THEN
  1576. reg := map.Find(ident)
  1577. END;
  1578. IF reg # InstructionSet.none THEN
  1579. IF size # bitsDefault THEN
  1580. Error ("invalid register size specification"); RETURN FALSE;
  1581. END;
  1582. InitRegister(operands[opCount], reg);
  1583. INC (opCount);
  1584. NextSymbol;
  1585. END;
  1586. ELSE
  1587. reg := InstructionSet.none;
  1588. END;
  1589. IF reg = InstructionSet.none THEN
  1590. IF symbol = symLBraket THEN
  1591. (* mem, written as [....] *)
  1592. NextSymbol;
  1593. InitMem(mem, size, InstructionSet.none,0); (*! ??? *)
  1594. IF symbol = symLabel THEN (* register segment as in [ES:...] *)
  1595. reg := InstructionSet.FindRegister (ident);
  1596. IF reg = InstructionSet.none THEN
  1597. ErrorSS("undefined symbol",idents);
  1598. RETURN FALSE;
  1599. END;
  1600. mem.segment := reg;
  1601. NextSymbol;
  1602. END;
  1603. IF symbol = symIdent THEN (* register, for example [EAX] or [ES:EAX] *)
  1604. reg := InstructionSet.FindRegister (ident);
  1605. IF reg # InstructionSet.none THEN
  1606. mem.register := reg;
  1607. NextSymbol;
  1608. IF symbol = symTimes THEN (* register multiply as in [EAX*4] *)
  1609. NextSymbol;
  1610. IF ~Factor (mem.scale, FALSE,type) THEN
  1611. RETURN FALSE;
  1612. END;
  1613. mem.index := mem.register;
  1614. mem.register := InstructionSet.none;
  1615. END;
  1616. IF symbol = symPlus THEN (* register add as in [EAX + EBX] *)
  1617. NextSymbol;
  1618. IF symbol = symIdent THEN
  1619. reg := InstructionSet.FindRegister (ident);
  1620. IF reg # InstructionSet.none THEN (* maybe it is this: [EAX + EBX * 4] *)
  1621. NextSymbol;
  1622. IF mem.index = InstructionSet.none THEN
  1623. mem.index := reg;
  1624. IF symbol = symTimes THEN
  1625. NextSymbol;
  1626. IF ~Factor (mem.scale, FALSE,type) THEN
  1627. RETURN FALSE;
  1628. END;
  1629. END;
  1630. ELSE
  1631. mem.register := reg;
  1632. END;
  1633. END;
  1634. END;
  1635. END;
  1636. END;
  1637. END;
  1638. IF symbol = symPlus THEN
  1639. NextSymbol;
  1640. END;
  1641. IF (scope # NIL) & (symbol = symIdent) THEN
  1642. GetMemFixup (idents, mem);
  1643. END;
  1644. IF (symbol # symRBraket) & (symbol # symNegate) THEN
  1645. val2 := 0;
  1646. IF ~Expression (val2, FALSE ,type) THEN
  1647. RETURN FALSE;
  1648. END;
  1649. INC (mem.displacement, val2);
  1650. ELSIF (mem.register = InstructionSet.none) & (mem.index = InstructionSet.none) THEN
  1651. Error("operand missing: no register provided");
  1652. RETURN FALSE;
  1653. END;
  1654. operands[opCount] := mem;
  1655. INC (opCount);
  1656. IF ~Ensure (symRBraket, 556) THEN
  1657. RETURN FALSE;
  1658. END;
  1659. ELSE
  1660. (* number or identifier (symbol) *)
  1661. InitImm(offset,size,0);
  1662. IF (scope # NIL) & (symbol = symIdent) THEN (* identifier: must be a symbol *)
  1663. GetOffsetFixup (idents, offset);
  1664. END;
  1665. IF offset.symbol.name = "" THEN (* nothing could be fixuped, must be a number / constant *)
  1666. type := offset.type; currentFixup := ""; currentLabel := NIL;
  1667. IF ~Expression (val2, FALSE,type) THEN
  1668. RETURN FALSE;
  1669. ELSE
  1670. offset.type := type;
  1671. IF currentFixup # "" THEN
  1672. offset.symbol.name := currentFixup; offset.symbolOffset := val2;
  1673. ELSIF currentLabel # NIL THEN
  1674. IF (offset.sizeInBytes = bitsDefault ) & (val2 > emitter.code.pc) THEN (* forward jump *)
  1675. offset.sizeInBytes := bits32
  1676. END;
  1677. (*
  1678. IF offset.sizeInBytes = bitsDefault THEN
  1679. offset.sizeInBytes := bits32;
  1680. END;
  1681. *)
  1682. END;
  1683. END;
  1684. offset.val := val2;
  1685. IF symbol = symColon THEN (* additional prefixed operand separated by ":", segmentation register *)
  1686. NextSymbol;
  1687. IF ~Expression (val3, FALSE, type) THEN
  1688. RETURN FALSE;
  1689. END;
  1690. InitOffset(operands[opCount],bitsDefault,val3);
  1691. INC (opCount);
  1692. END;
  1693. ELSE
  1694. NextSymbol;
  1695. END;
  1696. operands[opCount] := offset;
  1697. INC (opCount);
  1698. END;
  1699. END;
  1700. IF symbol = symComma THEN
  1701. NextSymbol;
  1702. ELSIF (symbol # symLn) & (symbol # symEnd) THEN
  1703. Error("operand missing");
  1704. END
  1705. END;
  1706. prevFixup := fixup;
  1707. AdaptOperandSizes(operands);
  1708. IF ~emitter.EmitInstruction (mnem, operands, pass = maxPasses) THEN
  1709. RETURN FALSE;
  1710. END;
  1711. IF fixup = prevFixup THEN
  1712. Duplicate ((emitter.code.pc - prevPC) , NIL);
  1713. ELSE
  1714. Duplicate ((emitter.code.pc - prevPC) , fixup);
  1715. END;
  1716. RETURN TRUE;
  1717. END GetInstruction;
  1718. PROCEDURE Reset;
  1719. BEGIN
  1720. position := orgPos;
  1721. reader.SetPos(orgReaderPos);
  1722. emitter.code.SetPC(orgCodePos);
  1723. NextChar;
  1724. END Reset;
  1725. PROCEDURE FindLabels;
  1726. VAR firstInLine : BOOLEAN; label: NamedLabel;
  1727. BEGIN
  1728. IF Trace THEN KernelLog.String("find labels"); KernelLog.Ln; END;
  1729. LOOP
  1730. NextSymbol;
  1731. IF symbol = symLn THEN
  1732. firstInLine := TRUE;
  1733. ELSIF symbol = symLabel THEN
  1734. IF firstInLine THEN
  1735. IF labels.Find(idents) # NIL THEN
  1736. Error("multiply declared identifier")
  1737. ELSE
  1738. NEW(label,MAX(LONGINT),idents);
  1739. labels.Add(label);
  1740. IF Trace THEN KernelLog.String("found label"); KernelLog.String(idents); KernelLog.Ln; END;
  1741. END
  1742. END;
  1743. ELSIF symbol = symEnd THEN
  1744. EXIT
  1745. ELSE
  1746. firstInLine := FALSE;
  1747. END;
  1748. END;
  1749. END FindLabels;
  1750. PROCEDURE FixupLabels;
  1751. VAR label: NamedLabel;
  1752. BEGIN
  1753. IF Trace THEN KernelLog.String("patch fixups "); KernelLog.Ln; END;
  1754. fixup := emitter.code.fixupList.firstFixup;
  1755. WHILE fixup # NIL DO
  1756. IF (fixup.symbol.name = in.name) & (fixup.symbolOffset < 0) THEN
  1757. label := labels.first;
  1758. WHILE (label # NIL) & (label.index # -fixup.symbolOffset) DO label := label.nextNamedLabel END;
  1759. (*
  1760. fixup.SetSymbolOffset(label.offset);
  1761. *)
  1762. fixup.SetSymbol(out.name,0,0,label.offset+fixup.displacement);
  1763. IF Trace THEN
  1764. KernelLog.String("patch fixup: ");
  1765. KernelLog.Hex(fixup.offset,1); KernelLog.String(" "); KernelLog.Hex(-fixup.displacement, 1);
  1766. KernelLog.String(" "); KernelLog.Hex(label.offset, 1); KernelLog.Ln;
  1767. END;
  1768. END;
  1769. fixup := fixup.nextFixup;
  1770. END;
  1771. END FixupLabels;
  1772. BEGIN
  1773. prevSourceName := sourceName;
  1774. prevCpuBits := emitter.cpuBits;
  1775. prevCpuOptions := emitter.cpuOptions;
  1776. IF scope # NIL THEN
  1777. sourceName := scope.ownerModule.sourceName;
  1778. END;
  1779. NEW(labels);
  1780. orgReaderPos := reader.Pos();
  1781. orgCodePos := emitter.code.pc;
  1782. NextChar;
  1783. (* first we have to find all labels as their names might collide with symbol names *)
  1784. FindLabels;
  1785. FOR pass := 1 TO maxPasses DO (*! currently maxPasses = 1 *)
  1786. Reset;
  1787. times := 1;
  1788. prevPC := emitter.code.pc;
  1789. currentLabel := NIL;
  1790. absoluteMode := FALSE;
  1791. orgOffset := 0;
  1792. NextSymbol;
  1793. IF (scope # NIL) THEN
  1794. IF symbol # symLBrace THEN
  1795. (* treat CPU options as an optional limitation and not vice versa *)
  1796. ELSE
  1797. emitter.cpuOptions := {};
  1798. NextSymbol;
  1799. (* parse code flags such as {SYSTEM.i386 .... } *)
  1800. LOOP
  1801. IF ~Ensure (symIdent, 551) THEN
  1802. RETURN
  1803. END;
  1804. IF ident # "SYSTEM" THEN
  1805. Error("unsupported target identifier");
  1806. RETURN
  1807. END;
  1808. IF symbol # symPeriod THEN
  1809. Error("identifier expected");
  1810. RETURN;
  1811. END;
  1812. IF ~GetCPU (TRUE) THEN
  1813. RETURN;
  1814. END;
  1815. IF symbol = symRBrace THEN
  1816. EXIT
  1817. ELSIF symbol = symComma THEN
  1818. NextSymbol
  1819. ELSE
  1820. Error("target specifier expected");
  1821. RETURN;
  1822. END;
  1823. END;
  1824. NextSymbol;
  1825. END
  1826. END;
  1827. LOOP
  1828. IF symbol = symLn THEN
  1829. NextSymbol;
  1830. ELSIF symbol = symLabel THEN
  1831. currentLabel := labels.Find(idents);
  1832. ASSERT(currentLabel # NIL);
  1833. IF absoluteMode THEN
  1834. currentLabel.SetOffset(absoluteOffset);
  1835. ELSE
  1836. currentLabel.SetOffset(emitter.code.pc)
  1837. END;
  1838. NextSymbol;
  1839. ELSIF symbol = symIdent THEN
  1840. IF ident = "END" THEN
  1841. symbol := symNone;
  1842. ELSIF ident = "BITS" THEN
  1843. NextSymbol;
  1844. IF ~Ensure (symNumber, 553) OR ~emitter.SetBits (val) THEN
  1845. SkipLine;
  1846. ELSE
  1847. NextSymbol;
  1848. END;
  1849. ELSIF ident = "ALIGN" THEN
  1850. NextSymbol;
  1851. IF Expression(alignment, TRUE, type) THEN
  1852. Align(alignment);
  1853. END;
  1854. ELSIF ~(scope # NIL) & (ident = "CPU") THEN
  1855. IF ~GetCPU (FALSE) THEN
  1856. SkipLine;
  1857. END;
  1858. ELSIF ~(scope # NIL) & (ident = "ABSOLUTE") THEN
  1859. absoluteMode := TRUE;
  1860. NextSymbol;
  1861. IF ~Expression (absoluteOffset, TRUE,type) THEN
  1862. SkipLine;
  1863. END;
  1864. ELSIF ~(scope # NIL) & (ident = "ORG") THEN
  1865. NextSymbol;
  1866. IF (orgOffset # 0) OR ~Expression (orgOffset, TRUE,type) THEN
  1867. SkipLine;
  1868. END;
  1869. ELSIF ~(scope # NIL) & (ident = "RESB") THEN
  1870. NextSymbol;
  1871. IF ~Reserve (1) THEN SkipLine END;
  1872. ELSIF ~(scope # NIL) & (ident = "RESW") THEN
  1873. NextSymbol;
  1874. IF ~Reserve (2) THEN SkipLine END;
  1875. ELSIF ~(scope # NIL) & (ident = "RESD") THEN
  1876. NextSymbol;
  1877. IF ~Reserve (4) THEN SkipLine END;
  1878. (*
  1879. ELSIF ident = "EQU" THEN
  1880. IF currentLabel # NIL THEN
  1881. NextSymbol;
  1882. IF Expression (val2, FALSE) THEN
  1883. currentLabel.pc := val2;
  1884. currentLabel.equ := TRUE;
  1885. ELSE
  1886. SkipLine;
  1887. END;
  1888. ELSE
  1889. Error("???");
  1890. RETURN;
  1891. END;
  1892. *)
  1893. ELSIF ident = "TIMES" THEN
  1894. NextSymbol;
  1895. IF ~Expression (times, TRUE,type) THEN
  1896. SkipLine;
  1897. ELSIF times < 0 THEN
  1898. Error("unsupported negative value"); RETURN;
  1899. ELSE
  1900. prevPC := emitter.code.pc;
  1901. END;
  1902. ELSIF ident = "DB" THEN
  1903. IF ~PutData (bits8) THEN SkipLine END;
  1904. ELSIF ident = "DW" THEN
  1905. IF ~PutData (bits16) THEN SkipLine END;
  1906. ELSIF ident = "DD" THEN
  1907. IF ~PutData (bits32) THEN SkipLine END;
  1908. ELSIF ident = "REP" THEN
  1909. NextSymbol;
  1910. emitter.code.PutByte (InstructionSet.prfREP);
  1911. ELSIF ident = "LOCK" THEN
  1912. NextSymbol;
  1913. emitter.code.PutByte (InstructionSet.prfLOCK);
  1914. ELSIF ident = "REPE" THEN
  1915. NextSymbol;
  1916. emitter.code.PutByte (InstructionSet.prfREPE);
  1917. ELSIF ident = "REPZ" THEN
  1918. NextSymbol;
  1919. emitter.code.PutByte (InstructionSet.prfREPZ);
  1920. ELSIF ident = "REPNE" THEN
  1921. NextSymbol;
  1922. emitter.code.PutByte (InstructionSet.prfREPNE);
  1923. ELSIF ident = "REPNZ" THEN
  1924. NextSymbol;
  1925. emitter.code.PutByte (InstructionSet.prfREPNZ);
  1926. ELSIF ~GetInstruction () THEN
  1927. SkipLine
  1928. END;
  1929. currentLabel := NIL;
  1930. ELSIF (symbol = symNone) OR (symbol = symEnd) THEN
  1931. EXIT
  1932. ELSE
  1933. Error("identifier expected");
  1934. RETURN;
  1935. END;
  1936. END;
  1937. END;
  1938. (*
  1939. FixupLabels();
  1940. *)
  1941. (*! FixupLabels(labels.first,code) *)
  1942. sourceName := prevSourceName;
  1943. emitter.cpuBits := prevCpuBits;
  1944. emitter.cpuOptions := prevCpuOptions;
  1945. END Assemble;
  1946. END Assembly;
  1947. VAR kernelWriter: Streams.Writer;
  1948. PROCEDURE Ord (ch: CHAR): INTEGER;
  1949. BEGIN RETURN ORD (ch) - ORD ("0")
  1950. END Ord;
  1951. PROCEDURE HexOrd (ch: CHAR): INTEGER;
  1952. BEGIN
  1953. IF ch <= "9" THEN RETURN ORD (ch) - ORD ("0")
  1954. ELSE RETURN ORD (CAP (ch)) - ORD ("A") + 10
  1955. END
  1956. END HexOrd;
  1957. PROCEDURE IsRegisterOperand*(CONST op: Operand): BOOLEAN;
  1958. BEGIN
  1959. RETURN op.type IN {reg8, reg16, reg32, reg64, CRn, DRn, segReg, sti, mmx, xmm}
  1960. END IsRegisterOperand;
  1961. PROCEDURE IsMemoryOperand*(CONST op: Operand): BOOLEAN;
  1962. BEGIN RETURN op.type = mem
  1963. END IsMemoryOperand;
  1964. PROCEDURE IsImmediateOperand*(CONST op: Operand): BOOLEAN;
  1965. BEGIN RETURN op.type = imm
  1966. END IsImmediateOperand;
  1967. PROCEDURE DumpType*(w: Streams.Writer; type: LONGINT);
  1968. BEGIN
  1969. CASE type OF
  1970. reg8: w.String("reg8")
  1971. |reg16: w.String("reg16");
  1972. |reg32: w.String("reg32");
  1973. |reg64: w.String("reg64");
  1974. |CRn: w.String("CRn");
  1975. |DRn: w.String("DRn");
  1976. |segReg: w.String("segReg");
  1977. |mmx: w.String("mmx");
  1978. |xmm: w.String("xmm");
  1979. |mem: w.String("mem");
  1980. |sti: w.String("sti");
  1981. |imm: w.String("imm");
  1982. |ioffset: w.String("ioffset");
  1983. |pntr1616: w.String("pntr1616");
  1984. |pntr1632: w.String("pntr1632");
  1985. ELSE
  1986. w.String("?"); w.Int(type,1); w.String("?");
  1987. END;
  1988. END DumpType;
  1989. PROCEDURE DumpOperand*(w: Streams.Writer; CONST operand: Operand);
  1990. BEGIN
  1991. CASE operand.type OF
  1992. |reg8, reg16, reg32, reg64, CRn, DRn, segReg, sti, mmx, xmm:
  1993. w.String(InstructionSet.registers[operand.register].name);
  1994. |mem:
  1995. IF operand.sizeInBytes = 1 THEN w.String("BYTE ")
  1996. ELSIF operand.sizeInBytes= 2 THEN w.String("WORD ")
  1997. ELSIF operand.sizeInBytes = 4 THEN w.String("DWORD ")
  1998. ELSIF operand.sizeInBytes = 8 THEN w.String("QWORD ")
  1999. END;
  2000. w.String("[");
  2001. IF operand.register # none THEN
  2002. w.String(InstructionSet.registers[operand.register].name);
  2003. IF operand.index # none THEN w.String("+") END;
  2004. END;
  2005. IF operand.index # none THEN
  2006. w.String(InstructionSet.registers[operand.index].name);
  2007. IF operand.scale # 1 THEN
  2008. w.String("*"); w.Int(operand.scale,1);
  2009. END;
  2010. END;
  2011. IF operand.symbol.name # "" THEN
  2012. Basic.WriteSegmentedName(w, operand.symbol.name); w.String(":"); w.Int(operand.displacement,1);
  2013. IF operand.symbolOffset # 0 THEN w.String("(@"); w.Int(operand.symbolOffset,1); w.String(")") END;
  2014. ELSIF operand.displacement # 0 THEN
  2015. IF (operand.displacement > 0) & ((operand.register # none) OR (operand.index # none)) THEN w.String("+");END;
  2016. w.Int(operand.displacement,1);
  2017. END;
  2018. w.String("]");
  2019. |imm,ioffset:
  2020. IF operand.symbol.name # "" THEN
  2021. Basic.WriteSegmentedName(w, operand.symbol.name); w.String(":"); w.Int(operand.displacement,1);
  2022. IF operand.symbolOffset # 0 THEN w.String("(@"); w.Int(operand.symbolOffset,1); w.String(")") END;
  2023. ELSE
  2024. IF (operand.val > MAX(LONGINT)) OR (operand.val < MIN(LONGINT)) THEN
  2025. w.Hex(operand.val,1); w.String("H");
  2026. ELSE
  2027. w.Int(SHORT(operand.val),1);
  2028. END;
  2029. END;
  2030. |pntr1616:
  2031. |pntr1632:
  2032. ELSE
  2033. HALT(100)
  2034. END;
  2035. END DumpOperand;
  2036. PROCEDURE DumpInstruction(w: Streams.Writer; mnemonic: LONGINT; CONST operands: ARRAY OF Operand);
  2037. VAR i: LONGINT;
  2038. CONST DebugSize = FALSE;
  2039. BEGIN
  2040. IF mnemonic # none THEN
  2041. w.String(InstructionSet.mnemonics[mnemonic].name);
  2042. i := 0;
  2043. WHILE(i<maxNumberOperands) & (operands[i].type # none) DO
  2044. IF i = 0 THEN w.Char(09X) ELSE w.String(", ") END;
  2045. DumpOperand(w,operands[i]);
  2046. IF DebugSize THEN
  2047. w.String("(*"); DumpType(w,operands[i].type); w.String(":"); w.Int(operands[i].sizeInBytes,1); w.String("*)");
  2048. END;
  2049. INC(i);
  2050. END;
  2051. w.String("; ");
  2052. END;
  2053. END DumpInstruction;
  2054. PROCEDURE Matches(CONST operand: Operand; type: InstructionSet.OperandType): BOOLEAN;
  2055. PROCEDURE IsMemReg(regIndex: LONGINT): BOOLEAN;
  2056. BEGIN
  2057. RETURN InstructionSet.RegisterType(regIndex) IN {reg16, reg32, reg64}
  2058. END IsMemReg;
  2059. BEGIN
  2060. CASE operand.type OF
  2061. |reg8:
  2062. CASE type OF
  2063. InstructionSet.reg8, InstructionSet.regmem8:
  2064. RETURN TRUE;
  2065. | InstructionSet.AL, InstructionSet.rAX:
  2066. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2067. | InstructionSet.CL:
  2068. RETURN InstructionSet.RegisterIndex(operand.register) = RCX;
  2069. ELSE
  2070. RETURN FALSE;
  2071. END;
  2072. |reg16:
  2073. CASE type OF
  2074. InstructionSet.reg16, InstructionSet.regmem16:
  2075. RETURN TRUE;
  2076. | InstructionSet.AX, InstructionSet.rAX:
  2077. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2078. | InstructionSet.DX:
  2079. RETURN InstructionSet.RegisterIndex(operand.register) = RDX;
  2080. ELSE
  2081. RETURN FALSE;
  2082. END;
  2083. |reg32:
  2084. CASE type OF
  2085. InstructionSet.reg32, InstructionSet.regmem32:
  2086. RETURN TRUE;
  2087. | InstructionSet.EAX, InstructionSet.rAX:
  2088. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2089. ELSE
  2090. RETURN FALSE;
  2091. END;
  2092. |reg64:
  2093. CASE type OF
  2094. InstructionSet.reg64, InstructionSet.regmem64:
  2095. RETURN TRUE;
  2096. | InstructionSet.RAX, InstructionSet.rAX:
  2097. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2098. ELSE
  2099. RETURN FALSE;
  2100. END;
  2101. |CRn:
  2102. CASE type OF
  2103. InstructionSet.CRn:
  2104. RETURN TRUE;
  2105. | InstructionSet.CR8:
  2106. RETURN InstructionSet.RegisterIndex(operand.register) = 8;
  2107. ELSE
  2108. RETURN FALSE;
  2109. END;
  2110. |DRn:
  2111. RETURN type = InstructionSet.DRn;
  2112. |segReg:
  2113. CASE type OF
  2114. InstructionSet.segReg:
  2115. RETURN TRUE;
  2116. | InstructionSet.ES:
  2117. RETURN InstructionSet.RegisterIndex(operand.register) = segES;
  2118. | InstructionSet.CS:
  2119. RETURN InstructionSet.RegisterIndex(operand.register) = segCS;
  2120. | InstructionSet.SS:
  2121. RETURN InstructionSet.RegisterIndex(operand.register) = segSS;
  2122. | InstructionSet.DS:
  2123. RETURN InstructionSet.RegisterIndex(operand.register) = segDS;
  2124. | InstructionSet.FS:
  2125. RETURN InstructionSet.RegisterIndex(operand.register) = segFS;
  2126. | InstructionSet.GS:
  2127. RETURN InstructionSet.RegisterIndex(operand.register) = segGS;
  2128. ELSE
  2129. RETURN FALSE;
  2130. END
  2131. |sti:
  2132. CASE type OF
  2133. InstructionSet.sti:
  2134. RETURN TRUE;
  2135. | InstructionSet.st0:
  2136. RETURN InstructionSet.RegisterIndex(operand.register) = 0;
  2137. ELSE
  2138. RETURN FALSE;
  2139. END
  2140. |mmx:
  2141. CASE type OF
  2142. InstructionSet.mmx, InstructionSet.mmxmem32, InstructionSet.mmxmem64:
  2143. RETURN TRUE;
  2144. ELSE
  2145. RETURN FALSE;
  2146. END
  2147. |xmm:
  2148. CASE type OF
  2149. InstructionSet.xmm, InstructionSet.xmmmem32, InstructionSet.xmmmem64, InstructionSet.xmmmem128:
  2150. RETURN TRUE;
  2151. ELSE
  2152. RETURN FALSE;
  2153. END
  2154. |mem:
  2155. CASE type OF
  2156. | InstructionSet.mem:
  2157. RETURN TRUE;
  2158. | InstructionSet.mem8:
  2159. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8);
  2160. | InstructionSet.regmem8:
  2161. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2162. | InstructionSet.mem16:
  2163. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16);
  2164. | InstructionSet.regmem16:
  2165. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2166. | InstructionSet.mem32:
  2167. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32);
  2168. | InstructionSet.regmem32, InstructionSet.mmxmem32, InstructionSet.xmmmem32:
  2169. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2170. | InstructionSet.mem64:
  2171. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64);
  2172. | InstructionSet.regmem64, InstructionSet.mmxmem64, InstructionSet.xmmmem64:
  2173. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2174. | InstructionSet.mem128:
  2175. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits128);
  2176. | InstructionSet.xmmmem128:
  2177. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits128)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2178. | InstructionSet.moffset8:
  2179. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.register= none);
  2180. | InstructionSet.moffset16:
  2181. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.register= none);
  2182. | InstructionSet.moffset32:
  2183. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) & (operand.register= none);
  2184. | InstructionSet.moffset64:
  2185. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64)) & (operand.register= none);
  2186. ELSE
  2187. RETURN FALSE;
  2188. END;
  2189. |imm,ioffset:
  2190. CASE type OF
  2191. InstructionSet.one:
  2192. RETURN operand.val = 1
  2193. | InstructionSet.three:
  2194. RETURN operand.val = 3
  2195. | InstructionSet.rel8off:
  2196. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)
  2197. | InstructionSet.imm8:
  2198. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.val >= -80H) & (operand.val < 100H)
  2199. | InstructionSet.simm8:
  2200. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.val >= -80H) & (operand.val < 80H)
  2201. | InstructionSet.uimm8:
  2202. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.val >= 0H) & (operand.val < 100H)
  2203. | InstructionSet.rel16off:
  2204. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16) & FALSE (* do not allow 16 bit jumps *)
  2205. | InstructionSet.imm16:
  2206. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.val >= -8000H) & (operand.val < 10000H)
  2207. | InstructionSet.simm16:
  2208. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.val >= -8000H) & (operand.val < 8000H)
  2209. | InstructionSet.uimm16:
  2210. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.val >= 0H) & (operand.val < 10000H)
  2211. | InstructionSet.rel32off:
  2212. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32) (* & & (operand.val >= -80000000H) & (operand.val < 100000000H) PACO confused? *)
  2213. | InstructionSet.imm32:
  2214. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) (* & & (operand.val >= -80000000H) & (operand.val < 100000000H) PACO confused? *)
  2215. | InstructionSet.simm32:
  2216. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) (* & & (operand.val >= -80000000H) & (operand.val < 80000000H) PACO confused? *)
  2217. | InstructionSet.uimm32:
  2218. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) & (operand.val >= 0H) (* & (operand.val < 100000000H) PACO confused? *)
  2219. | InstructionSet.imm64:
  2220. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64)
  2221. ELSE
  2222. RETURN FALSE
  2223. END
  2224. |pntr1616:
  2225. RETURN type = InstructionSet.pntr1616;
  2226. |pntr1632:
  2227. RETURN type = InstructionSet.pntr1632;
  2228. ELSE
  2229. HALT(100)
  2230. END;
  2231. END Matches;
  2232. PROCEDURE ValueInByteRange (value: HUGEINT): BOOLEAN;
  2233. BEGIN RETURN SYSTEM.VAL (SHORTINT, value) = value
  2234. END ValueInByteRange;
  2235. PROCEDURE ValueInWordRange (value: HUGEINT): BOOLEAN;
  2236. BEGIN RETURN SYSTEM.VAL (INTEGER, value) = value
  2237. END ValueInWordRange;
  2238. PROCEDURE InitOperand*(VAR operand: Operand);
  2239. BEGIN
  2240. operand.type := none;
  2241. operand.index := none;
  2242. operand.register:= none;
  2243. operand.segment:= none;
  2244. operand.sizeInBytes := none;
  2245. operand.scale := 1;
  2246. operand.displacement := 0;
  2247. operand.val := 0;
  2248. operand.pc := none;
  2249. operand.symbol.name := "";
  2250. operand.symbol.fingerprint := 0;
  2251. operand.selector := none;
  2252. operand.offset := 0;
  2253. END InitOperand;
  2254. PROCEDURE InitRegister* (VAR operand: Operand; register: Register);
  2255. BEGIN
  2256. InitOperand(operand);
  2257. operand.type := InstructionSet.RegisterType(register);
  2258. operand.register :=register;
  2259. CASE operand.type OF
  2260. reg8,reg16,reg32,reg64,segReg,CRn,DRn,sti,xmm,mmx: (* ok *)
  2261. |InstructionSet.st0: operand.type := InstructionSet.sti;
  2262. ELSE
  2263. HALT(100);
  2264. END;
  2265. operand.sizeInBytes := InstructionSet.registers[register].sizeInBytes
  2266. END InitRegister;
  2267. PROCEDURE NewRegister*(register: Register): Operand;
  2268. VAR operand: Operand;
  2269. BEGIN InitRegister(operand,register); RETURN operand
  2270. END NewRegister;
  2271. PROCEDURE InitMem*(VAR operand: Operand; size: Size; reg: Register; displacement: LONGINT);
  2272. BEGIN
  2273. InitOperand(operand);
  2274. operand.type := mem;
  2275. operand.sizeInBytes := size;
  2276. operand.register:= reg;
  2277. operand.displacement := displacement;
  2278. operand.scale := 1;
  2279. END InitMem;
  2280. PROCEDURE SetIndexScale*(VAR operand: Operand; index: Register; scale: LONGINT);
  2281. BEGIN
  2282. operand.index := index;
  2283. operand.scale := scale
  2284. END SetIndexScale;
  2285. PROCEDURE NewMem*(size: Size; reg: Register; displacement: LONGINT): Operand;
  2286. VAR operand: Operand;
  2287. BEGIN
  2288. InitMem(operand,size,reg,displacement); RETURN operand
  2289. END NewMem;
  2290. PROCEDURE InitMem8* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2291. BEGIN InitMem (operand, bits8, reg, displacement);
  2292. END InitMem8;
  2293. PROCEDURE NewMem8* (reg: Register; displacement: LONGINT): Operand;
  2294. VAR operand: Operand;
  2295. BEGIN InitMem8 (operand,reg, displacement); RETURN operand
  2296. END NewMem8;
  2297. PROCEDURE InitMem16* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2298. BEGIN InitMem (operand,bits16, reg, displacement);
  2299. END InitMem16;
  2300. PROCEDURE NewMem16* (reg: Register; displacement: LONGINT): Operand;
  2301. VAR operand: Operand;
  2302. BEGIN InitMem16 (operand,reg, displacement); RETURN operand
  2303. END NewMem16;
  2304. PROCEDURE InitMem32* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2305. BEGIN InitMem (operand,bits32, reg, displacement);
  2306. END InitMem32;
  2307. PROCEDURE NewMem32* (reg: Register; displacement: LONGINT): Operand;
  2308. VAR operand: Operand;
  2309. BEGIN InitMem32 (operand,reg, displacement); RETURN operand
  2310. END NewMem32;
  2311. PROCEDURE InitMem64* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2312. BEGIN InitMem (operand,bits64, reg, displacement);
  2313. END InitMem64;
  2314. PROCEDURE NewMem64* (reg: Register; displacement: LONGINT): Operand;
  2315. VAR operand: Operand;
  2316. BEGIN InitMem64 (operand,reg, displacement); RETURN operand
  2317. END NewMem64;
  2318. PROCEDURE InitMem128* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2319. BEGIN InitMem (operand,bits128, reg, displacement);
  2320. END InitMem128;
  2321. PROCEDURE NewMem128* (reg: Register; displacement: LONGINT): Operand;
  2322. VAR operand: Operand;
  2323. BEGIN InitMem128 (operand,reg, displacement); RETURN operand
  2324. END NewMem128;
  2325. PROCEDURE SetSymbol*(VAR operand: Operand; symbol: Sections.SectionName; fingerprint: LONGINT; symbolOffset, displacement: LONGINT);
  2326. BEGIN
  2327. operand.symbol.name := symbol;
  2328. operand.symbol.fingerprint := fingerprint;
  2329. operand.symbolOffset := symbolOffset; operand.displacement := displacement;
  2330. END SetSymbol;
  2331. PROCEDURE InitImm* (VAR operand: Operand; size: SHORTINT; val: HUGEINT);
  2332. BEGIN InitOperand(operand); operand.type := imm; operand.sizeInBytes := size; operand.val := val;
  2333. END InitImm;
  2334. PROCEDURE InitImm8* (VAR operand: Operand; val: HUGEINT);
  2335. BEGIN InitImm (operand, bits8, val);
  2336. END InitImm8;
  2337. PROCEDURE NewImm8*(val: HUGEINT): Operand;
  2338. VAR operand: Operand;
  2339. BEGIN InitImm8(operand,val); RETURN operand
  2340. END NewImm8;
  2341. PROCEDURE InitImm16* (VAR operand: Operand; val: HUGEINT);
  2342. BEGIN InitImm (operand, bits16, val);
  2343. END InitImm16;
  2344. PROCEDURE NewImm16*(val: HUGEINT): Operand;
  2345. VAR operand:Operand;
  2346. BEGIN InitImm16(operand,val); RETURN operand
  2347. END NewImm16;
  2348. PROCEDURE InitImm32* (VAR operand: Operand; val: HUGEINT);
  2349. BEGIN InitImm (operand, bits32, val);
  2350. END InitImm32;
  2351. PROCEDURE NewImm32*(val: HUGEINT): Operand;
  2352. VAR operand: Operand;
  2353. BEGIN InitImm32(operand,val); RETURN operand
  2354. END NewImm32;
  2355. PROCEDURE InitImm64* (VAR operand: Operand; val: HUGEINT);
  2356. BEGIN InitImm (operand, bits64, val);
  2357. END InitImm64;
  2358. PROCEDURE NewImm64*(val: HUGEINT): Operand;
  2359. VAR operand: Operand;
  2360. BEGIN InitImm64(operand,val); RETURN operand
  2361. END NewImm64;
  2362. PROCEDURE InitOffset* (VAR operand: Operand; size: SHORTINT; val: HUGEINT);
  2363. BEGIN InitOperand(operand); operand.type := ioffset; operand.sizeInBytes := size; operand.val := val;
  2364. END InitOffset;
  2365. PROCEDURE InitOffset8* (VAR operand: Operand; val: HUGEINT);
  2366. BEGIN InitOffset (operand, bits8, val);
  2367. END InitOffset8;
  2368. PROCEDURE NewOffset8*(val: HUGEINT): Operand;
  2369. VAR operand: Operand;
  2370. BEGIN InitOffset8(operand,val); RETURN operand
  2371. END NewOffset8;
  2372. PROCEDURE InitOffset16* (VAR operand: Operand; val: HUGEINT);
  2373. BEGIN InitOffset (operand, bits16, val);
  2374. END InitOffset16;
  2375. PROCEDURE NewOffset16*(val: HUGEINT): Operand;
  2376. VAR operand: Operand;
  2377. BEGIN InitOffset16(operand,val); RETURN operand
  2378. END NewOffset16;
  2379. PROCEDURE InitOffset32* (VAR operand: Operand; val: HUGEINT);
  2380. BEGIN InitOffset (operand, bits32, val);
  2381. END InitOffset32;
  2382. PROCEDURE NewOffset32*(val: HUGEINT): Operand;
  2383. VAR operand: Operand;
  2384. BEGIN InitOffset32(operand,val); RETURN operand
  2385. END NewOffset32;
  2386. PROCEDURE InitOffset64* (VAR operand: Operand; val: HUGEINT);
  2387. BEGIN InitOffset (operand, bits64, val);
  2388. END InitOffset64;
  2389. PROCEDURE NewOffset64*(val: HUGEINT): Operand;
  2390. VAR operand: Operand;
  2391. BEGIN InitOffset64(operand,val); RETURN operand
  2392. END NewOffset64;
  2393. PROCEDURE InitPntr1616* (VAR operand: Operand; s, o: LONGINT);
  2394. BEGIN InitOperand(operand); operand.type := pntr1616; operand.selector := s; operand.offset := o;
  2395. END InitPntr1616;
  2396. PROCEDURE InitPntr1632* (VAR operand: Operand; s, o: LONGINT);
  2397. BEGIN InitOperand(operand); operand.type := pntr1632; operand.selector := s; operand.offset := o;
  2398. END InitPntr1632;
  2399. PROCEDURE SetSize*(VAR operand: Operand;sizeInBytes: Size);
  2400. BEGIN operand.sizeInBytes := sizeInBytes
  2401. END SetSize;
  2402. PROCEDURE SameOperand*(CONST left,right: Operand): BOOLEAN;
  2403. BEGIN
  2404. IF (left.type # right.type) OR (left.sizeInBytes # right.sizeInBytes) OR (left.symbol # right.symbol) THEN RETURN FALSE END;
  2405. CASE left.type OF
  2406. reg8,reg16,reg32,reg64,segReg,CRn,DRn,sti,xmm,mmx: RETURN left.register = right.register
  2407. | imm,ioffset: RETURN (left.val = right.val) & ((left.symbol.name="") OR (left.displacement = right.displacement))
  2408. | mem:RETURN (left.register = right.register) & (left.displacement = right.displacement) & (left.index = right.index) & (left.scale = right.scale)
  2409. | pntr1616,pntr1632: RETURN (left.selector=right.selector) & (left.offset=right.offset)
  2410. END;
  2411. RETURN FALSE
  2412. END SameOperand;
  2413. PROCEDURE Test*(context: Commands.Context);
  2414. VAR assembly: Emitter;
  2415. (*errorHandler: ErrorHandler; *)
  2416. op1,op2,op3: Operand;
  2417. diagnostics: Diagnostics.StreamDiagnostics;
  2418. code: Code;
  2419. pooledName: Basic.SegmentedName;
  2420. PROCEDURE Op(CONST name: ARRAY OF CHAR): LONGINT;
  2421. BEGIN
  2422. RETURN InstructionSet.FindMnemonic(name)
  2423. END Op;
  2424. BEGIN
  2425. InitOperand(op1); InitOperand(op2); InitOperand(op3);
  2426. NEW(diagnostics,context.error);
  2427. Basic.ToSegmentedName("test", pooledName);
  2428. NEW(code,Sections.CodeSection,8,0,pooledName,TRUE,TRUE);
  2429. NEW(assembly,diagnostics);
  2430. assembly.SetCode(code);
  2431. InitRegister(op1,InstructionSet.regEAX);
  2432. InitImm32(op2,10);
  2433. assembly.Emit2(Op("MOV"),op1,op2);
  2434. context.out.Update;
  2435. code.Dump(context.out);
  2436. END Test;
  2437. BEGIN
  2438. IF Trace THEN
  2439. NEW(kernelWriter,KernelLog.Send,1000);
  2440. END;
  2441. END FoxAMD64Assembler.
  2442. OCAMD64Assembler.Test ~