FoxAMD64Assembler.Mod 90 KB

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