FoxAMD64Assembler.Mod 88 KB

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