FoxAMD64Assembler.Mod 91 KB

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