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