FoxAMD64Assembler.Mod 91 KB

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