FoxAMD64Assembler.Mod 91 KB

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