FoxAMD64Assembler.Mod 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725
  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. position := orgPos;
  1732. reader.SetPos(orgReaderPos);
  1733. emitter.code.SetPC(orgCodePos);
  1734. NextChar;
  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. (* first we have to find all labels as their names might collide with symbol names *)
  1797. FindLabels;
  1798. FOR pass := 1 TO maxPasses DO (*! currently maxPasses = 1 *)
  1799. Reset;
  1800. times := 1;
  1801. prevPC := emitter.code.pc;
  1802. currentLabel := NIL;
  1803. absoluteMode := FALSE;
  1804. orgOffset := 0;
  1805. NextSymbol;
  1806. IF (scope # NIL) THEN
  1807. IF symbol # symLBrace THEN
  1808. (* treat CPU options as an optional limitation and not vice versa *)
  1809. ELSE
  1810. emitter.cpuOptions := {};
  1811. NextSymbol;
  1812. (* parse code flags such as {SYSTEM.i386 .... } *)
  1813. LOOP
  1814. IF ~Ensure (symIdent, 551) THEN
  1815. RETURN
  1816. END;
  1817. IF ident # "SYSTEM" THEN
  1818. Error("unsupportorted target identifier");
  1819. RETURN
  1820. END;
  1821. IF symbol # symPeriod THEN
  1822. Error("identifier expected");
  1823. RETURN;
  1824. END;
  1825. IF ~GetCPU (TRUE) THEN
  1826. RETURN;
  1827. END;
  1828. IF symbol = symRBrace THEN
  1829. EXIT
  1830. ELSIF symbol = symComma THEN
  1831. NextSymbol
  1832. ELSE
  1833. Error("target specifier expected");
  1834. RETURN;
  1835. END;
  1836. END;
  1837. NextSymbol;
  1838. END
  1839. END;
  1840. LOOP
  1841. IF symbol = symLn THEN
  1842. NextSymbol;
  1843. ELSIF symbol = symLabel THEN
  1844. currentLabel := labels.Find(idents);
  1845. ASSERT(currentLabel # NIL);
  1846. IF absoluteMode THEN
  1847. currentLabel.SetOffset(absoluteOffset);
  1848. ELSE
  1849. currentLabel.SetOffset(emitter.code.pc)
  1850. END;
  1851. NextSymbol;
  1852. ELSIF symbol = symIdent THEN
  1853. IF ident = "END" THEN
  1854. symbol := symNone;
  1855. ELSIF ident = "BITS" THEN
  1856. NextSymbol;
  1857. IF ~Ensure (symNumber, 553) OR ~emitter.SetBits (val) THEN
  1858. SkipLine;
  1859. ELSE
  1860. NextSymbol;
  1861. END;
  1862. ELSIF ident = "ALIGN" THEN
  1863. NextSymbol;
  1864. IF Expression(alignment, TRUE, type) THEN
  1865. Align(alignment);
  1866. END;
  1867. ELSIF ~(scope # NIL) & (ident = "CPU") THEN
  1868. IF ~GetCPU (FALSE) THEN
  1869. SkipLine;
  1870. END;
  1871. ELSIF ~(scope # NIL) & (ident = "ABSOLUTE") THEN
  1872. absoluteMode := TRUE;
  1873. NextSymbol;
  1874. IF ~Expression (absoluteOffset, TRUE,type) THEN
  1875. SkipLine;
  1876. END;
  1877. ELSIF ~(scope # NIL) & (ident = "ORG") THEN
  1878. NextSymbol;
  1879. IF (orgOffset # 0) OR ~Expression (orgOffset, TRUE,type) THEN
  1880. SkipLine;
  1881. END;
  1882. ELSIF ~(scope # NIL) & (ident = "RESB") THEN
  1883. NextSymbol;
  1884. IF ~Reserve (1) THEN SkipLine END;
  1885. ELSIF ~(scope # NIL) & (ident = "RESW") THEN
  1886. NextSymbol;
  1887. IF ~Reserve (2) THEN SkipLine END;
  1888. ELSIF ~(scope # NIL) & (ident = "RESD") THEN
  1889. NextSymbol;
  1890. IF ~Reserve (4) THEN SkipLine END;
  1891. (*
  1892. ELSIF ident = "EQU" THEN
  1893. IF currentLabel # NIL THEN
  1894. NextSymbol;
  1895. IF Expression (val2, FALSE) THEN
  1896. currentLabel.pc := val2;
  1897. currentLabel.equ := TRUE;
  1898. ELSE
  1899. SkipLine;
  1900. END;
  1901. ELSE
  1902. Error("???");
  1903. RETURN;
  1904. END;
  1905. *)
  1906. ELSIF ident = "TIMES" THEN
  1907. NextSymbol;
  1908. IF ~Expression (times, TRUE,type) THEN
  1909. SkipLine;
  1910. ELSIF times < 0 THEN
  1911. Error("unsupported negative value"); RETURN;
  1912. ELSE
  1913. prevPC := emitter.code.pc;
  1914. END;
  1915. ELSIF ident = "DB" THEN
  1916. IF ~PutData (bits8) THEN SkipLine END;
  1917. ELSIF ident = "DW" THEN
  1918. IF ~PutData (bits16) THEN SkipLine END;
  1919. ELSIF ident = "DD" THEN
  1920. IF ~PutData (bits32) THEN SkipLine END;
  1921. ELSIF ident = "DQ" THEN
  1922. IF ~PutData (bits64) THEN SkipLine END;
  1923. ELSIF ident = "REP" THEN
  1924. NextSymbol;
  1925. emitter.code.PutByte (InstructionSet.prfREP);
  1926. ELSIF ident = "LOCK" THEN
  1927. NextSymbol;
  1928. emitter.code.PutByte (InstructionSet.prfLOCK);
  1929. ELSIF ident = "REPE" THEN
  1930. NextSymbol;
  1931. emitter.code.PutByte (InstructionSet.prfREPE);
  1932. ELSIF ident = "REPZ" THEN
  1933. NextSymbol;
  1934. emitter.code.PutByte (InstructionSet.prfREPZ);
  1935. ELSIF ident = "REPNE" THEN
  1936. NextSymbol;
  1937. emitter.code.PutByte (InstructionSet.prfREPNE);
  1938. ELSIF ident = "REPNZ" THEN
  1939. NextSymbol;
  1940. emitter.code.PutByte (InstructionSet.prfREPNZ);
  1941. ELSIF ~GetInstruction () THEN
  1942. SkipLine
  1943. END;
  1944. currentLabel := NIL;
  1945. ELSIF (symbol = symNone) OR (symbol = symEnd) THEN
  1946. EXIT
  1947. ELSE
  1948. Error("identifier expected");
  1949. RETURN;
  1950. END;
  1951. END;
  1952. END;
  1953. (*
  1954. FixupLabels();
  1955. *)
  1956. (*! FixupLabels(labels.first,code) *)
  1957. sourceName := prevSourceName;
  1958. emitter.cpuBits := prevCpuBits;
  1959. emitter.cpuOptions := prevCpuOptions;
  1960. emitter.assembly := prevAssembly;
  1961. END Assemble;
  1962. END Assembly;
  1963. VAR kernelWriter: Streams.Writer;
  1964. PROCEDURE Ord (ch: CHAR): INTEGER;
  1965. BEGIN RETURN ORD (ch) - ORD ("0")
  1966. END Ord;
  1967. PROCEDURE HexOrd (ch: CHAR): INTEGER;
  1968. BEGIN
  1969. IF ch <= "9" THEN RETURN ORD (ch) - ORD ("0")
  1970. ELSE RETURN ORD (CAP (ch)) - ORD ("A") + 10
  1971. END
  1972. END HexOrd;
  1973. PROCEDURE IsRegisterOperand*(CONST op: Operand): BOOLEAN;
  1974. BEGIN
  1975. RETURN op.type IN {reg8, reg16, reg32, reg64, CRn, DRn, segReg, sti, mmx, xmm}
  1976. END IsRegisterOperand;
  1977. PROCEDURE IsMemoryOperand*(CONST op: Operand): BOOLEAN;
  1978. BEGIN RETURN op.type = mem
  1979. END IsMemoryOperand;
  1980. PROCEDURE IsImmediateOperand*(CONST op: Operand): BOOLEAN;
  1981. BEGIN RETURN op.type = imm
  1982. END IsImmediateOperand;
  1983. PROCEDURE DumpType*(w: Streams.Writer; type: LONGINT);
  1984. BEGIN
  1985. CASE type OF
  1986. reg8: w.String("reg8")
  1987. |reg16: w.String("reg16");
  1988. |reg32: w.String("reg32");
  1989. |reg64: w.String("reg64");
  1990. |CRn: w.String("CRn");
  1991. |DRn: w.String("DRn");
  1992. |segReg: w.String("segReg");
  1993. |mmx: w.String("mmx");
  1994. |xmm: w.String("xmm");
  1995. |mem: w.String("mem");
  1996. |sti: w.String("sti");
  1997. |imm: w.String("imm");
  1998. |ioffset: w.String("ioffset");
  1999. |pntr1616: w.String("pntr1616");
  2000. |pntr1632: w.String("pntr1632");
  2001. ELSE
  2002. w.String("?"); w.Int(type,1); w.String("?");
  2003. END;
  2004. END DumpType;
  2005. PROCEDURE DumpOperand*(w: Streams.Writer; CONST operand: Operand);
  2006. BEGIN
  2007. CASE operand.type OF
  2008. |reg8, reg16, reg32, reg64, CRn, DRn, segReg, sti, mmx, xmm:
  2009. w.String(InstructionSet.registers[operand.register].name);
  2010. |mem:
  2011. IF operand.sizeInBytes = 1 THEN w.String("BYTE ")
  2012. ELSIF operand.sizeInBytes= 2 THEN w.String("WORD ")
  2013. ELSIF operand.sizeInBytes = 4 THEN w.String("DWORD ")
  2014. ELSIF operand.sizeInBytes = 8 THEN w.String("QWORD ")
  2015. END;
  2016. w.String("[");
  2017. IF operand.register # none THEN
  2018. w.String(InstructionSet.registers[operand.register].name);
  2019. IF operand.index # none THEN w.String("+") END;
  2020. END;
  2021. IF operand.index # none THEN
  2022. w.String(InstructionSet.registers[operand.index].name);
  2023. IF operand.scale # 1 THEN
  2024. w.String("*"); w.Int(operand.scale,1);
  2025. END;
  2026. END;
  2027. IF operand.symbol.name # "" THEN
  2028. Basic.WriteSegmentedName(w, operand.symbol.name); w.String(":"); w.Int(operand.displacement,1);
  2029. IF operand.symbolOffset # 0 THEN w.String("(@"); w.Int(operand.symbolOffset,1); w.String(")") END;
  2030. ELSIF operand.displacement # 0 THEN
  2031. IF (operand.displacement > 0) & ((operand.register # none) OR (operand.index # none)) THEN w.String("+");END;
  2032. w.Int(operand.displacement,1);
  2033. END;
  2034. w.String("]");
  2035. |imm,ioffset:
  2036. IF operand.symbol.name # "" THEN
  2037. Basic.WriteSegmentedName(w, operand.symbol.name); w.String(":"); w.Int(operand.displacement,1);
  2038. IF operand.symbolOffset # 0 THEN w.String("(@"); w.Int(operand.symbolOffset,1); w.String(")") END;
  2039. ELSE
  2040. IF (operand.val > MAX(LONGINT)) OR (operand.val < MIN(LONGINT)) THEN
  2041. w.Hex(operand.val,1); w.String("H");
  2042. ELSE
  2043. w.Int(SHORT(operand.val),1);
  2044. END;
  2045. END;
  2046. |pntr1616:
  2047. |pntr1632:
  2048. ELSE
  2049. HALT(100)
  2050. END;
  2051. END DumpOperand;
  2052. PROCEDURE DumpInstruction(w: Streams.Writer; mnemonic: LONGINT; CONST operands: ARRAY OF Operand);
  2053. VAR i: LONGINT;
  2054. CONST DebugSize = FALSE;
  2055. BEGIN
  2056. IF mnemonic # none THEN
  2057. w.String(InstructionSet.mnemonics[mnemonic].name);
  2058. i := 0;
  2059. WHILE(i<maxNumberOperands) & (operands[i].type # none) DO
  2060. IF i = 0 THEN w.Char(09X) ELSE w.String(", ") END;
  2061. DumpOperand(w,operands[i]);
  2062. IF DebugSize THEN
  2063. w.String("(*"); DumpType(w,operands[i].type); w.String(":"); w.Int(operands[i].sizeInBytes,1); w.String("*)");
  2064. END;
  2065. INC(i);
  2066. END;
  2067. w.String("; ");
  2068. END;
  2069. END DumpInstruction;
  2070. PROCEDURE Matches(CONST operand: Operand; type: InstructionSet.OperandType): BOOLEAN;
  2071. PROCEDURE IsMemReg(regIndex: LONGINT): BOOLEAN;
  2072. BEGIN
  2073. RETURN InstructionSet.RegisterType(regIndex) IN {reg16, reg32, reg64}
  2074. END IsMemReg;
  2075. BEGIN
  2076. CASE operand.type OF
  2077. |reg8:
  2078. CASE type OF
  2079. InstructionSet.reg8, InstructionSet.regmem8:
  2080. RETURN TRUE;
  2081. | InstructionSet.AL, InstructionSet.rAX:
  2082. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2083. | InstructionSet.CL:
  2084. RETURN InstructionSet.RegisterIndex(operand.register) = RCX;
  2085. ELSE
  2086. RETURN FALSE;
  2087. END;
  2088. |reg16:
  2089. CASE type OF
  2090. InstructionSet.reg16, InstructionSet.regmem16:
  2091. RETURN TRUE;
  2092. | InstructionSet.AX, InstructionSet.rAX:
  2093. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2094. | InstructionSet.DX:
  2095. RETURN InstructionSet.RegisterIndex(operand.register) = RDX;
  2096. ELSE
  2097. RETURN FALSE;
  2098. END;
  2099. |reg32:
  2100. CASE type OF
  2101. InstructionSet.reg32, InstructionSet.regmem32:
  2102. RETURN TRUE;
  2103. | InstructionSet.EAX, InstructionSet.rAX:
  2104. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2105. ELSE
  2106. RETURN FALSE;
  2107. END;
  2108. |reg64:
  2109. CASE type OF
  2110. InstructionSet.reg64, InstructionSet.regmem64:
  2111. RETURN TRUE;
  2112. | InstructionSet.RAX, InstructionSet.rAX:
  2113. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2114. ELSE
  2115. RETURN FALSE;
  2116. END;
  2117. |CRn:
  2118. CASE type OF
  2119. InstructionSet.CRn:
  2120. RETURN TRUE;
  2121. | InstructionSet.CR8:
  2122. RETURN InstructionSet.RegisterIndex(operand.register) = 8;
  2123. ELSE
  2124. RETURN FALSE;
  2125. END;
  2126. |DRn:
  2127. RETURN type = InstructionSet.DRn;
  2128. |segReg:
  2129. CASE type OF
  2130. InstructionSet.segReg:
  2131. RETURN TRUE;
  2132. | InstructionSet.ES:
  2133. RETURN InstructionSet.RegisterIndex(operand.register) = segES;
  2134. | InstructionSet.CS:
  2135. RETURN InstructionSet.RegisterIndex(operand.register) = segCS;
  2136. | InstructionSet.SS:
  2137. RETURN InstructionSet.RegisterIndex(operand.register) = segSS;
  2138. | InstructionSet.DS:
  2139. RETURN InstructionSet.RegisterIndex(operand.register) = segDS;
  2140. | InstructionSet.FS:
  2141. RETURN InstructionSet.RegisterIndex(operand.register) = segFS;
  2142. | InstructionSet.GS:
  2143. RETURN InstructionSet.RegisterIndex(operand.register) = segGS;
  2144. ELSE
  2145. RETURN FALSE;
  2146. END
  2147. |sti:
  2148. CASE type OF
  2149. InstructionSet.sti:
  2150. RETURN TRUE;
  2151. | InstructionSet.st0:
  2152. RETURN InstructionSet.RegisterIndex(operand.register) = 0;
  2153. ELSE
  2154. RETURN FALSE;
  2155. END
  2156. |mmx:
  2157. CASE type OF
  2158. InstructionSet.mmx, InstructionSet.mmxmem32, InstructionSet.mmxmem64:
  2159. RETURN TRUE;
  2160. ELSE
  2161. RETURN FALSE;
  2162. END
  2163. |xmm:
  2164. CASE type OF
  2165. InstructionSet.xmm, InstructionSet.xmmmem32, InstructionSet.xmmmem64, InstructionSet.xmmmem128:
  2166. RETURN TRUE;
  2167. ELSE
  2168. RETURN FALSE;
  2169. END
  2170. |mem:
  2171. CASE type OF
  2172. | InstructionSet.mem:
  2173. RETURN TRUE;
  2174. | InstructionSet.mem8:
  2175. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8);
  2176. | InstructionSet.regmem8:
  2177. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2178. | InstructionSet.mem16:
  2179. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16);
  2180. | InstructionSet.regmem16:
  2181. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2182. | InstructionSet.mem32:
  2183. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32);
  2184. | InstructionSet.regmem32, InstructionSet.mmxmem32, InstructionSet.xmmmem32:
  2185. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2186. | InstructionSet.mem64:
  2187. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64);
  2188. | InstructionSet.regmem64, InstructionSet.mmxmem64, InstructionSet.xmmmem64:
  2189. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2190. | InstructionSet.mem128:
  2191. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits128);
  2192. | InstructionSet.xmmmem128:
  2193. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits128)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2194. | InstructionSet.moffset8:
  2195. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.register= none);
  2196. | InstructionSet.moffset16:
  2197. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.register= none);
  2198. | InstructionSet.moffset32:
  2199. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) & (operand.register= none);
  2200. | InstructionSet.moffset64:
  2201. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64)) & (operand.register= none);
  2202. ELSE
  2203. RETURN FALSE;
  2204. END;
  2205. |imm,ioffset:
  2206. CASE type OF
  2207. InstructionSet.one:
  2208. RETURN operand.val = 1
  2209. | InstructionSet.three:
  2210. RETURN operand.val = 3
  2211. | InstructionSet.rel8off:
  2212. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)
  2213. | InstructionSet.imm8:
  2214. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.val >= -80H) & (operand.val < 100H)
  2215. | InstructionSet.simm8:
  2216. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.val >= -80H) & (operand.val < 80H)
  2217. | InstructionSet.uimm8:
  2218. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.val >= 0H) & (operand.val < 100H)
  2219. | InstructionSet.rel16off:
  2220. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16) & FALSE (* do not allow 16 bit jumps *)
  2221. | InstructionSet.imm16:
  2222. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.val >= -8000H) & (operand.val < 10000H)
  2223. | InstructionSet.simm16:
  2224. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.val >= -8000H) & (operand.val < 8000H)
  2225. | InstructionSet.uimm16:
  2226. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.val >= 0H) & (operand.val < 10000H)
  2227. | InstructionSet.rel32off:
  2228. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32) (* & & (operand.val >= -80000000H) & (operand.val < 100000000H) PACO confused? *)
  2229. | InstructionSet.imm32:
  2230. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) (* & & (operand.val >= -80000000H) & (operand.val < 100000000H) PACO confused? *)
  2231. | InstructionSet.simm32:
  2232. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) (* & & (operand.val >= -80000000H) & (operand.val < 80000000H) PACO confused? *)
  2233. | InstructionSet.uimm32:
  2234. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) & (operand.val >= 0H) (* & (operand.val < 100000000H) PACO confused? *)
  2235. | InstructionSet.imm64:
  2236. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64)
  2237. ELSE
  2238. RETURN FALSE
  2239. END
  2240. |pntr1616:
  2241. RETURN type = InstructionSet.pntr1616;
  2242. |pntr1632:
  2243. RETURN type = InstructionSet.pntr1632;
  2244. ELSE
  2245. HALT(100)
  2246. END;
  2247. END Matches;
  2248. PROCEDURE ValueInByteRange (value: HUGEINT): BOOLEAN;
  2249. BEGIN RETURN SYSTEM.VAL (SHORTINT, value) = value
  2250. END ValueInByteRange;
  2251. PROCEDURE ValueInWordRange (value: HUGEINT): BOOLEAN;
  2252. BEGIN RETURN SYSTEM.VAL (INTEGER, value) = value
  2253. END ValueInWordRange;
  2254. PROCEDURE InitOperand*(VAR operand: Operand);
  2255. BEGIN
  2256. operand.type := none;
  2257. operand.index := none;
  2258. operand.register:= none;
  2259. operand.segment:= none;
  2260. operand.sizeInBytes := none;
  2261. operand.scale := 1;
  2262. operand.displacement := 0;
  2263. operand.val := 0;
  2264. operand.pc := none;
  2265. operand.symbol.name := "";
  2266. operand.symbol.fingerprint := 0;
  2267. operand.selector := none;
  2268. operand.offset := 0;
  2269. END InitOperand;
  2270. PROCEDURE InitRegister* (VAR operand: Operand; register: Register);
  2271. BEGIN
  2272. InitOperand(operand);
  2273. operand.type := InstructionSet.RegisterType(register);
  2274. operand.register :=register;
  2275. CASE operand.type OF
  2276. reg8,reg16,reg32,reg64,segReg,CRn,DRn,sti,xmm,mmx: (* ok *)
  2277. |InstructionSet.st0: operand.type := InstructionSet.sti;
  2278. ELSE
  2279. HALT(100);
  2280. END;
  2281. operand.sizeInBytes := InstructionSet.registers[register].sizeInBytes
  2282. END InitRegister;
  2283. PROCEDURE NewRegister*(register: Register): Operand;
  2284. VAR operand: Operand;
  2285. BEGIN InitRegister(operand,register); RETURN operand
  2286. END NewRegister;
  2287. PROCEDURE InitMem*(VAR operand: Operand; size: Size; reg: Register; displacement: LONGINT);
  2288. BEGIN
  2289. InitOperand(operand);
  2290. operand.type := mem;
  2291. operand.sizeInBytes := size;
  2292. operand.register:= reg;
  2293. operand.displacement := displacement;
  2294. operand.scale := 1;
  2295. END InitMem;
  2296. PROCEDURE SetIndexScale*(VAR operand: Operand; index: Register; scale: LONGINT);
  2297. BEGIN
  2298. operand.index := index;
  2299. operand.scale := scale
  2300. END SetIndexScale;
  2301. PROCEDURE NewMem*(size: Size; reg: Register; displacement: LONGINT): Operand;
  2302. VAR operand: Operand;
  2303. BEGIN
  2304. InitMem(operand,size,reg,displacement); RETURN operand
  2305. END NewMem;
  2306. PROCEDURE InitMem8* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2307. BEGIN InitMem (operand, bits8, reg, displacement);
  2308. END InitMem8;
  2309. PROCEDURE NewMem8* (reg: Register; displacement: LONGINT): Operand;
  2310. VAR operand: Operand;
  2311. BEGIN InitMem8 (operand,reg, displacement); RETURN operand
  2312. END NewMem8;
  2313. PROCEDURE InitMem16* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2314. BEGIN InitMem (operand,bits16, reg, displacement);
  2315. END InitMem16;
  2316. PROCEDURE NewMem16* (reg: Register; displacement: LONGINT): Operand;
  2317. VAR operand: Operand;
  2318. BEGIN InitMem16 (operand,reg, displacement); RETURN operand
  2319. END NewMem16;
  2320. PROCEDURE InitMem32* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2321. BEGIN InitMem (operand,bits32, reg, displacement);
  2322. END InitMem32;
  2323. PROCEDURE NewMem32* (reg: Register; displacement: LONGINT): Operand;
  2324. VAR operand: Operand;
  2325. BEGIN InitMem32 (operand,reg, displacement); RETURN operand
  2326. END NewMem32;
  2327. PROCEDURE InitMem64* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2328. BEGIN InitMem (operand,bits64, reg, displacement);
  2329. END InitMem64;
  2330. PROCEDURE NewMem64* (reg: Register; displacement: LONGINT): Operand;
  2331. VAR operand: Operand;
  2332. BEGIN InitMem64 (operand,reg, displacement); RETURN operand
  2333. END NewMem64;
  2334. PROCEDURE InitMem128* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2335. BEGIN InitMem (operand,bits128, reg, displacement);
  2336. END InitMem128;
  2337. PROCEDURE NewMem128* (reg: Register; displacement: LONGINT): Operand;
  2338. VAR operand: Operand;
  2339. BEGIN InitMem128 (operand,reg, displacement); RETURN operand
  2340. END NewMem128;
  2341. PROCEDURE SetSymbol*(VAR operand: Operand; symbol: Sections.SectionName; fingerprint: LONGINT; symbolOffset, displacement: LONGINT);
  2342. BEGIN
  2343. operand.symbol.name := symbol;
  2344. operand.symbol.fingerprint := fingerprint;
  2345. operand.symbolOffset := symbolOffset; operand.displacement := displacement;
  2346. END SetSymbol;
  2347. PROCEDURE InitImm* (VAR operand: Operand; size: SHORTINT; val: HUGEINT);
  2348. BEGIN InitOperand(operand); operand.type := imm; operand.sizeInBytes := size; operand.val := val;
  2349. END InitImm;
  2350. PROCEDURE InitImm8* (VAR operand: Operand; val: HUGEINT);
  2351. BEGIN InitImm (operand, bits8, val);
  2352. END InitImm8;
  2353. PROCEDURE NewImm8*(val: HUGEINT): Operand;
  2354. VAR operand: Operand;
  2355. BEGIN InitImm8(operand,val); RETURN operand
  2356. END NewImm8;
  2357. PROCEDURE InitImm16* (VAR operand: Operand; val: HUGEINT);
  2358. BEGIN InitImm (operand, bits16, val);
  2359. END InitImm16;
  2360. PROCEDURE NewImm16*(val: HUGEINT): Operand;
  2361. VAR operand:Operand;
  2362. BEGIN InitImm16(operand,val); RETURN operand
  2363. END NewImm16;
  2364. PROCEDURE InitImm32* (VAR operand: Operand; val: HUGEINT);
  2365. BEGIN InitImm (operand, bits32, val);
  2366. END InitImm32;
  2367. PROCEDURE NewImm32*(val: HUGEINT): Operand;
  2368. VAR operand: Operand;
  2369. BEGIN InitImm32(operand,val); RETURN operand
  2370. END NewImm32;
  2371. PROCEDURE InitImm64* (VAR operand: Operand; val: HUGEINT);
  2372. BEGIN InitImm (operand, bits64, val);
  2373. END InitImm64;
  2374. PROCEDURE NewImm64*(val: HUGEINT): Operand;
  2375. VAR operand: Operand;
  2376. BEGIN InitImm64(operand,val); RETURN operand
  2377. END NewImm64;
  2378. PROCEDURE InitOffset* (VAR operand: Operand; size: SHORTINT; val: HUGEINT);
  2379. BEGIN InitOperand(operand); operand.type := ioffset; operand.sizeInBytes := size; operand.val := val;
  2380. END InitOffset;
  2381. PROCEDURE InitOffset8* (VAR operand: Operand; val: HUGEINT);
  2382. BEGIN InitOffset (operand, bits8, val);
  2383. END InitOffset8;
  2384. PROCEDURE NewOffset8*(val: HUGEINT): Operand;
  2385. VAR operand: Operand;
  2386. BEGIN InitOffset8(operand,val); RETURN operand
  2387. END NewOffset8;
  2388. PROCEDURE InitOffset16* (VAR operand: Operand; val: HUGEINT);
  2389. BEGIN InitOffset (operand, bits16, val);
  2390. END InitOffset16;
  2391. PROCEDURE NewOffset16*(val: HUGEINT): Operand;
  2392. VAR operand: Operand;
  2393. BEGIN InitOffset16(operand,val); RETURN operand
  2394. END NewOffset16;
  2395. PROCEDURE InitOffset32* (VAR operand: Operand; val: HUGEINT);
  2396. BEGIN InitOffset (operand, bits32, val);
  2397. END InitOffset32;
  2398. PROCEDURE NewOffset32*(val: HUGEINT): Operand;
  2399. VAR operand: Operand;
  2400. BEGIN InitOffset32(operand,val); RETURN operand
  2401. END NewOffset32;
  2402. PROCEDURE InitOffset64* (VAR operand: Operand; val: HUGEINT);
  2403. BEGIN InitOffset (operand, bits64, val);
  2404. END InitOffset64;
  2405. PROCEDURE NewOffset64*(val: HUGEINT): Operand;
  2406. VAR operand: Operand;
  2407. BEGIN InitOffset64(operand,val); RETURN operand
  2408. END NewOffset64;
  2409. PROCEDURE InitPntr1616* (VAR operand: Operand; s, o: LONGINT);
  2410. BEGIN InitOperand(operand); operand.type := pntr1616; operand.selector := s; operand.offset := o;
  2411. END InitPntr1616;
  2412. PROCEDURE InitPntr1632* (VAR operand: Operand; s, o: LONGINT);
  2413. BEGIN InitOperand(operand); operand.type := pntr1632; operand.selector := s; operand.offset := o;
  2414. END InitPntr1632;
  2415. PROCEDURE SetSize*(VAR operand: Operand;sizeInBytes: Size);
  2416. BEGIN operand.sizeInBytes := sizeInBytes
  2417. END SetSize;
  2418. PROCEDURE SameOperand*(CONST left,right: Operand): BOOLEAN;
  2419. BEGIN
  2420. IF (left.type # right.type) OR (left.sizeInBytes # right.sizeInBytes) OR (left.symbol # right.symbol) THEN RETURN FALSE END;
  2421. CASE left.type OF
  2422. reg8,reg16,reg32,reg64,segReg,CRn,DRn,sti,xmm,mmx: RETURN left.register = right.register
  2423. | imm,ioffset: RETURN (left.val = right.val) & ((left.symbol.name="") OR (left.displacement = right.displacement))
  2424. | mem:RETURN (left.register = right.register) & (left.displacement = right.displacement) & (left.index = right.index) & (left.scale = right.scale)
  2425. | pntr1616,pntr1632: RETURN (left.selector=right.selector) & (left.offset=right.offset)
  2426. END;
  2427. RETURN FALSE
  2428. END SameOperand;
  2429. PROCEDURE Test*(context: Commands.Context);
  2430. VAR assembly: Emitter;
  2431. (*errorHandler: ErrorHandler; *)
  2432. op1,op2,op3: Operand;
  2433. diagnostics: Diagnostics.StreamDiagnostics;
  2434. code: Code;
  2435. pooledName: Basic.SegmentedName;
  2436. PROCEDURE Op(CONST name: ARRAY OF CHAR): LONGINT;
  2437. BEGIN
  2438. RETURN InstructionSet.FindMnemonic(name)
  2439. END Op;
  2440. BEGIN
  2441. InitOperand(op1); InitOperand(op2); InitOperand(op3);
  2442. NEW(diagnostics,context.error);
  2443. Basic.ToSegmentedName("test", pooledName);
  2444. NEW(code,Sections.CodeSection,8,0,pooledName,TRUE,TRUE);
  2445. NEW(assembly,diagnostics);
  2446. assembly.SetCode(code);
  2447. InitRegister(op1,InstructionSet.regEAX);
  2448. InitImm32(op2,10);
  2449. assembly.Emit2(Op("MOV"),op1,op2);
  2450. context.out.Update;
  2451. code.Dump(context.out);
  2452. END Test;
  2453. BEGIN
  2454. IF Trace THEN
  2455. NEW(kernelWriter,KernelLog.Send,1000);
  2456. END;
  2457. END FoxAMD64Assembler.
  2458. OCAMD64Assembler.Test ~