FoxAMD64Assembler.Mod 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795
  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 = Diagnostics.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, val2, val3: LONGINT;
  1069. currentLabel: NamedLabel;
  1070. labels: NamedLabelList;
  1071. prevPC: LONGINT;
  1072. pass: LONGINT;
  1073. absoluteMode: BOOLEAN;
  1074. absoluteOffset: LONGINT;
  1075. alignment: LONGINT;
  1076. orgOffset: LONGINT;
  1077. char: CHAR;
  1078. orgReaderPos: LONGINT;
  1079. orgCodePos: LONGINT;
  1080. prevSourceName: Basic.FileName;
  1081. position: Basic.Position;
  1082. prevCpuBits: Size;
  1083. prevCpuOptions: InstructionSet.CPUOptions;
  1084. prevAssembly: Assembly;
  1085. PROCEDURE NextChar;
  1086. BEGIN
  1087. (*
  1088. IF (dump # NIL) & (pass = maxPasses) THEN dump.Char (char) END;
  1089. *)
  1090. reader.Char(char); INC(position.start);
  1091. END NextChar;
  1092. PROCEDURE SkipBlanks;
  1093. BEGIN
  1094. (* tf returns 01X when an embedded object is encountered *)
  1095. WHILE (char = SPACE) OR (char = TAB) OR (char = 01X) DO NextChar END;
  1096. IF char = ";" THEN
  1097. WHILE (char # CR) & (char # LF) & (char # 0X) DO NextChar END (* Skip comments *)
  1098. END;
  1099. END SkipBlanks;
  1100. PROCEDURE GetNumber (VAR intval: LONGINT);
  1101. VAR i, m, n: INTEGER; dig: ARRAY 24 OF CHAR;
  1102. BEGIN
  1103. i := 0; m := 0; n := 0;
  1104. WHILE ('0' <= char) & (char <= '9') OR ('A' <= CAP (char)) & (CAP (char) <= 'F') DO
  1105. IF (m > 0) OR (char # "0") THEN (* ignore leading zeros *)
  1106. IF n < LEN(dig) THEN dig[n] := char; INC(n) END;
  1107. INC(m)
  1108. END;
  1109. NextChar; INC(i)
  1110. END;
  1111. IF n = m THEN intval := 0; i := 0;
  1112. IF (CAP (char) = "H") OR (char = "X") THEN NextChar;
  1113. IF (n = Scanner.MaxHexDigits) & (dig[0] > "7") THEN (* prevent overflow *) intval := -1 END;
  1114. WHILE i < n DO intval := intval * 10H + HexOrd (dig[i]); INC(i) END;
  1115. ELSE
  1116. IF (n = Scanner.MaxHugeHexDigits) & (dig[0] > "7") THEN (* prevent overflow *) intval := -1 END;
  1117. WHILE i < n DO intval := intval * 10 + Ord (dig[i]); INC(i) END
  1118. END
  1119. END;
  1120. END GetNumber;
  1121. PROCEDURE GetIdentifier;
  1122. VAR i: LONGINT;
  1123. BEGIN
  1124. i := 0;
  1125. REPEAT
  1126. IF i < Scanner.MaxIdentifierLength - 1 THEN
  1127. IF ('0' <= char) & (char <= '9') THEN
  1128. ident[i] := char; idents[i] := char;
  1129. ELSE
  1130. ident[i] := (* CAP *) (char); idents[i] := char; END;
  1131. INC (i);
  1132. END;
  1133. NextChar
  1134. UNTIL ~( ('A' <= CAP(char)) & (CAP(char) <= 'Z') OR ('0' <= char) & (char <= '9') OR (char = '_') );
  1135. ident[i] := 0X; idents[i] := 0X;
  1136. END GetIdentifier;
  1137. PROCEDURE GetString;
  1138. VAR i: LONGINT;
  1139. BEGIN
  1140. i := 0;
  1141. NextChar;
  1142. WHILE (char # "'") & (i < Scanner.MaxIdentifierLength - 1) DO
  1143. ident[i] := char; INC (i);
  1144. NextChar;
  1145. END;
  1146. ident[i] := 0X;
  1147. NextChar;
  1148. END GetString;
  1149. PROCEDURE NextSymbol;
  1150. BEGIN
  1151. SkipBlanks;
  1152. errPos := position;
  1153. CASE char OF
  1154. 'A' .. 'Z', 'a' .. 'z', '_' :
  1155. GetIdentifier;
  1156. SkipBlanks;
  1157. IF char = ':' THEN
  1158. NextChar; symbol := symLabel;
  1159. ELSE
  1160. symbol := symIdent;
  1161. END;
  1162. | '0' .. '9':
  1163. GetNumber (val);
  1164. symbol := symNumber;
  1165. | "'": GetString;
  1166. symbol := symString;
  1167. | '.': symbol := symPeriod;
  1168. NextChar;
  1169. | ';': symbol := symSemicolon;
  1170. NextChar;
  1171. | ':': symbol := symColon;
  1172. NextChar;
  1173. | CR: symbol := symLn;
  1174. NextChar; INC(position.line);
  1175. position.linepos := position.start;
  1176. IF char = LF THEN NextChar END;
  1177. | LF: symbol := symLn;
  1178. NextChar;INC(position.line);
  1179. position.linepos := position.start;
  1180. IF char = CR THEN NextChar END;
  1181. | ',': symbol := symComma;
  1182. NextChar;
  1183. | '+': symbol := symPlus;
  1184. NextChar;
  1185. | '-': symbol := symMinus;
  1186. NextChar;
  1187. | '*': symbol := symTimes;
  1188. NextChar;
  1189. | '/': symbol := symDiv;
  1190. NextChar;
  1191. | '%': symbol := symMod;
  1192. NextChar;
  1193. | '~': symbol := symNegate;
  1194. NextChar;
  1195. | '(': symbol := symLParen;
  1196. NextChar;
  1197. | ')': symbol := symRParen;
  1198. NextChar;
  1199. | '[': symbol := symLBraket;
  1200. NextChar;
  1201. | ']': symbol := symRBraket;
  1202. NextChar;
  1203. | '{': symbol := symLBrace;
  1204. NextChar;
  1205. | '}': symbol := symRBrace;
  1206. NextChar;
  1207. | '@': symbol := symAt;
  1208. NextChar;
  1209. | '$': NextChar;
  1210. IF char = '$' THEN
  1211. symbol := symPCOffset; NextChar;
  1212. ELSE
  1213. symbol := symPC;
  1214. END
  1215. | 0X: symbol := symEnd;
  1216. ELSE
  1217. symbol := symNone;
  1218. NextChar;
  1219. END;
  1220. END NextSymbol;
  1221. PROCEDURE SkipLine;
  1222. BEGIN
  1223. WHILE (symbol # symLn) & (symbol # symNone) DO
  1224. NextSymbol;
  1225. END;
  1226. END SkipLine;
  1227. PROCEDURE Ensure (desiredSymbol, errNumber : LONGINT) : BOOLEAN;
  1228. VAR temp: LONGINT;
  1229. BEGIN
  1230. temp := symbol;
  1231. IF symbol = desiredSymbol THEN
  1232. NextSymbol;
  1233. RETURN TRUE;
  1234. ELSE
  1235. Error("other symbol expected");
  1236. RETURN FALSE;
  1237. END;
  1238. END Ensure;
  1239. PROCEDURE GetCPU (cumulateOptions: BOOLEAN): BOOLEAN;
  1240. VAR i: LONGINT;
  1241. BEGIN
  1242. SkipBlanks;
  1243. GetIdentifier;
  1244. Strings.UpperCase(ident);
  1245. i := InstructionSet.FindCPU (ident);
  1246. IF i # InstructionSet.none THEN
  1247. IF cumulateOptions THEN
  1248. emitter.cpuOptions := emitter.cpuOptions + InstructionSet.cpus[i].cpuOptions;
  1249. ELSE
  1250. emitter.cpuOptions := InstructionSet.cpus[i].cpuOptions + InstructionSet.cpuOptions;
  1251. END;
  1252. NextSymbol;
  1253. RETURN TRUE;
  1254. ELSE
  1255. ErrorSS ("cpu unknown",ident);
  1256. emitter.cpuOptions := prevCpuOptions;
  1257. RETURN FALSE;
  1258. END;
  1259. END GetCPU;
  1260. PROCEDURE Factor (VAR x: LONGINT; critical: BOOLEAN; VAR type: SHORTINT): BOOLEAN;
  1261. VAR label: NamedLabel; l: LONGINT;
  1262. BEGIN
  1263. IF symbol = symNumber THEN
  1264. x := val; NextSymbol; RETURN TRUE;
  1265. ELSIF symbol = symPC THEN
  1266. x := (orgOffset + emitter.code.pc ); NextSymbol; RETURN TRUE;
  1267. ELSIF symbol = symPCOffset THEN
  1268. x := orgOffset; NextSymbol; RETURN TRUE;
  1269. ELSIF symbol = symString THEN
  1270. x := 0; l := Strings.Length (ident);
  1271. IF l > 0 THEN INC (x, ORD (ident [0])) END;
  1272. IF l > 1 THEN INC (x, ORD (ident [1])*100H) END;
  1273. IF l > 2 THEN INC (x, ORD (ident [2])*10000H) END;
  1274. IF l > 3 THEN INC (x, ORD (ident [3])*1000000H) END;
  1275. NextSymbol; RETURN TRUE;
  1276. ELSIF symbol = symIdent THEN
  1277. label := labels.Find (idents);
  1278. NextSymbol;
  1279. IF label # NIL THEN
  1280. x := (label.offset );
  1281. type := ioffset;
  1282. currentLabel := label;
  1283. (*
  1284. IF x = MAX(LONGINT) THEN
  1285. x := -label.index;
  1286. currentFixup := in;
  1287. END;
  1288. *)
  1289. RETURN TRUE;
  1290. ELSIF scope # NIL THEN
  1291. IF ~GetValue(idents,x) THEN
  1292. IF (pass = maxPasses) THEN
  1293. Error("constant expected");
  1294. END;
  1295. RETURN FALSE;
  1296. ELSE
  1297. RETURN TRUE;
  1298. END
  1299. END;
  1300. IF (~critical) & (pass # maxPasses) THEN
  1301. x := 0;
  1302. RETURN TRUE
  1303. END;
  1304. Error("undefined symbol");
  1305. RETURN FALSE;
  1306. ELSIF symbol = symLParen THEN
  1307. NextSymbol;
  1308. RETURN Expression (x, critical,type) & Ensure (symRParen, 555);
  1309. END;
  1310. Error("parse error in expression");
  1311. RETURN FALSE
  1312. END Factor;
  1313. PROCEDURE Term (VAR x: LONGINT; critical: BOOLEAN; VAR type: SHORTINT): BOOLEAN;
  1314. VAR y, op : LONGINT;
  1315. BEGIN
  1316. IF Factor (x, critical,type) THEN
  1317. WHILE (symbol = symTimes) OR (symbol = symDiv) OR (symbol = symMod) DO
  1318. op := symbol; NextSymbol;
  1319. IF Factor (y, critical,type) THEN
  1320. IF op = symTimes THEN x := x * y
  1321. ELSIF op = symDiv THEN x := x DIV y
  1322. ELSE x := x MOD y
  1323. END;
  1324. ELSE
  1325. RETURN FALSE;
  1326. END;
  1327. END;
  1328. RETURN TRUE;
  1329. ELSE
  1330. RETURN FALSE;
  1331. END;
  1332. END Term;
  1333. PROCEDURE Expression (VAR x: LONGINT; critical: BOOLEAN; VAR type: SHORTINT): BOOLEAN;
  1334. VAR y, op : LONGINT;
  1335. BEGIN
  1336. IF symbol = symMinus THEN
  1337. op := symbol; NextSymbol;
  1338. IF Term (x, critical,type) THEN
  1339. x := -x
  1340. ELSE
  1341. RETURN FALSE;
  1342. END;
  1343. ELSIF symbol = symPlus THEN
  1344. op := symbol; NextSymbol;
  1345. IF ~Term (x, critical,type) THEN
  1346. RETURN FALSE;
  1347. END;
  1348. ELSIF symbol = symNegate THEN
  1349. op := symbol; NextSymbol;
  1350. IF Term (x, critical,type) THEN
  1351. x := -x - 1
  1352. ELSE
  1353. RETURN FALSE;
  1354. END;
  1355. ELSIF ~Term (x, critical,type) THEN
  1356. RETURN FALSE;
  1357. END;
  1358. WHILE (symbol = symPlus) OR (symbol = symMinus) DO
  1359. op := symbol; NextSymbol;
  1360. IF Term (y, critical,type) THEN
  1361. IF op = symPlus THEN x := x + y ELSE x := x - y END;
  1362. ELSE
  1363. RETURN FALSE;
  1364. END;
  1365. END;
  1366. RETURN TRUE;
  1367. END Expression;
  1368. PROCEDURE Align(size: LONGINT);
  1369. VAR pc: LONGINT;
  1370. BEGIN
  1371. IF size <= 0 THEN Error("invalid alignment size"); RETURN END;
  1372. pc := emitter.code.pc DIV 8; (* bytes *)
  1373. WHILE pc MOD size # 0 DO
  1374. emitter.code.PutByte(0);
  1375. INC(pc);
  1376. END;
  1377. END Align;
  1378. PROCEDURE PutData (size: Size): BOOLEAN;
  1379. VAR i: LONGINT; type:SHORTINT; ofs: Operand;
  1380. BEGIN
  1381. NextSymbol;
  1382. WHILE symbol # symLn DO
  1383. IF symbol = symString THEN
  1384. i := 0;
  1385. WHILE ident[i] # 0X DO
  1386. emitter.code.PutByte (ORD (ident[i]));
  1387. INC (i);
  1388. END;
  1389. IF size # bits8 THEN
  1390. i := (size ) - i MOD (size );
  1391. WHILE i # 0 DO emitter.code.PutByte (0); DEC (i) END;
  1392. END;
  1393. NextSymbol;
  1394. ELSIF (scope # NIL) & (symbol = symAt) THEN
  1395. NextSymbol;
  1396. IF symbol # symIdent THEN Error("identifier missing") END;
  1397. GetOffsetFixup (idents, ofs);
  1398. NextSymbol;
  1399. IF symbol = symPlus THEN
  1400. NextSymbol;
  1401. IF Expression(i, FALSE, type) THEN
  1402. ofs.displacement := i
  1403. END;
  1404. ELSIF symbol = symMinus THEN
  1405. NextSymbol;
  1406. IF Expression(i, FALSE, type) THEN
  1407. ofs.displacement := - i
  1408. END;
  1409. END;
  1410. IF pass = maxPasses THEN
  1411. emitter.AddFixup(BinaryCode.Absolute, ofs.sizeInBytes, emitter.code.pc, ofs.symbol, ofs.symbolOffset,ofs.displacement);
  1412. END;
  1413. emitter.code.PutBytes (0, size );
  1414. ELSIF Expression (i, FALSE,type) THEN
  1415. emitter.code.PutBytes (i, size );
  1416. ELSE
  1417. RETURN FALSE;
  1418. END;
  1419. IF symbol = symComma THEN
  1420. NextSymbol;
  1421. ELSIF symbol # symLn THEN
  1422. Error("operand missing");
  1423. END
  1424. END;
  1425. Duplicate ((emitter.code.pc - prevPC) , NIL);
  1426. RETURN TRUE;
  1427. END PutData;
  1428. PROCEDURE Duplicate (size: LONGINT; fixup: BinaryCode.Fixup);
  1429. VAR i: LONGINT; buffer: ARRAY 100 OF CHAR; pc: LONGINT;
  1430. BEGIN
  1431. IF times = 1 THEN RETURN END;
  1432. pc := (prevPC );
  1433. IF (dump # NIL) & (pass = maxPasses) THEN dump.Hex (emitter.code.pc, 1); dump.Char (' ') END;
  1434. FOR i := 0 TO size - 1 DO
  1435. buffer[i] := emitter.code.GetByte (pc); INC(pc);
  1436. IF (dump # NIL) & (pass = maxPasses) THEN dump.Hex (ORD (buffer[i]), -2); END;
  1437. END;
  1438. pc := (prevPC );
  1439. IF times > 1 THEN
  1440. WHILE times # 1 DO
  1441. IF fixup # NIL THEN
  1442. HALT(200);
  1443. (*!!
  1444. AddFixup (fixup.adr, pc + fixup.offset - prevPC);
  1445. *)
  1446. END;
  1447. FOR i := 0 TO size - 1 DO
  1448. emitter.code.PutByteAt (pc, ORD (buffer[i])); INC(pc);
  1449. IF (dump # NIL) & (pass = maxPasses) THEN dump.Hex (ORD (buffer[i]), -2); END;
  1450. END;
  1451. DEC (times);
  1452. END;
  1453. ELSE
  1454. times := 1;
  1455. END;
  1456. IF (dump # NIL) & (pass = maxPasses) THEN dump.Ln END;
  1457. END Duplicate;
  1458. PROCEDURE Reserve (size: Size) : BOOLEAN;
  1459. VAR type : SHORTINT;
  1460. BEGIN
  1461. IF Expression (val2, TRUE, type) THEN
  1462. absoluteOffset := absoluteOffset + val2 * size;
  1463. RETURN TRUE;
  1464. ELSE
  1465. RETURN FALSE;
  1466. END;
  1467. END Reserve;
  1468. PROCEDURE GetScopeSymbol (CONST ident: ARRAY OF CHAR): SyntaxTree.Symbol;
  1469. VAR sym: SyntaxTree.Symbol; localScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  1470. BEGIN
  1471. localScope := scope;
  1472. identifier := SyntaxTree.NewIdentifier(ident);
  1473. IF Trace THEN KernelLog.String("GetScopeSymbol:"); KernelLog.String(ident); KernelLog.Ln; END;
  1474. WHILE (sym = NIL) & (localScope # NIL) DO
  1475. sym := localScope.FindSymbol(identifier);
  1476. localScope := localScope.outerScope
  1477. END;
  1478. IF (sym # NIL) & (sym IS SyntaxTree.Import) THEN
  1479. NextSymbol;
  1480. IF Ensure(symPeriod,0) & (symbol = symIdent) THEN
  1481. identifier := SyntaxTree.NewIdentifier(idents);
  1482. IF Trace THEN KernelLog.String("GetScopeSymbol :"); KernelLog.String(idents); KernelLog.Ln; END;
  1483. localScope := sym(SyntaxTree.Import).module.moduleScope;
  1484. sym := NIL;
  1485. WHILE (sym = NIL) & (localScope # NIL) DO
  1486. sym := localScope.FindSymbol(identifier);
  1487. localScope := localScope.outerScope
  1488. END;
  1489. END;
  1490. END;
  1491. IF Trace THEN IF sym = NIL THEN KernelLog.String("not found") ELSE KernelLog.String("found"); END; KernelLog.Ln; END;
  1492. RETURN sym
  1493. END GetScopeSymbol;
  1494. PROCEDURE GetValue(CONST ident: ARRAY OF CHAR; VAR x: LONGINT): BOOLEAN;
  1495. VAR scopeSymbol:SyntaxTree.Symbol;
  1496. BEGIN
  1497. scopeSymbol := GetScopeSymbol (ident);
  1498. IF scopeSymbol = NIL THEN RETURN FALSE
  1499. ELSIF ~(scopeSymbol IS SyntaxTree.Constant) THEN RETURN FALSE
  1500. ELSE
  1501. IF (scopeSymbol.type.resolved IS SyntaxTree.CharacterType) & (scopeSymbol.type.resolved.sizeInBits=8) THEN
  1502. x := ORD(scopeSymbol(SyntaxTree.Constant).value.resolved(SyntaxTree.CharacterValue).value)
  1503. ELSIF scopeSymbol.type.resolved IS SyntaxTree.IntegerType THEN
  1504. x := LONGINT (scopeSymbol(SyntaxTree.Constant).value.resolved(SyntaxTree.IntegerValue).value) (* TODO: fix explicit integer truncation *)
  1505. ELSE
  1506. Error("number expected");
  1507. RETURN FALSE;
  1508. END;
  1509. RETURN TRUE;
  1510. END;
  1511. END GetValue;
  1512. PROCEDURE GetMemFixup (CONST ident: ARRAY OF CHAR; VAR operand: Operand);
  1513. VAR scopeSymbol:SyntaxTree.Symbol;
  1514. BEGIN
  1515. scopeSymbol := GetScopeSymbol (ident);
  1516. IF scopeSymbol = NIL THEN RETURN END;
  1517. IF scopeSymbol IS SyntaxTree.Constant THEN
  1518. RETURN
  1519. END;
  1520. IF inlined & exported THEN
  1521. Error("no symbols may be accessed in exported and inlined procedures");
  1522. END;
  1523. IF (scopeSymbol IS SyntaxTree.Variable) & (scopeSymbol.scope = module.module.moduleScope) THEN (* global variable. offset not supported *)
  1524. Error("global variables cannot be accessed as memory operands");
  1525. ELSIF (scopeSymbol IS SyntaxTree.Variable) THEN (* local variable *)
  1526. operand.displacement := (scopeSymbol.offsetInBits DIV 8)
  1527. ELSIF (scopeSymbol IS SyntaxTree.Parameter) THEN (* local parameter *)
  1528. operand.displacement := (scopeSymbol.offsetInBits DIV 8)
  1529. ELSE
  1530. RETURN (* ? *)
  1531. END;
  1532. (*! mem.fixup := scopeSymbol.adr; *)
  1533. NextSymbol;
  1534. END GetMemFixup;
  1535. PROCEDURE GetOffsetFixup (CONST ident: ARRAY OF CHAR; VAR operand: Operand);
  1536. VAR scopeSymbol: SyntaxTree.Symbol;name: Basic.SegmentedName; symbol: IntermediateCode.Section;
  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. Error("direct access to local variable offset forbidden");
  1560. END;
  1561. operand.sizeInBytes := emitter.cpuBits;
  1562. END GetOffsetFixup;
  1563. (* the following procedure is used to adapt sizes for relative jumps *)
  1564. PROCEDURE AdaptOperandSizes(VAR operands: ARRAY OF Operand);
  1565. VAR i: LONGINT;
  1566. PROCEDURE OffsetSize(val: HUGEINT): SHORTINT;
  1567. BEGIN
  1568. DEC(val,emitter.code.pc);
  1569. IF (val > MIN(SHORTINT)+2) & (val < MAX(SHORTINT)) THEN
  1570. RETURN bits8
  1571. (* We do not support word (16-bit) displacement jumps
  1572. (i.e. prefixing the jump instruction with the `addr16' opcode prefix),
  1573. since the 80386 insists upon masking `%eip' to 16 bits after the word
  1574. displacement is added. *)
  1575. ELSIF (val > MIN(LONGINT)+2) & (val < MAX(LONGINT)-2) THEN
  1576. RETURN bits32
  1577. ELSE
  1578. RETURN bits64
  1579. END;
  1580. END OffsetSize;
  1581. BEGIN
  1582. i := 0;
  1583. WHILE (i< LEN(operands)) & (operands[i].type # none) DO
  1584. IF (operands[i].type = ioffset) & (operands[i].sizeInBytes = bitsDefault)
  1585. THEN
  1586. IF operands[i].symbol.name = "" THEN
  1587. operands[i].sizeInBytes := OffsetSize(operands[i].val);
  1588. ELSE
  1589. operands[i].sizeInBytes := bits32
  1590. END;
  1591. END;
  1592. INC(i)
  1593. END;
  1594. END AdaptOperandSizes;
  1595. PROCEDURE GetInstruction (): BOOLEAN;
  1596. VAR
  1597. position: Basic.Position;
  1598. mnem, opCount: LONGINT;
  1599. size: Size;
  1600. operands: ARRAY InstructionSet.maxNumberOperands OF Operand;
  1601. prevFixup: BinaryCode.Fixup;
  1602. mem: Operand;
  1603. offset: Operand;
  1604. i: LONGINT;
  1605. type: SHORTINT;
  1606. BEGIN
  1607. position := errPos;
  1608. mnem := InstructionSet.FindMnemonic (ident);
  1609. IF mnem = InstructionSet.none THEN
  1610. ErrorSS("unkown instruction",idents);
  1611. RETURN FALSE;
  1612. END;
  1613. opCount := 0;
  1614. NextSymbol;
  1615. FOR i := 0 TO LEN(operands)-1 DO
  1616. InitOperand(operands[i]);
  1617. END;
  1618. WHILE (symbol # symLn) & (symbol # symNone) & (symbol # symEnd) DO
  1619. IF symbol = symIdent THEN
  1620. IF (ident = "BYTE") OR (ident = "SHORT") THEN
  1621. size := bits8; NextSymbol;
  1622. ELSIF (ident = "WORD") OR (ident = "NEAR") THEN
  1623. size := bits16; NextSymbol;
  1624. ELSIF ident = "DWORD" THEN
  1625. size := bits32; NextSymbol;
  1626. ELSIF ident = "QWORD" THEN
  1627. size := bits64; NextSymbol;
  1628. ELSIF ident = "TWORD" THEN
  1629. size := bits128; NextSymbol;
  1630. ELSE
  1631. size := bitsDefault;
  1632. END;
  1633. ELSE
  1634. size := bitsDefault;
  1635. END;
  1636. IF symbol = symIdent THEN (* register ?, for example EAX *)
  1637. reg := InstructionSet.FindRegister (ident);
  1638. IF reg = InstructionSet.none THEN
  1639. reg := map.Find(ident)
  1640. END;
  1641. IF reg # InstructionSet.none THEN
  1642. IF size # bitsDefault THEN
  1643. Error ("invalid register size specification"); RETURN FALSE;
  1644. END;
  1645. InitRegister(operands[opCount], reg);
  1646. INC (opCount);
  1647. NextSymbol;
  1648. END;
  1649. ELSE
  1650. reg := InstructionSet.none;
  1651. END;
  1652. IF reg = InstructionSet.none THEN
  1653. IF symbol = symLBraket THEN
  1654. (* mem, written as [....] *)
  1655. NextSymbol;
  1656. InitMem(mem, size, InstructionSet.none,0); (*! ??? *)
  1657. IF symbol = symLabel THEN (* register segment as in [ES:...] *)
  1658. reg := InstructionSet.FindRegister (ident);
  1659. IF reg = InstructionSet.none THEN
  1660. ErrorSS("undefined symbol",idents);
  1661. RETURN FALSE;
  1662. END;
  1663. mem.segment := reg;
  1664. NextSymbol;
  1665. END;
  1666. IF symbol = symIdent THEN (* register, for example [EAX] or [ES:EAX] *)
  1667. reg := InstructionSet.FindRegister (ident);
  1668. IF reg # InstructionSet.none THEN
  1669. mem.register := reg;
  1670. NextSymbol;
  1671. IF symbol = symTimes THEN (* register multiply as in [EAX*4] *)
  1672. NextSymbol;
  1673. IF ~Factor (mem.scale, FALSE,type) THEN
  1674. RETURN FALSE;
  1675. END;
  1676. mem.index := mem.register;
  1677. mem.register := InstructionSet.none;
  1678. END;
  1679. IF symbol = symPlus THEN (* register add as in [EAX + EBX] *)
  1680. NextSymbol;
  1681. IF symbol = symIdent THEN
  1682. reg := InstructionSet.FindRegister (ident);
  1683. IF reg # InstructionSet.none THEN (* maybe it is this: [EAX + EBX * 4] *)
  1684. NextSymbol;
  1685. IF mem.index = InstructionSet.none THEN
  1686. mem.index := reg;
  1687. IF symbol = symTimes THEN
  1688. NextSymbol;
  1689. IF ~Factor (mem.scale, FALSE,type) THEN
  1690. RETURN FALSE;
  1691. END;
  1692. END;
  1693. ELSE
  1694. mem.register := reg;
  1695. END;
  1696. END;
  1697. END;
  1698. END;
  1699. END;
  1700. END;
  1701. IF symbol = symPlus THEN
  1702. NextSymbol;
  1703. END;
  1704. IF (scope # NIL) & (symbol = symIdent) THEN
  1705. GetMemFixup (idents, mem);
  1706. END;
  1707. IF (symbol # symRBraket) & (symbol # symNegate) THEN
  1708. val2 := 0;
  1709. IF ~Expression (val2, FALSE ,type) THEN
  1710. RETURN FALSE;
  1711. END;
  1712. INC (mem.displacement, val2);
  1713. ELSIF (mem.register = InstructionSet.none) & (mem.index = InstructionSet.none) THEN
  1714. Error("operand missing: no register provided");
  1715. RETURN FALSE;
  1716. END;
  1717. operands[opCount] := mem;
  1718. INC (opCount);
  1719. IF ~Ensure (symRBraket, 556) THEN
  1720. RETURN FALSE;
  1721. END;
  1722. ELSE
  1723. (* number or identifier (symbol) *)
  1724. InitImm(offset,size,0);
  1725. IF (scope # NIL) & (symbol = symIdent) THEN (* identifier: must be a symbol *)
  1726. GetOffsetFixup (idents, offset);
  1727. END;
  1728. IF offset.symbol.name = "" THEN (* nothing could be fixuped, must be a number / constant *)
  1729. type := offset.type; currentFixup := ""; currentLabel := NIL;
  1730. IF ~Expression (val2, FALSE,type) THEN
  1731. RETURN FALSE;
  1732. ELSE
  1733. offset.type := type;
  1734. IF currentFixup # "" THEN
  1735. offset.symbol.name := currentFixup; offset.symbolOffset := val2;
  1736. ELSIF currentLabel # NIL THEN
  1737. IF (offset.sizeInBytes = bitsDefault ) & (val2 > emitter.code.pc) THEN (* forward jump *)
  1738. offset.sizeInBytes := bits32
  1739. END;
  1740. (*
  1741. IF offset.sizeInBytes = bitsDefault THEN
  1742. offset.sizeInBytes := bits32;
  1743. END;
  1744. *)
  1745. END;
  1746. END;
  1747. offset.val := val2;
  1748. IF symbol = symColon THEN (* additional prefixed operand separated by ":", segmentation register *)
  1749. NextSymbol;
  1750. IF ~Expression (val3, FALSE, type) THEN
  1751. RETURN FALSE;
  1752. END;
  1753. InitOffset(operands[opCount],bitsDefault,val3);
  1754. INC (opCount);
  1755. END;
  1756. ELSE
  1757. NextSymbol;
  1758. END;
  1759. operands[opCount] := offset;
  1760. INC (opCount);
  1761. END;
  1762. END;
  1763. IF symbol = symComma THEN
  1764. NextSymbol;
  1765. ELSIF (symbol # symLn) & (symbol # symEnd) THEN
  1766. Error("operand missing");
  1767. END
  1768. END;
  1769. prevFixup := fixup;
  1770. AdaptOperandSizes(operands);
  1771. errPos := position;
  1772. IF ~emitter.EmitInstruction (mnem, operands, pass = maxPasses) THEN
  1773. RETURN FALSE;
  1774. END;
  1775. IF fixup = prevFixup THEN
  1776. Duplicate ((emitter.code.pc - prevPC) , NIL);
  1777. ELSE
  1778. Duplicate ((emitter.code.pc - prevPC) , fixup);
  1779. END;
  1780. RETURN TRUE;
  1781. END GetInstruction;
  1782. PROCEDURE Reset;
  1783. BEGIN
  1784. reader.SetPos(orgReaderPos);
  1785. emitter.code.SetPC(orgCodePos);
  1786. NextChar;
  1787. position := orgPos;
  1788. END Reset;
  1789. PROCEDURE FindLabels;
  1790. VAR firstInLine : BOOLEAN; label: NamedLabel;
  1791. BEGIN
  1792. IF Trace THEN KernelLog.String("find labels"); KernelLog.Ln; END;
  1793. LOOP
  1794. NextSymbol;
  1795. IF symbol = symLn THEN
  1796. firstInLine := TRUE;
  1797. ELSIF symbol = symLabel THEN
  1798. IF firstInLine THEN
  1799. IF labels.Find(idents) # NIL THEN
  1800. Error("multiply declared identifier")
  1801. ELSE
  1802. NEW(label,MAX(LONGINT),idents);
  1803. labels.Add(label);
  1804. IF Trace THEN KernelLog.String("found label"); KernelLog.String(idents); KernelLog.Ln; END;
  1805. END
  1806. END;
  1807. ELSIF symbol = symEnd THEN
  1808. EXIT
  1809. ELSE
  1810. firstInLine := FALSE;
  1811. END;
  1812. END;
  1813. END FindLabels;
  1814. PROCEDURE FixupLabels;
  1815. VAR label: NamedLabel;
  1816. BEGIN
  1817. IF Trace THEN KernelLog.String("patch fixups "); KernelLog.Ln; END;
  1818. fixup := emitter.code.fixupList.firstFixup;
  1819. WHILE fixup # NIL DO
  1820. IF (fixup.symbol.name = in.name) & (fixup.symbolOffset < 0) THEN
  1821. label := labels.first;
  1822. WHILE (label # NIL) & (label.index # -fixup.symbolOffset) DO label := label.nextNamedLabel END;
  1823. (*
  1824. fixup.SetSymbolOffset(label.offset);
  1825. *)
  1826. fixup.SetSymbol(out.name,0,0,label.offset+fixup.displacement);
  1827. IF Trace THEN
  1828. KernelLog.String("patch fixup: ");
  1829. KernelLog.Hex(fixup.offset,1); KernelLog.String(" "); KernelLog.Hex(-fixup.displacement, 1);
  1830. KernelLog.String(" "); KernelLog.Hex(label.offset, 1); KernelLog.Ln;
  1831. END;
  1832. END;
  1833. fixup := fixup.nextFixup;
  1834. END;
  1835. END FixupLabels;
  1836. BEGIN
  1837. prevAssembly := emitter.assembly;
  1838. prevSourceName := sourceName;
  1839. prevCpuBits := emitter.cpuBits;
  1840. prevCpuOptions := emitter.cpuOptions;
  1841. emitter.assembly := SELF;
  1842. IF scope # NIL THEN
  1843. sourceName := scope.ownerModule.sourceName;
  1844. END;
  1845. NEW(labels);
  1846. orgReaderPos := reader.Pos();
  1847. orgCodePos := emitter.code.pc;
  1848. NextChar;
  1849. position := orgPos;
  1850. (* first we have to find all labels as their names might collide with symbol names *)
  1851. FindLabels;
  1852. FOR pass := 1 TO maxPasses DO (*! currently maxPasses = 1 *)
  1853. Reset;
  1854. times := 1;
  1855. prevPC := emitter.code.pc;
  1856. currentLabel := NIL;
  1857. absoluteMode := FALSE;
  1858. orgOffset := 0;
  1859. NextSymbol;
  1860. IF (scope # NIL) THEN
  1861. IF symbol # symLBrace THEN
  1862. (* treat CPU options as an optional limitation and not vice versa *)
  1863. ELSE
  1864. emitter.cpuOptions := {};
  1865. NextSymbol;
  1866. (* parse code flags such as {SYSTEM.i386 .... } *)
  1867. LOOP
  1868. IF ~Ensure (symIdent, 551) THEN
  1869. RETURN
  1870. END;
  1871. IF ident # "SYSTEM" THEN
  1872. Error("unsupportorted target identifier");
  1873. RETURN
  1874. END;
  1875. IF symbol # symPeriod THEN
  1876. Error("identifier expected");
  1877. RETURN;
  1878. END;
  1879. IF ~GetCPU (TRUE) THEN
  1880. RETURN;
  1881. END;
  1882. IF symbol = symRBrace THEN
  1883. EXIT
  1884. ELSIF symbol = symComma THEN
  1885. NextSymbol
  1886. ELSE
  1887. Error("target specifier expected");
  1888. RETURN;
  1889. END;
  1890. END;
  1891. NextSymbol;
  1892. END
  1893. END;
  1894. LOOP
  1895. IF symbol = symLn THEN
  1896. NextSymbol;
  1897. ELSIF symbol = symLabel THEN
  1898. currentLabel := labels.Find(idents);
  1899. ASSERT(currentLabel # NIL);
  1900. IF absoluteMode THEN
  1901. currentLabel.SetOffset(absoluteOffset);
  1902. ELSE
  1903. currentLabel.SetOffset(emitter.code.pc)
  1904. END;
  1905. NextSymbol;
  1906. ELSIF symbol = symIdent THEN
  1907. IF ident = "END" THEN
  1908. symbol := symNone;
  1909. ELSIF ident = "BITS" THEN
  1910. NextSymbol;
  1911. IF ~Ensure (symNumber, 553) OR ~emitter.SetBits (val) THEN
  1912. SkipLine;
  1913. ELSE
  1914. NextSymbol;
  1915. END;
  1916. ELSIF ident = "ALIGN" THEN
  1917. NextSymbol;
  1918. IF Expression(alignment, TRUE, type) THEN
  1919. Align(alignment);
  1920. END;
  1921. ELSIF ~(scope # NIL) & (ident = "CPU") THEN
  1922. IF ~GetCPU (FALSE) THEN
  1923. SkipLine;
  1924. END;
  1925. ELSIF ~(scope # NIL) & (ident = "ABSOLUTE") THEN
  1926. absoluteMode := TRUE;
  1927. NextSymbol;
  1928. IF ~Expression (absoluteOffset, TRUE,type) THEN
  1929. SkipLine;
  1930. END;
  1931. ELSIF ~(scope # NIL) & (ident = "ORG") THEN
  1932. NextSymbol;
  1933. IF (orgOffset # 0) OR ~Expression (orgOffset, TRUE,type) THEN
  1934. SkipLine;
  1935. END;
  1936. ELSIF ~(scope # NIL) & (ident = "RESB") THEN
  1937. NextSymbol;
  1938. IF ~Reserve (1) THEN SkipLine END;
  1939. ELSIF ~(scope # NIL) & (ident = "RESW") THEN
  1940. NextSymbol;
  1941. IF ~Reserve (2) THEN SkipLine END;
  1942. ELSIF ~(scope # NIL) & (ident = "RESD") THEN
  1943. NextSymbol;
  1944. IF ~Reserve (4) THEN SkipLine END;
  1945. (*
  1946. ELSIF ident = "EQU" THEN
  1947. IF currentLabel # NIL THEN
  1948. NextSymbol;
  1949. IF Expression (val2, FALSE) THEN
  1950. currentLabel.pc := val2;
  1951. currentLabel.equ := TRUE;
  1952. ELSE
  1953. SkipLine;
  1954. END;
  1955. ELSE
  1956. Error("???");
  1957. RETURN;
  1958. END;
  1959. *)
  1960. ELSIF ident = "TIMES" THEN
  1961. NextSymbol;
  1962. IF ~Expression (times, TRUE,type) THEN
  1963. SkipLine;
  1964. ELSIF times < 0 THEN
  1965. Error("unsupported negative value"); RETURN;
  1966. ELSE
  1967. prevPC := emitter.code.pc;
  1968. END;
  1969. ELSIF ident = "DB" THEN
  1970. IF ~PutData (bits8) THEN SkipLine END;
  1971. ELSIF ident = "DW" THEN
  1972. IF ~PutData (bits16) THEN SkipLine END;
  1973. ELSIF ident = "DD" THEN
  1974. IF ~PutData (bits32) THEN SkipLine END;
  1975. ELSIF ident = "DQ" THEN
  1976. IF ~PutData (bits64) THEN SkipLine END;
  1977. ELSIF ident = "REP" THEN
  1978. NextSymbol;
  1979. emitter.code.PutByte (InstructionSet.prfREP);
  1980. ELSIF ident = "LOCK" THEN
  1981. NextSymbol;
  1982. emitter.code.PutByte (InstructionSet.prfLOCK);
  1983. ELSIF ident = "REPE" THEN
  1984. NextSymbol;
  1985. emitter.code.PutByte (InstructionSet.prfREPE);
  1986. ELSIF ident = "REPZ" THEN
  1987. NextSymbol;
  1988. emitter.code.PutByte (InstructionSet.prfREPZ);
  1989. ELSIF ident = "REPNE" THEN
  1990. NextSymbol;
  1991. emitter.code.PutByte (InstructionSet.prfREPNE);
  1992. ELSIF ident = "REPNZ" THEN
  1993. NextSymbol;
  1994. emitter.code.PutByte (InstructionSet.prfREPNZ);
  1995. ELSIF ~GetInstruction () THEN
  1996. SkipLine
  1997. END;
  1998. currentLabel := NIL;
  1999. ELSIF (symbol = symNone) OR (symbol = symEnd) THEN
  2000. EXIT
  2001. ELSE
  2002. Error("identifier expected");
  2003. RETURN;
  2004. END;
  2005. END;
  2006. END;
  2007. (*
  2008. FixupLabels();
  2009. *)
  2010. (*! FixupLabels(labels.first,code) *)
  2011. sourceName := prevSourceName;
  2012. emitter.cpuBits := prevCpuBits;
  2013. emitter.cpuOptions := prevCpuOptions;
  2014. emitter.assembly := prevAssembly;
  2015. END Assemble;
  2016. END Assembly;
  2017. VAR kernelWriter: Streams.Writer;
  2018. PROCEDURE Ord (ch: CHAR): INTEGER;
  2019. BEGIN RETURN ORD (ch) - ORD ("0")
  2020. END Ord;
  2021. PROCEDURE HexOrd (ch: CHAR): INTEGER;
  2022. BEGIN
  2023. IF ch <= "9" THEN RETURN ORD (ch) - ORD ("0")
  2024. ELSE RETURN ORD (CAP (ch)) - ORD ("A") + 10
  2025. END
  2026. END HexOrd;
  2027. PROCEDURE IsRegisterOperand*(CONST op: Operand): BOOLEAN;
  2028. BEGIN
  2029. RETURN op.type IN {reg8, reg16, reg32, reg64, CRn, DRn, segReg, sti, mmx, xmm, ymm}
  2030. END IsRegisterOperand;
  2031. PROCEDURE IsMemoryOperand*(CONST op: Operand): BOOLEAN;
  2032. BEGIN RETURN op.type = mem
  2033. END IsMemoryOperand;
  2034. PROCEDURE IsImmediateOperand*(CONST op: Operand): BOOLEAN;
  2035. BEGIN RETURN op.type = imm
  2036. END IsImmediateOperand;
  2037. PROCEDURE DumpType*(w: Streams.Writer; type: LONGINT);
  2038. BEGIN
  2039. CASE type OF
  2040. reg8: w.String("reg8")
  2041. |reg16: w.String("reg16");
  2042. |reg32: w.String("reg32");
  2043. |reg64: w.String("reg64");
  2044. |CRn: w.String("CRn");
  2045. |DRn: w.String("DRn");
  2046. |segReg: w.String("segReg");
  2047. |mmx: w.String("mmx");
  2048. |xmm: w.String("xmm");
  2049. |ymm: w.String("ymm");
  2050. |mem: w.String("mem");
  2051. |sti: w.String("sti");
  2052. |imm: w.String("imm");
  2053. |ioffset: w.String("ioffset");
  2054. |pntr1616: w.String("pntr1616");
  2055. |pntr1632: w.String("pntr1632");
  2056. ELSE
  2057. w.String("?"); w.Int(type,1); w.String("?");
  2058. END;
  2059. END DumpType;
  2060. PROCEDURE DumpOperand*(w: Streams.Writer; CONST operand: Operand);
  2061. BEGIN
  2062. CASE operand.type OF
  2063. |reg8, reg16, reg32, reg64, CRn, DRn, segReg, sti, mmx, xmm, ymm:
  2064. w.String(InstructionSet.registers[operand.register].name);
  2065. |mem:
  2066. IF operand.sizeInBytes = 1 THEN w.String("BYTE ")
  2067. ELSIF operand.sizeInBytes= 2 THEN w.String("WORD ")
  2068. ELSIF operand.sizeInBytes = 4 THEN w.String("DWORD ")
  2069. ELSIF operand.sizeInBytes = 8 THEN w.String("QWORD ")
  2070. END;
  2071. w.String("[");
  2072. IF operand.register # none THEN
  2073. w.String(InstructionSet.registers[operand.register].name);
  2074. IF operand.index # none THEN w.String("+") END;
  2075. END;
  2076. IF operand.index # none THEN
  2077. w.String(InstructionSet.registers[operand.index].name);
  2078. IF operand.scale # 1 THEN
  2079. w.String("*"); w.Int(operand.scale,1);
  2080. END;
  2081. END;
  2082. IF operand.symbol.name # "" THEN
  2083. Basic.WriteSegmentedName(w, operand.symbol.name); w.String(":"); w.Int(operand.displacement,1);
  2084. IF operand.symbolOffset # 0 THEN w.String("(@"); w.Int(operand.symbolOffset,1); w.String(")") END;
  2085. ELSIF operand.displacement # 0 THEN
  2086. IF (operand.displacement > 0) & ((operand.register # none) OR (operand.index # none)) THEN w.String("+");END;
  2087. w.Int(operand.displacement,1);
  2088. END;
  2089. w.String("]");
  2090. |imm,ioffset:
  2091. IF operand.symbol.name # "" THEN
  2092. Basic.WriteSegmentedName(w, operand.symbol.name); w.String(":"); w.Int(operand.displacement,1);
  2093. IF operand.symbolOffset # 0 THEN w.String("(@"); w.Int(operand.symbolOffset,1); w.String(")") END;
  2094. ELSE
  2095. IF (operand.val > MAX(LONGINT)) OR (operand.val < MIN(LONGINT)) THEN
  2096. w.Hex(operand.val,1); w.String("H");
  2097. ELSE
  2098. w.Int(SHORT(operand.val),1);
  2099. END;
  2100. END;
  2101. |pntr1616:
  2102. |pntr1632:
  2103. ELSE
  2104. HALT(100)
  2105. END;
  2106. END DumpOperand;
  2107. PROCEDURE DumpInstruction(w: Streams.Writer; mnemonic: LONGINT; CONST operands: ARRAY OF Operand);
  2108. VAR i: LONGINT;
  2109. CONST DebugSize = FALSE;
  2110. BEGIN
  2111. IF mnemonic # none THEN
  2112. w.String(InstructionSet.mnemonics[mnemonic].name);
  2113. i := 0;
  2114. WHILE(i<maxNumberOperands) & (operands[i].type # none) DO
  2115. IF i = 0 THEN w.Char(09X) ELSE w.String(", ") END;
  2116. DumpOperand(w,operands[i]);
  2117. IF DebugSize THEN
  2118. w.String("(*"); DumpType(w,operands[i].type); w.String(":"); w.Int(operands[i].sizeInBytes,1); w.String("*)");
  2119. END;
  2120. INC(i);
  2121. END;
  2122. w.String("; ");
  2123. END;
  2124. END DumpInstruction;
  2125. PROCEDURE Matches(CONST operand: Operand; type: InstructionSet.OperandType): BOOLEAN;
  2126. PROCEDURE IsMemReg(regIndex: LONGINT): BOOLEAN;
  2127. BEGIN
  2128. RETURN InstructionSet.RegisterType(regIndex) IN {reg16, reg32, reg64}
  2129. END IsMemReg;
  2130. BEGIN
  2131. CASE operand.type OF
  2132. |reg8:
  2133. CASE type OF
  2134. InstructionSet.reg8, InstructionSet.regmem8:
  2135. RETURN TRUE;
  2136. | InstructionSet.AL, InstructionSet.rAX:
  2137. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2138. | InstructionSet.CL:
  2139. RETURN InstructionSet.RegisterIndex(operand.register) = RCX;
  2140. ELSE
  2141. RETURN FALSE;
  2142. END;
  2143. |reg16:
  2144. CASE type OF
  2145. InstructionSet.reg16, InstructionSet.regmem16:
  2146. RETURN TRUE;
  2147. | InstructionSet.AX, InstructionSet.rAX:
  2148. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2149. | InstructionSet.DX:
  2150. RETURN InstructionSet.RegisterIndex(operand.register) = RDX;
  2151. ELSE
  2152. RETURN FALSE;
  2153. END;
  2154. |reg32:
  2155. CASE type OF
  2156. InstructionSet.reg32, InstructionSet.regmem32:
  2157. RETURN TRUE;
  2158. | InstructionSet.EAX, InstructionSet.rAX:
  2159. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2160. ELSE
  2161. RETURN FALSE;
  2162. END;
  2163. |reg64:
  2164. CASE type OF
  2165. InstructionSet.reg64, InstructionSet.regmem64:
  2166. RETURN TRUE;
  2167. | InstructionSet.RAX, InstructionSet.rAX:
  2168. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2169. ELSE
  2170. RETURN FALSE;
  2171. END;
  2172. |CRn:
  2173. CASE type OF
  2174. InstructionSet.CRn:
  2175. RETURN TRUE;
  2176. | InstructionSet.CR8:
  2177. RETURN InstructionSet.RegisterIndex(operand.register) = 8;
  2178. ELSE
  2179. RETURN FALSE;
  2180. END;
  2181. |DRn:
  2182. RETURN type = InstructionSet.DRn;
  2183. |segReg:
  2184. CASE type OF
  2185. InstructionSet.segReg:
  2186. RETURN TRUE;
  2187. | InstructionSet.ES:
  2188. RETURN InstructionSet.RegisterIndex(operand.register) = segES;
  2189. | InstructionSet.CS:
  2190. RETURN InstructionSet.RegisterIndex(operand.register) = segCS;
  2191. | InstructionSet.SS:
  2192. RETURN InstructionSet.RegisterIndex(operand.register) = segSS;
  2193. | InstructionSet.DS:
  2194. RETURN InstructionSet.RegisterIndex(operand.register) = segDS;
  2195. | InstructionSet.FS:
  2196. RETURN InstructionSet.RegisterIndex(operand.register) = segFS;
  2197. | InstructionSet.GS:
  2198. RETURN InstructionSet.RegisterIndex(operand.register) = segGS;
  2199. ELSE
  2200. RETURN FALSE;
  2201. END
  2202. |sti:
  2203. CASE type OF
  2204. InstructionSet.sti:
  2205. RETURN TRUE;
  2206. | InstructionSet.st0:
  2207. RETURN InstructionSet.RegisterIndex(operand.register) = 0;
  2208. ELSE
  2209. RETURN FALSE;
  2210. END
  2211. |mmx:
  2212. CASE type OF
  2213. InstructionSet.mmx, InstructionSet.mmxmem32, InstructionSet.mmxmem64:
  2214. RETURN TRUE;
  2215. ELSE
  2216. RETURN FALSE;
  2217. END
  2218. |xmm:
  2219. CASE type OF
  2220. InstructionSet.xmm, InstructionSet.xmmmem32, InstructionSet.xmmmem64, InstructionSet.xmmmem128:
  2221. RETURN TRUE;
  2222. ELSE
  2223. RETURN FALSE;
  2224. END
  2225. |ymm:
  2226. CASE type OF
  2227. InstructionSet.ymm, InstructionSet.ymmmem128, InstructionSet.ymmmem256:
  2228. RETURN TRUE;
  2229. ELSE
  2230. RETURN FALSE;
  2231. END
  2232. |mem:
  2233. CASE type OF
  2234. | InstructionSet.mem:
  2235. RETURN TRUE;
  2236. | InstructionSet.mem8:
  2237. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8);
  2238. | InstructionSet.regmem8:
  2239. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2240. | InstructionSet.mem16:
  2241. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16);
  2242. | InstructionSet.regmem16:
  2243. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2244. | InstructionSet.mem32:
  2245. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32);
  2246. | InstructionSet.regmem32, InstructionSet.mmxmem32, InstructionSet.xmmmem32:
  2247. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2248. | InstructionSet.mem64:
  2249. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64);
  2250. | InstructionSet.regmem64, InstructionSet.mmxmem64, InstructionSet.xmmmem64:
  2251. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2252. | InstructionSet.mem128:
  2253. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits128);
  2254. | InstructionSet.xmmmem128, InstructionSet.ymmmem128:
  2255. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits128)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2256. | InstructionSet.ymmmem256:
  2257. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits256)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2258. | InstructionSet.moffset8:
  2259. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.register= none);
  2260. | InstructionSet.moffset16:
  2261. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.register= none);
  2262. | InstructionSet.moffset32:
  2263. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) & (operand.register= none);
  2264. | InstructionSet.moffset64:
  2265. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64)) & (operand.register= none);
  2266. ELSE
  2267. RETURN FALSE;
  2268. END;
  2269. |imm,ioffset:
  2270. CASE type OF
  2271. InstructionSet.one:
  2272. RETURN operand.val = 1
  2273. | InstructionSet.three:
  2274. RETURN operand.val = 3
  2275. | InstructionSet.rel8off:
  2276. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)
  2277. | InstructionSet.imm8:
  2278. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.val >= -80H) & (operand.val < 100H)
  2279. | InstructionSet.simm8:
  2280. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.val >= -80H) & (operand.val < 80H)
  2281. | InstructionSet.uimm8:
  2282. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.val >= 0H) & (operand.val < 100H)
  2283. | InstructionSet.rel16off:
  2284. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16) & FALSE (* do not allow 16 bit jumps *)
  2285. | InstructionSet.imm16:
  2286. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.val >= -8000H) & (operand.val < 10000H)
  2287. | InstructionSet.simm16:
  2288. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.val >= -8000H) & (operand.val < 8000H)
  2289. | InstructionSet.uimm16:
  2290. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.val >= 0H) & (operand.val < 10000H)
  2291. | InstructionSet.rel32off:
  2292. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32) (* & & (operand.val >= -80000000H) & (operand.val < 100000000H) PACO confused? *)
  2293. | InstructionSet.imm32:
  2294. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) (* & & (operand.val >= -80000000H) & (operand.val < 100000000H) PACO confused? *)
  2295. | InstructionSet.simm32:
  2296. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) (* & & (operand.val >= -80000000H) & (operand.val < 80000000H) PACO confused? *)
  2297. | InstructionSet.uimm32:
  2298. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) & (operand.val >= 0H) (* & (operand.val < 100000000H) PACO confused? *)
  2299. | InstructionSet.imm64:
  2300. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64)
  2301. ELSE
  2302. RETURN FALSE
  2303. END
  2304. |pntr1616:
  2305. RETURN type = InstructionSet.pntr1616;
  2306. |pntr1632:
  2307. RETURN type = InstructionSet.pntr1632;
  2308. ELSE
  2309. HALT(100)
  2310. END;
  2311. END Matches;
  2312. PROCEDURE ValueInByteRange (value: HUGEINT): BOOLEAN;
  2313. BEGIN RETURN SYSTEM.VAL (SHORTINT, value) = value
  2314. END ValueInByteRange;
  2315. PROCEDURE ValueInWordRange (value: HUGEINT): BOOLEAN;
  2316. BEGIN RETURN SYSTEM.VAL (INTEGER, value) = value
  2317. END ValueInWordRange;
  2318. PROCEDURE InitOperand*(VAR operand: Operand);
  2319. BEGIN
  2320. operand.type := none;
  2321. operand.index := none;
  2322. operand.register:= none;
  2323. operand.segment:= none;
  2324. operand.sizeInBytes := none;
  2325. operand.scale := 1;
  2326. operand.displacement := 0;
  2327. operand.val := 0;
  2328. operand.pc := none;
  2329. operand.symbol.name := "";
  2330. operand.symbol.fingerprint := 0;
  2331. operand.selector := none;
  2332. operand.offset := 0;
  2333. END InitOperand;
  2334. PROCEDURE InitRegister* (VAR operand: Operand; register: Register);
  2335. BEGIN
  2336. InitOperand(operand);
  2337. operand.type := InstructionSet.RegisterType(register);
  2338. operand.register :=register;
  2339. CASE operand.type OF
  2340. reg8,reg16,reg32,reg64,segReg,CRn,DRn,sti,xmm,mmx,ymm: (* ok *)
  2341. |InstructionSet.st0: operand.type := InstructionSet.sti;
  2342. ELSE
  2343. HALT(100);
  2344. END;
  2345. operand.sizeInBytes := InstructionSet.registers[register].sizeInBytes
  2346. END InitRegister;
  2347. PROCEDURE NewRegister*(register: Register): Operand;
  2348. VAR operand: Operand;
  2349. BEGIN InitRegister(operand,register); RETURN operand
  2350. END NewRegister;
  2351. PROCEDURE InitMem*(VAR operand: Operand; size: Size; reg: Register; displacement: LONGINT);
  2352. BEGIN
  2353. InitOperand(operand);
  2354. operand.type := mem;
  2355. operand.sizeInBytes := size;
  2356. operand.register:= reg;
  2357. operand.displacement := displacement;
  2358. operand.scale := 1;
  2359. END InitMem;
  2360. PROCEDURE SetIndexScale*(VAR operand: Operand; index: Register; scale: LONGINT);
  2361. BEGIN
  2362. operand.index := index;
  2363. operand.scale := scale
  2364. END SetIndexScale;
  2365. PROCEDURE NewMem*(size: Size; reg: Register; displacement: LONGINT): Operand;
  2366. VAR operand: Operand;
  2367. BEGIN
  2368. InitMem(operand,size,reg,displacement); RETURN operand
  2369. END NewMem;
  2370. PROCEDURE InitMem8* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2371. BEGIN InitMem (operand, bits8, reg, displacement);
  2372. END InitMem8;
  2373. PROCEDURE NewMem8* (reg: Register; displacement: LONGINT): Operand;
  2374. VAR operand: Operand;
  2375. BEGIN InitMem8 (operand,reg, displacement); RETURN operand
  2376. END NewMem8;
  2377. PROCEDURE InitMem16* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2378. BEGIN InitMem (operand,bits16, reg, displacement);
  2379. END InitMem16;
  2380. PROCEDURE NewMem16* (reg: Register; displacement: LONGINT): Operand;
  2381. VAR operand: Operand;
  2382. BEGIN InitMem16 (operand,reg, displacement); RETURN operand
  2383. END NewMem16;
  2384. PROCEDURE InitMem32* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2385. BEGIN InitMem (operand,bits32, reg, displacement);
  2386. END InitMem32;
  2387. PROCEDURE NewMem32* (reg: Register; displacement: LONGINT): Operand;
  2388. VAR operand: Operand;
  2389. BEGIN InitMem32 (operand,reg, displacement); RETURN operand
  2390. END NewMem32;
  2391. PROCEDURE InitMem64* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2392. BEGIN InitMem (operand,bits64, reg, displacement);
  2393. END InitMem64;
  2394. PROCEDURE NewMem64* (reg: Register; displacement: LONGINT): Operand;
  2395. VAR operand: Operand;
  2396. BEGIN InitMem64 (operand,reg, displacement); RETURN operand
  2397. END NewMem64;
  2398. PROCEDURE InitMem128* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2399. BEGIN InitMem (operand,bits128, reg, displacement);
  2400. END InitMem128;
  2401. PROCEDURE NewMem128* (reg: Register; displacement: LONGINT): Operand;
  2402. VAR operand: Operand;
  2403. BEGIN InitMem128 (operand,reg, displacement); RETURN operand
  2404. END NewMem128;
  2405. PROCEDURE SetSymbol*(VAR operand: Operand; symbol: Sections.SectionName; fingerprint: LONGINT; symbolOffset, displacement: LONGINT);
  2406. BEGIN
  2407. operand.symbol.name := symbol;
  2408. operand.symbol.fingerprint := fingerprint;
  2409. operand.symbolOffset := symbolOffset; operand.displacement := displacement;
  2410. END SetSymbol;
  2411. PROCEDURE InitImm* (VAR operand: Operand; size: SHORTINT; val: HUGEINT);
  2412. BEGIN InitOperand(operand); operand.type := imm; operand.sizeInBytes := size; operand.val := val;
  2413. END InitImm;
  2414. PROCEDURE InitImm8* (VAR operand: Operand; val: HUGEINT);
  2415. BEGIN InitImm (operand, bits8, val);
  2416. END InitImm8;
  2417. PROCEDURE NewImm8*(val: HUGEINT): Operand;
  2418. VAR operand: Operand;
  2419. BEGIN InitImm8(operand,val); RETURN operand
  2420. END NewImm8;
  2421. PROCEDURE InitImm16* (VAR operand: Operand; val: HUGEINT);
  2422. BEGIN InitImm (operand, bits16, val);
  2423. END InitImm16;
  2424. PROCEDURE NewImm16*(val: HUGEINT): Operand;
  2425. VAR operand:Operand;
  2426. BEGIN InitImm16(operand,val); RETURN operand
  2427. END NewImm16;
  2428. PROCEDURE InitImm32* (VAR operand: Operand; val: HUGEINT);
  2429. BEGIN InitImm (operand, bits32, val);
  2430. END InitImm32;
  2431. PROCEDURE NewImm32*(val: HUGEINT): Operand;
  2432. VAR operand: Operand;
  2433. BEGIN InitImm32(operand,val); RETURN operand
  2434. END NewImm32;
  2435. PROCEDURE InitImm64* (VAR operand: Operand; val: HUGEINT);
  2436. BEGIN InitImm (operand, bits64, val);
  2437. END InitImm64;
  2438. PROCEDURE NewImm64*(val: HUGEINT): Operand;
  2439. VAR operand: Operand;
  2440. BEGIN InitImm64(operand,val); RETURN operand
  2441. END NewImm64;
  2442. PROCEDURE InitOffset* (VAR operand: Operand; size: SHORTINT; val: HUGEINT);
  2443. BEGIN InitOperand(operand); operand.type := ioffset; operand.sizeInBytes := size; operand.val := val;
  2444. END InitOffset;
  2445. PROCEDURE InitOffset8* (VAR operand: Operand; val: HUGEINT);
  2446. BEGIN InitOffset (operand, bits8, val);
  2447. END InitOffset8;
  2448. PROCEDURE NewOffset8*(val: HUGEINT): Operand;
  2449. VAR operand: Operand;
  2450. BEGIN InitOffset8(operand,val); RETURN operand
  2451. END NewOffset8;
  2452. PROCEDURE InitOffset16* (VAR operand: Operand; val: HUGEINT);
  2453. BEGIN InitOffset (operand, bits16, val);
  2454. END InitOffset16;
  2455. PROCEDURE NewOffset16*(val: HUGEINT): Operand;
  2456. VAR operand: Operand;
  2457. BEGIN InitOffset16(operand,val); RETURN operand
  2458. END NewOffset16;
  2459. PROCEDURE InitOffset32* (VAR operand: Operand; val: HUGEINT);
  2460. BEGIN InitOffset (operand, bits32, val);
  2461. END InitOffset32;
  2462. PROCEDURE NewOffset32*(val: HUGEINT): Operand;
  2463. VAR operand: Operand;
  2464. BEGIN InitOffset32(operand,val); RETURN operand
  2465. END NewOffset32;
  2466. PROCEDURE InitOffset64* (VAR operand: Operand; val: HUGEINT);
  2467. BEGIN InitOffset (operand, bits64, val);
  2468. END InitOffset64;
  2469. PROCEDURE NewOffset64*(val: HUGEINT): Operand;
  2470. VAR operand: Operand;
  2471. BEGIN InitOffset64(operand,val); RETURN operand
  2472. END NewOffset64;
  2473. PROCEDURE InitPntr1616* (VAR operand: Operand; s, o: LONGINT);
  2474. BEGIN InitOperand(operand); operand.type := pntr1616; operand.selector := s; operand.offset := o;
  2475. END InitPntr1616;
  2476. PROCEDURE InitPntr1632* (VAR operand: Operand; s, o: LONGINT);
  2477. BEGIN InitOperand(operand); operand.type := pntr1632; operand.selector := s; operand.offset := o;
  2478. END InitPntr1632;
  2479. PROCEDURE SetSize*(VAR operand: Operand;sizeInBytes: Size);
  2480. BEGIN operand.sizeInBytes := sizeInBytes
  2481. END SetSize;
  2482. PROCEDURE SameOperand*(CONST left,right: Operand): BOOLEAN;
  2483. BEGIN
  2484. IF (left.type # right.type) OR (left.sizeInBytes # right.sizeInBytes) OR (left.symbol # right.symbol) THEN RETURN FALSE END;
  2485. CASE left.type OF
  2486. reg8,reg16,reg32,reg64,segReg,CRn,DRn,sti,xmm,mmx,ymm: RETURN left.register = right.register
  2487. | imm,ioffset: RETURN (left.val = right.val) & ((left.symbol.name="") OR (left.displacement = right.displacement))
  2488. | mem:RETURN (left.register = right.register) & (left.displacement = right.displacement) & (left.index = right.index) & (left.scale = right.scale)
  2489. | pntr1616,pntr1632: RETURN (left.selector=right.selector) & (left.offset=right.offset)
  2490. END;
  2491. RETURN FALSE
  2492. END SameOperand;
  2493. PROCEDURE Test*(context: Commands.Context);
  2494. VAR assembly: Emitter;
  2495. (*errorHandler: ErrorHandler; *)
  2496. op1,op2,op3: Operand;
  2497. diagnostics: Diagnostics.StreamDiagnostics;
  2498. code: Code;
  2499. pooledName: Basic.SegmentedName;
  2500. PROCEDURE Op(CONST name: ARRAY OF CHAR): LONGINT;
  2501. BEGIN
  2502. RETURN InstructionSet.FindMnemonic(name)
  2503. END Op;
  2504. BEGIN
  2505. InitOperand(op1); InitOperand(op2); InitOperand(op3);
  2506. NEW(diagnostics,context.error);
  2507. Basic.ToSegmentedName("test", pooledName);
  2508. NEW(code,Sections.CodeSection,8,pooledName,TRUE,TRUE);
  2509. NEW(assembly,diagnostics);
  2510. assembly.SetCode(code);
  2511. InitRegister(op1,InstructionSet.regEAX);
  2512. InitImm32(op2,10);
  2513. assembly.Emit2(Op("MOV"),op1,op2);
  2514. context.out.Update;
  2515. code.Dump(context.out);
  2516. END Test;
  2517. BEGIN
  2518. IF Trace THEN
  2519. NEW(kernelWriter,KernelLog.Send,1000);
  2520. END;
  2521. END FoxAMD64Assembler.
  2522. OCAMD64Assembler.Test ~