FoxAMD64Assembler.Mod 90 KB

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