FoxAMD64Assembler.Mod 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692
  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. (* overal state *)
  180. cpuBits: Size; (* supported bit width for this cpu / target *)
  181. cpuOptions: InstructionSet.CPUOptions;
  182. dump: Streams.Writer;
  183. PROCEDURE & InitEmitter*(diagnostics: Diagnostics.Diagnostics);
  184. BEGIN
  185. SELF.diagnostics := diagnostics;
  186. cpuBits := bits32; cpuOptions := {0..31};
  187. error := FALSE;
  188. END InitEmitter;
  189. PROCEDURE SetCode*(code: BinaryCode.Section);
  190. BEGIN SELF.code := code;
  191. dump := code.comments
  192. END SetCode;
  193. PROCEDURE SetBits* (numberBits: LONGINT): BOOLEAN;
  194. BEGIN
  195. CASE numberBits OF
  196. 16: cpuBits := bits16;
  197. | 32: cpuBits := bits32;
  198. | 64: cpuBits := bits64;
  199. ELSE
  200. Error("number bits not supported");
  201. RETURN FALSE;
  202. END;
  203. RETURN TRUE;
  204. END SetBits;
  205. PROCEDURE Error(CONST message: ARRAY OF CHAR);
  206. VAR msg,name: ARRAY 256 OF CHAR;
  207. BEGIN
  208. COPY(message,msg);
  209. Strings.Append(msg," in ");
  210. ObjectFile.SegmentedNameToString(code.os.identifier.name,name);
  211. Strings.Append(msg, name);
  212. IF diagnostics # NIL THEN
  213. diagnostics.Error("",Diagnostics.Invalid,Diagnostics.Invalid,msg);
  214. END;
  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: LONGINT;
  964. error-: BOOLEAN;
  965. emitter: Emitter;
  966. (* overal state *)
  967. diagnostics: Diagnostics.Diagnostics;
  968. dump: Streams.Writer;
  969. (* temporaries *)
  970. fixup: BinaryCode.Fixup;
  971. type: SHORTINT;
  972. currentFixup: Sections.SectionName;
  973. currentLabel: NamedLabel;
  974. sourceName: Basic.FileName;
  975. PROCEDURE & InitAssembly*(diagnostics: Diagnostics.Diagnostics; emit: Emitter);
  976. BEGIN
  977. SELF.diagnostics := diagnostics;
  978. errPos := Diagnostics.Invalid;
  979. error := FALSE;
  980. SELF.emitter := emit;
  981. sourceName := "";
  982. END InitAssembly;
  983. PROCEDURE Error( CONST message: ARRAY OF CHAR);
  984. VAR pos: LONGINT; msg,name: ARRAY 256 OF CHAR;
  985. BEGIN
  986. pos := errPos;
  987. IF (pos = Diagnostics.Invalid) OR (sourceName = "") THEN
  988. COPY(message,msg);
  989. Strings.Append(msg," in ");
  990. ObjectFile.SegmentedNameToString(emitter.code.os.identifier.name, name);
  991. Strings.Append(msg, name);
  992. diagnostics.Error(sourceName,errPos,Diagnostics.Invalid,msg);
  993. ELSE
  994. diagnostics.Error(sourceName,errPos,Diagnostics.Invalid,message);
  995. END;
  996. error := TRUE;
  997. IF dump # NIL THEN dump.Update; END;
  998. END Error;
  999. PROCEDURE ErrorSS(CONST msg1,msg2: ARRAY OF CHAR);
  1000. VAR message: ARRAY 256 OF CHAR;
  1001. BEGIN
  1002. COPY(msg1,message);
  1003. Strings.Append(message," : ");
  1004. Strings.Append(message, msg2);
  1005. Error(message);
  1006. END ErrorSS;
  1007. PROCEDURE Assemble* (reader: Streams.Reader; orgPos: LONGINT; scope: SyntaxTree.Scope; in: IntermediateCode.Section; out: IntermediateCode.Section; module: Sections.Module; exported, inlined: BOOLEAN;
  1008. map: RegisterMap
  1009. );
  1010. CONST maxPasses = 2;
  1011. VAR
  1012. symbol, reg: LONGINT;
  1013. ident, idents: Name;
  1014. val, times, val2, val3: LONGINT;
  1015. currentLabel: NamedLabel;
  1016. labels: NamedLabelList;
  1017. prevPC: LONGINT;
  1018. pass: LONGINT;
  1019. absoluteMode: BOOLEAN;
  1020. absoluteOffset: LONGINT;
  1021. orgOffset: LONGINT;
  1022. char: CHAR;
  1023. orgReaderPos: LONGINT;
  1024. orgCodePos: LONGINT;
  1025. prevSourceName: Basic.FileName;
  1026. position: LONGINT;
  1027. prevCpuBits: Size;
  1028. prevCpuOptions: InstructionSet.CPUOptions;
  1029. PROCEDURE NextChar;
  1030. BEGIN
  1031. (*
  1032. IF (dump # NIL) & (pass = maxPasses) THEN dump.Char (char) END;
  1033. *)
  1034. reader.Char(char); INC(position);
  1035. END NextChar;
  1036. PROCEDURE SkipBlanks;
  1037. BEGIN
  1038. (* tf returns 01X when an embedded object is encountered *)
  1039. WHILE (char = SPACE) OR (char = TAB) OR (char = 01X) DO NextChar END;
  1040. IF char = ";" THEN
  1041. WHILE (char # CR) & (char # LF) & (char # 0X) DO NextChar END (* Skip comments *)
  1042. END;
  1043. END SkipBlanks;
  1044. PROCEDURE GetNumber (VAR intval: LONGINT);
  1045. VAR i, m, n: INTEGER; dig: ARRAY 24 OF CHAR;
  1046. BEGIN
  1047. i := 0; m := 0; n := 0;
  1048. WHILE ('0' <= char) & (char <= '9') OR ('A' <= CAP (char)) & (CAP (char) <= 'F') DO
  1049. IF (m > 0) OR (char # "0") THEN (* ignore leading zeros *)
  1050. IF n < LEN(dig) THEN dig[n] := char; INC(n) END;
  1051. INC(m)
  1052. END;
  1053. NextChar; INC(i)
  1054. END;
  1055. IF n = m THEN intval := 0; i := 0;
  1056. IF (CAP (char) = "H") OR (char = "X") THEN NextChar;
  1057. IF (n = Scanner.MaxHexDigits) & (dig[0] > "7") THEN (* prevent overflow *) intval := -1 END;
  1058. WHILE i < n DO intval := intval * 10H + HexOrd (dig[i]); INC(i) END;
  1059. ELSE
  1060. IF (n = Scanner.MaxHugeHexDigits) & (dig[0] > "7") THEN (* prevent overflow *) intval := -1 END;
  1061. WHILE i < n DO intval := intval * 10 + Ord (dig[i]); INC(i) END
  1062. END
  1063. END;
  1064. END GetNumber;
  1065. PROCEDURE GetIdentifier;
  1066. VAR i: LONGINT;
  1067. BEGIN
  1068. i := 0;
  1069. REPEAT
  1070. IF i < Scanner.MaxIdentifierLength - 1 THEN
  1071. IF ('0' <= char) & (char <= '9') THEN
  1072. ident[i] := char; idents[i] := char;
  1073. ELSE
  1074. ident[i] := (* CAP *) (char); idents[i] := char; END;
  1075. INC (i);
  1076. END;
  1077. NextChar
  1078. UNTIL ~((('A' <= CAP(char)) & (CAP(char) <= 'Z')) OR (('0' <= char) & (char <= '9')));
  1079. ident[i] := 0X; idents[i] := 0X;
  1080. END GetIdentifier;
  1081. PROCEDURE GetString;
  1082. VAR i: LONGINT;
  1083. BEGIN
  1084. i := 0;
  1085. NextChar;
  1086. WHILE (char # "'") & (i < Scanner.MaxIdentifierLength - 1) DO
  1087. ident[i] := char; INC (i);
  1088. NextChar;
  1089. END;
  1090. ident[i] := 0X;
  1091. NextChar;
  1092. END GetString;
  1093. PROCEDURE NextSymbol;
  1094. BEGIN
  1095. SkipBlanks;
  1096. errPos := position- 1;
  1097. CASE char OF
  1098. 'A' .. 'Z', 'a' .. 'z' :
  1099. GetIdentifier;
  1100. SkipBlanks;
  1101. IF char = ':' THEN
  1102. NextChar; symbol := symLabel;
  1103. ELSE
  1104. symbol := symIdent;
  1105. END;
  1106. | '0' .. '9':
  1107. GetNumber (val);
  1108. symbol := symNumber;
  1109. | "'": GetString;
  1110. symbol := symString;
  1111. | '.': symbol := symPeriod;
  1112. NextChar;
  1113. | ';': symbol := symSemicolon;
  1114. NextChar;
  1115. | ':': symbol := symColon;
  1116. NextChar;
  1117. | CR, LF: symbol := symLn;
  1118. NextChar;
  1119. | ',': symbol := symComma;
  1120. NextChar;
  1121. | '+': symbol := symPlus;
  1122. NextChar;
  1123. | '-': symbol := symMinus;
  1124. NextChar;
  1125. | '*': symbol := symTimes;
  1126. NextChar;
  1127. | '/': symbol := symDiv;
  1128. NextChar;
  1129. | '%': symbol := symMod;
  1130. NextChar;
  1131. | '~': symbol := symNegate;
  1132. NextChar;
  1133. | '(': symbol := symLParen;
  1134. NextChar;
  1135. | ')': symbol := symRParen;
  1136. NextChar;
  1137. | '[': symbol := symLBraket;
  1138. NextChar;
  1139. | ']': symbol := symRBraket;
  1140. NextChar;
  1141. | '{': symbol := symLBrace;
  1142. NextChar;
  1143. | '}': symbol := symRBrace;
  1144. NextChar;
  1145. | '@': symbol := symAt;
  1146. NextChar;
  1147. | '$': NextChar;
  1148. IF char = '$' THEN
  1149. symbol := symPCOffset; NextChar;
  1150. ELSE
  1151. symbol := symPC;
  1152. END
  1153. | 0X: symbol := symEnd;
  1154. ELSE
  1155. symbol := symNone;
  1156. NextChar;
  1157. END;
  1158. END NextSymbol;
  1159. PROCEDURE SkipLine;
  1160. BEGIN
  1161. WHILE (symbol # symLn) & (symbol # symNone) DO
  1162. NextSymbol;
  1163. END;
  1164. END SkipLine;
  1165. PROCEDURE Ensure (desiredSymbol, errNumber : LONGINT) : BOOLEAN;
  1166. VAR temp: LONGINT;
  1167. BEGIN
  1168. temp := symbol;
  1169. IF symbol = desiredSymbol THEN
  1170. NextSymbol;
  1171. RETURN TRUE;
  1172. ELSE
  1173. Error("other symbol expected");
  1174. RETURN FALSE;
  1175. END;
  1176. END Ensure;
  1177. PROCEDURE GetCPU (cumulateOptions: BOOLEAN): BOOLEAN;
  1178. VAR i: LONGINT;
  1179. BEGIN
  1180. SkipBlanks;
  1181. GetIdentifier;
  1182. Strings.UpperCase(ident);
  1183. i := InstructionSet.FindCPU (ident);
  1184. IF i # InstructionSet.none THEN
  1185. IF cumulateOptions THEN
  1186. emitter.cpuOptions := emitter.cpuOptions + InstructionSet.cpus[i].cpuOptions;
  1187. ELSE
  1188. emitter.cpuOptions := InstructionSet.cpus[i].cpuOptions + InstructionSet.cpuOptions;
  1189. END;
  1190. NextSymbol;
  1191. RETURN TRUE;
  1192. ELSE
  1193. ErrorSS ("cpu unknown",ident);
  1194. emitter.cpuOptions := prevCpuOptions;
  1195. RETURN FALSE;
  1196. END;
  1197. END GetCPU;
  1198. PROCEDURE Factor (VAR x: LONGINT; critical: BOOLEAN; VAR type: SHORTINT): BOOLEAN;
  1199. VAR label: NamedLabel; l: LONGINT;
  1200. BEGIN
  1201. IF symbol = symNumber THEN
  1202. x := val; NextSymbol; RETURN TRUE;
  1203. ELSIF symbol = symPC THEN
  1204. x := (orgOffset + emitter.code.pc ); NextSymbol; RETURN TRUE;
  1205. ELSIF symbol = symPCOffset THEN
  1206. x := orgOffset; NextSymbol; RETURN TRUE;
  1207. ELSIF symbol = symString THEN
  1208. x := 0; l := Strings.Length (ident);
  1209. IF l > 0 THEN INC (x, ORD (ident [0])) END;
  1210. IF l > 1 THEN INC (x, ORD (ident [1])*100H) END;
  1211. IF l > 2 THEN INC (x, ORD (ident [2])*10000H) END;
  1212. IF l > 3 THEN INC (x, ORD (ident [3])*1000000H) END;
  1213. NextSymbol; RETURN TRUE;
  1214. ELSIF symbol = symIdent THEN
  1215. label := labels.Find (idents);
  1216. NextSymbol;
  1217. IF label # NIL THEN
  1218. x := (label.offset );
  1219. type := ioffset;
  1220. currentLabel := label;
  1221. (*
  1222. IF x = MAX(LONGINT) THEN
  1223. x := -label.index;
  1224. currentFixup := in;
  1225. END;
  1226. *)
  1227. RETURN TRUE;
  1228. ELSIF scope # NIL THEN
  1229. IF ~GetValue(idents,x) THEN
  1230. IF (pass = maxPasses) THEN
  1231. Error("constant expected");
  1232. END;
  1233. RETURN FALSE;
  1234. ELSE
  1235. RETURN TRUE;
  1236. END
  1237. END;
  1238. IF (~critical) & (pass # maxPasses) THEN
  1239. x := 0;
  1240. RETURN TRUE
  1241. END;
  1242. Error("undefined symbol");
  1243. RETURN FALSE;
  1244. ELSIF symbol = symLParen THEN
  1245. NextSymbol;
  1246. RETURN Expression (x, critical,type) & Ensure (symRParen, 555);
  1247. END;
  1248. Error("parse error in expression");
  1249. RETURN FALSE
  1250. END Factor;
  1251. PROCEDURE Term (VAR x: LONGINT; critical: BOOLEAN; VAR type: SHORTINT): BOOLEAN;
  1252. VAR y, op : LONGINT;
  1253. BEGIN
  1254. IF Factor (x, critical,type) THEN
  1255. WHILE (symbol = symTimes) OR (symbol = symDiv) OR (symbol = symMod) DO
  1256. op := symbol; NextSymbol;
  1257. IF Factor (y, critical,type) THEN
  1258. IF op = symTimes THEN x := x * y
  1259. ELSIF op = symDiv THEN x := x DIV y
  1260. ELSE x := x MOD y
  1261. END;
  1262. ELSE
  1263. RETURN FALSE;
  1264. END;
  1265. END;
  1266. RETURN TRUE;
  1267. ELSE
  1268. RETURN FALSE;
  1269. END;
  1270. END Term;
  1271. PROCEDURE Expression (VAR x: LONGINT; critical: BOOLEAN; VAR type: SHORTINT): BOOLEAN;
  1272. VAR y, op : LONGINT;
  1273. BEGIN
  1274. IF symbol = symMinus THEN
  1275. op := symbol; NextSymbol;
  1276. IF Term (x, critical,type) THEN
  1277. x := -x
  1278. ELSE
  1279. RETURN FALSE;
  1280. END;
  1281. ELSIF symbol = symPlus THEN
  1282. op := symbol; NextSymbol;
  1283. IF ~Term (x, critical,type) THEN
  1284. RETURN FALSE;
  1285. END;
  1286. ELSIF symbol = symNegate THEN
  1287. op := symbol; NextSymbol;
  1288. IF Term (x, critical,type) THEN
  1289. x := -x - 1
  1290. ELSE
  1291. RETURN FALSE;
  1292. END;
  1293. ELSIF ~Term (x, critical,type) THEN
  1294. RETURN FALSE;
  1295. END;
  1296. WHILE (symbol = symPlus) OR (symbol = symMinus) DO
  1297. op := symbol; NextSymbol;
  1298. IF Term (y, critical,type) THEN
  1299. IF op = symPlus THEN x := x + y ELSE x := x - y END;
  1300. ELSE
  1301. RETURN FALSE;
  1302. END;
  1303. END;
  1304. RETURN TRUE;
  1305. END Expression;
  1306. PROCEDURE PutData (size: Size): BOOLEAN;
  1307. VAR i: LONGINT; type:SHORTINT; ofs: Operand;
  1308. BEGIN
  1309. NextSymbol;
  1310. WHILE symbol # symLn DO
  1311. IF symbol = symString THEN
  1312. i := 0;
  1313. WHILE ident[i] # 0X DO
  1314. emitter.code.PutByte (ORD (ident[i]));
  1315. INC (i);
  1316. END;
  1317. IF size # bits8 THEN
  1318. i := (size ) - i MOD (size );
  1319. WHILE i # 0 DO emitter.code.PutByte (0); DEC (i) END;
  1320. END;
  1321. NextSymbol;
  1322. ELSIF (scope # NIL) & (symbol = symAt) THEN
  1323. NextSymbol;
  1324. IF symbol # symIdent THEN Error("identifier missing") END;
  1325. GetOffsetFixup (idents, ofs);
  1326. NextSymbol;
  1327. IF symbol = symPlus THEN
  1328. NextSymbol;
  1329. IF Expression(i, FALSE, type) THEN
  1330. ofs.displacement := i
  1331. END;
  1332. ELSIF symbol = symMinus THEN
  1333. NextSymbol;
  1334. IF Expression(i, FALSE, type) THEN
  1335. ofs.displacement := - i
  1336. END;
  1337. END;
  1338. IF pass = maxPasses THEN
  1339. emitter.AddFixup(BinaryCode.Absolute, ofs.sizeInBytes, emitter.code.pc, ofs.symbol, ofs.symbolOffset,ofs.displacement);
  1340. END;
  1341. emitter.code.PutBytes (0, size );
  1342. ELSIF Expression (i, FALSE,type) THEN
  1343. emitter.code.PutBytes (i, size );
  1344. ELSE
  1345. RETURN FALSE;
  1346. END;
  1347. IF symbol = symComma THEN
  1348. NextSymbol;
  1349. ELSIF symbol # symLn THEN
  1350. Error("operand missing");
  1351. END
  1352. END;
  1353. Duplicate ((emitter.code.pc - prevPC) , NIL);
  1354. RETURN TRUE;
  1355. END PutData;
  1356. PROCEDURE Duplicate (size: LONGINT; fixup: BinaryCode.Fixup);
  1357. VAR i: LONGINT; buffer: ARRAY 100 OF CHAR; pc: LONGINT;
  1358. BEGIN
  1359. IF times = 1 THEN RETURN END;
  1360. pc := (prevPC );
  1361. IF (dump # NIL) & (pass = maxPasses) THEN dump.Hex (emitter.code.pc, 1); dump.Char (' ') END;
  1362. FOR i := 0 TO size - 1 DO
  1363. buffer[i] := emitter.code.GetByte (pc); INC(pc);
  1364. IF (dump # NIL) & (pass = maxPasses) THEN dump.Hex (ORD (buffer[i]), -2); END;
  1365. END;
  1366. pc := (prevPC );
  1367. IF times > 1 THEN
  1368. WHILE times # 1 DO
  1369. IF fixup # NIL THEN
  1370. HALT(200);
  1371. (*!!
  1372. AddFixup (fixup.adr, pc + fixup.offset - prevPC);
  1373. *)
  1374. END;
  1375. FOR i := 0 TO size - 1 DO
  1376. emitter.code.PutByteAt (pc, ORD (buffer[i])); INC(pc);
  1377. IF (dump # NIL) & (pass = maxPasses) THEN dump.Hex (ORD (buffer[i]), -2); END;
  1378. END;
  1379. DEC (times);
  1380. END;
  1381. ELSE
  1382. times := 1;
  1383. END;
  1384. IF (dump # NIL) & (pass = maxPasses) THEN dump.Ln END;
  1385. END Duplicate;
  1386. PROCEDURE Reserve (size: Size) : BOOLEAN;
  1387. VAR type : SHORTINT;
  1388. BEGIN
  1389. IF Expression (val2, TRUE, type) THEN
  1390. absoluteOffset := absoluteOffset + val2 * size;
  1391. RETURN TRUE;
  1392. ELSE
  1393. RETURN FALSE;
  1394. END;
  1395. END Reserve;
  1396. PROCEDURE GetScopeSymbol (CONST ident: ARRAY OF CHAR): SyntaxTree.Symbol;
  1397. VAR sym: SyntaxTree.Symbol; localScope: SyntaxTree.Scope; identifier: SyntaxTree.Identifier;
  1398. BEGIN
  1399. localScope := scope;
  1400. identifier := SyntaxTree.NewIdentifier(ident);
  1401. IF Trace THEN KernelLog.String("GetScopeSymbol:"); KernelLog.String(ident); KernelLog.Ln; END;
  1402. WHILE (sym = NIL) & (localScope # NIL) DO
  1403. sym := localScope.FindSymbol(identifier);
  1404. localScope := localScope.outerScope
  1405. END;
  1406. IF (sym # NIL) & (sym IS SyntaxTree.Import) THEN
  1407. NextSymbol;
  1408. IF Ensure(symPeriod,0) & (symbol = symIdent) THEN
  1409. identifier := SyntaxTree.NewIdentifier(idents);
  1410. IF Trace THEN KernelLog.String("GetScopeSymbol :"); KernelLog.String(idents); KernelLog.Ln; END;
  1411. localScope := sym(SyntaxTree.Import).module.moduleScope;
  1412. sym := NIL;
  1413. WHILE (sym = NIL) & (localScope # NIL) DO
  1414. sym := localScope.FindSymbol(identifier);
  1415. localScope := localScope.outerScope
  1416. END;
  1417. END;
  1418. END;
  1419. IF Trace THEN IF sym = NIL THEN KernelLog.String("not found") ELSE KernelLog.String("found"); END; KernelLog.Ln; END;
  1420. RETURN sym
  1421. END GetScopeSymbol;
  1422. PROCEDURE GetValue(CONST ident: ARRAY OF CHAR; VAR x: LONGINT): BOOLEAN;
  1423. VAR scopeSymbol:SyntaxTree.Symbol;
  1424. BEGIN
  1425. scopeSymbol := GetScopeSymbol (ident);
  1426. IF scopeSymbol = NIL THEN RETURN FALSE
  1427. ELSIF ~(scopeSymbol IS SyntaxTree.Constant) THEN RETURN FALSE
  1428. ELSE
  1429. IF (scopeSymbol.type.resolved IS SyntaxTree.CharacterType) & (scopeSymbol.type.resolved.sizeInBits=8) THEN
  1430. x := ORD(scopeSymbol(SyntaxTree.Constant).value.resolved(SyntaxTree.CharacterValue).value)
  1431. ELSIF scopeSymbol.type.resolved IS SyntaxTree.IntegerType THEN
  1432. x := scopeSymbol(SyntaxTree.Constant).value.resolved(SyntaxTree.IntegerValue).value
  1433. ELSE
  1434. Error("number expected");
  1435. RETURN FALSE;
  1436. END;
  1437. RETURN TRUE;
  1438. END;
  1439. END GetValue;
  1440. PROCEDURE GetMemFixup (CONST ident: ARRAY OF CHAR; VAR operand: Operand);
  1441. VAR scopeSymbol:SyntaxTree.Symbol;
  1442. BEGIN
  1443. scopeSymbol := GetScopeSymbol (ident);
  1444. IF scopeSymbol = NIL THEN RETURN END;
  1445. IF scopeSymbol IS SyntaxTree.Constant THEN
  1446. RETURN
  1447. END;
  1448. IF inlined & exported THEN
  1449. Error("no symbols may be accessed in exported and inlined procedures");
  1450. END;
  1451. IF (scopeSymbol IS SyntaxTree.Variable) & (scopeSymbol.scope = module.module.moduleScope) THEN (* global variable. offset not supported *)
  1452. Error("global variables cannot be accessed as memory operands");
  1453. ELSIF (scopeSymbol IS SyntaxTree.Variable) THEN (* local variable *)
  1454. operand.displacement := (scopeSymbol.offsetInBits DIV 8)
  1455. ELSIF (scopeSymbol IS SyntaxTree.Parameter) THEN (* local parameter *)
  1456. operand.displacement := (scopeSymbol.offsetInBits DIV 8)
  1457. ELSE
  1458. RETURN (* ? *)
  1459. END;
  1460. (*! mem.fixup := scopeSymbol.adr; *)
  1461. NextSymbol;
  1462. END GetMemFixup;
  1463. PROCEDURE GetOffsetFixup (CONST ident: ARRAY OF CHAR; VAR operand: Operand);
  1464. VAR scopeSymbol: SyntaxTree.Symbol;name: Basic.SegmentedName; symbol: IntermediateCode.Section;
  1465. BEGIN
  1466. IF labels.Find(ident) # NIL THEN RETURN END;
  1467. scopeSymbol := GetScopeSymbol (ident);
  1468. IF (scopeSymbol = NIL) OR (scopeSymbol IS SyntaxTree.Constant) THEN RETURN END;
  1469. IF inlined & exported THEN
  1470. Error("no symbols may be accessed in exported and inlined procedures");
  1471. END;
  1472. Global.GetSymbolSegmentedName(scopeSymbol,name);
  1473. IF scopeSymbol.scope IS SyntaxTree.ModuleScope THEN
  1474. IF (scopeSymbol IS SyntaxTree.Variable) THEN
  1475. InitMem(operand,IntermediateCode.Bits32,none,0); (* or immediate ?? *)
  1476. ELSIF (scopeSymbol IS SyntaxTree.Procedure) & (scopeSymbol.scope = module.module.moduleScope) THEN
  1477. IF scopeSymbol(SyntaxTree.Procedure).isInline THEN
  1478. Error("fobidden reference to inline call");
  1479. ELSE
  1480. InitOffset32(operand,0); (* or immediate ?? *)
  1481. END;
  1482. ELSIF (scopeSymbol IS SyntaxTree.Procedure) THEN
  1483. InitOffset32(operand,0); (* or immediate ?? *)
  1484. END;
  1485. SetSymbol(operand,name,0,0,0);
  1486. ELSE
  1487. Error("direct access to local variable offset forbidden");
  1488. END;
  1489. operand.sizeInBytes := emitter.cpuBits;
  1490. END GetOffsetFixup;
  1491. (* the following procedure is used to adapt sizes for relative jumps *)
  1492. PROCEDURE AdaptOperandSizes(VAR operands: ARRAY OF Operand);
  1493. VAR i: LONGINT;
  1494. PROCEDURE OffsetSize(val: HUGEINT): SHORTINT;
  1495. BEGIN
  1496. DEC(val,emitter.code.pc);
  1497. IF (val > MIN(SHORTINT)+2) & (val < MAX(SHORTINT)) THEN
  1498. RETURN bits8
  1499. (* We do not support word (16-bit) displacement jumps
  1500. (i.e. prefixing the jump instruction with the `addr16' opcode prefix),
  1501. since the 80386 insists upon masking `%eip' to 16 bits after the word
  1502. displacement is added. *)
  1503. ELSIF (val > MIN(LONGINT)+2) & (val < MAX(LONGINT)-2) THEN
  1504. RETURN bits32
  1505. ELSE
  1506. RETURN bits64
  1507. END;
  1508. END OffsetSize;
  1509. BEGIN
  1510. i := 0;
  1511. WHILE (i< LEN(operands)) & (operands[i].type # none) DO
  1512. IF (operands[i].type = ioffset) & (operands[i].sizeInBytes = bitsDefault)
  1513. THEN
  1514. IF operands[i].symbol.name = "" THEN
  1515. operands[i].sizeInBytes := OffsetSize(operands[i].val);
  1516. ELSE
  1517. operands[i].sizeInBytes := bits32
  1518. END;
  1519. END;
  1520. INC(i)
  1521. END;
  1522. END AdaptOperandSizes;
  1523. PROCEDURE GetInstruction (): BOOLEAN;
  1524. VAR
  1525. mnem, opCount: LONGINT;
  1526. size: Size;
  1527. operands: ARRAY InstructionSet.maxNumberOperands OF Operand;
  1528. prevFixup: BinaryCode.Fixup;
  1529. mem: Operand;
  1530. offset: Operand;
  1531. i: LONGINT;
  1532. type: SHORTINT;
  1533. BEGIN
  1534. mnem := InstructionSet.FindMnemonic (ident);
  1535. IF mnem = InstructionSet.none THEN
  1536. ErrorSS("unkown instruction",idents);
  1537. RETURN FALSE;
  1538. END;
  1539. opCount := 0;
  1540. NextSymbol;
  1541. FOR i := 0 TO LEN(operands)-1 DO
  1542. InitOperand(operands[i]);
  1543. END;
  1544. WHILE (symbol # symLn) & (symbol # symNone) & (symbol # symEnd) DO
  1545. IF symbol = symIdent THEN
  1546. IF (ident = "BYTE") OR (ident = "SHORT") THEN
  1547. size := bits8; NextSymbol;
  1548. ELSIF (ident = "WORD") OR (ident = "NEAR") THEN
  1549. size := bits16; NextSymbol;
  1550. ELSIF ident = "DWORD" THEN
  1551. size := bits32; NextSymbol;
  1552. ELSIF ident = "QWORD" THEN
  1553. size := bits64; NextSymbol;
  1554. ELSIF ident = "TWORD" THEN
  1555. size := bits128; NextSymbol;
  1556. ELSE
  1557. size := bitsDefault;
  1558. END;
  1559. ELSE
  1560. size := bitsDefault;
  1561. END;
  1562. IF symbol = symIdent THEN (* register ?, for example EAX *)
  1563. reg := InstructionSet.FindRegister (ident);
  1564. IF reg = InstructionSet.none THEN
  1565. reg := map.Find(ident)
  1566. END;
  1567. IF reg # InstructionSet.none THEN
  1568. IF size # bitsDefault THEN
  1569. Error ("invalid register size specification"); RETURN FALSE;
  1570. END;
  1571. InitRegister(operands[opCount], reg);
  1572. INC (opCount);
  1573. NextSymbol;
  1574. END;
  1575. ELSE
  1576. reg := InstructionSet.none;
  1577. END;
  1578. IF reg = InstructionSet.none THEN
  1579. IF symbol = symLBraket THEN
  1580. (* mem, written as [....] *)
  1581. NextSymbol;
  1582. InitMem(mem, size, InstructionSet.none,0); (*! ??? *)
  1583. IF symbol = symLabel THEN (* register segment as in [ES:...] *)
  1584. reg := InstructionSet.FindRegister (ident);
  1585. IF reg = InstructionSet.none THEN
  1586. ErrorSS("undefined symbol",idents);
  1587. RETURN FALSE;
  1588. END;
  1589. mem.segment := reg;
  1590. NextSymbol;
  1591. END;
  1592. IF symbol = symIdent THEN (* register, for example [EAX] or [ES:EAX] *)
  1593. reg := InstructionSet.FindRegister (ident);
  1594. IF reg # InstructionSet.none THEN
  1595. mem.register := reg;
  1596. NextSymbol;
  1597. IF symbol = symTimes THEN (* register multiply as in [EAX*4] *)
  1598. NextSymbol;
  1599. IF ~Factor (mem.scale, FALSE,type) THEN
  1600. RETURN FALSE;
  1601. END;
  1602. mem.index := mem.register;
  1603. mem.register := InstructionSet.none;
  1604. END;
  1605. IF symbol = symPlus THEN (* register add as in [EAX + EBX] *)
  1606. NextSymbol;
  1607. IF symbol = symIdent THEN
  1608. reg := InstructionSet.FindRegister (ident);
  1609. IF reg # InstructionSet.none THEN (* maybe it is this: [EAX + EBX * 4] *)
  1610. NextSymbol;
  1611. IF mem.index = InstructionSet.none THEN
  1612. mem.index := reg;
  1613. IF symbol = symTimes THEN
  1614. NextSymbol;
  1615. IF ~Factor (mem.scale, FALSE,type) THEN
  1616. RETURN FALSE;
  1617. END;
  1618. END;
  1619. ELSE
  1620. mem.register := reg;
  1621. END;
  1622. END;
  1623. END;
  1624. END;
  1625. END;
  1626. END;
  1627. IF symbol = symPlus THEN
  1628. NextSymbol;
  1629. END;
  1630. IF (scope # NIL) & (symbol = symIdent) THEN
  1631. GetMemFixup (idents, mem);
  1632. END;
  1633. IF (symbol # symRBraket) & (symbol # symNegate) THEN
  1634. val2 := 0;
  1635. IF ~Expression (val2, FALSE ,type) THEN
  1636. RETURN FALSE;
  1637. END;
  1638. INC (mem.displacement, val2);
  1639. ELSIF (mem.register = InstructionSet.none) & (mem.index = InstructionSet.none) THEN
  1640. Error("operand missing: no register provided");
  1641. RETURN FALSE;
  1642. END;
  1643. operands[opCount] := mem;
  1644. INC (opCount);
  1645. IF ~Ensure (symRBraket, 556) THEN
  1646. RETURN FALSE;
  1647. END;
  1648. ELSE
  1649. (* number or identifier (symbol) *)
  1650. InitImm(offset,size,0);
  1651. IF (scope # NIL) & (symbol = symIdent) THEN (* identifier: must be a symbol *)
  1652. GetOffsetFixup (idents, offset);
  1653. END;
  1654. IF offset.symbol.name = "" THEN (* nothing could be fixuped, must be a number / constant *)
  1655. type := offset.type; currentFixup := ""; currentLabel := NIL;
  1656. IF ~Expression (val2, FALSE,type) THEN
  1657. RETURN FALSE;
  1658. ELSE
  1659. offset.type := type;
  1660. IF currentFixup # "" THEN
  1661. offset.symbol.name := currentFixup; offset.symbolOffset := val2;
  1662. ELSIF currentLabel # NIL THEN
  1663. IF (offset.sizeInBytes = bitsDefault ) & (val2 > emitter.code.pc) THEN (* forward jump *)
  1664. offset.sizeInBytes := bits32
  1665. END;
  1666. (*
  1667. IF offset.sizeInBytes = bitsDefault THEN
  1668. offset.sizeInBytes := bits32;
  1669. END;
  1670. *)
  1671. END;
  1672. END;
  1673. offset.val := val2;
  1674. IF symbol = symColon THEN (* additional prefixed operand separated by ":", segmentation register *)
  1675. NextSymbol;
  1676. IF ~Expression (val3, FALSE, type) THEN
  1677. RETURN FALSE;
  1678. END;
  1679. InitOffset(operands[opCount],bitsDefault,val3);
  1680. INC (opCount);
  1681. END;
  1682. ELSE
  1683. NextSymbol;
  1684. END;
  1685. operands[opCount] := offset;
  1686. INC (opCount);
  1687. END;
  1688. END;
  1689. IF symbol = symComma THEN
  1690. NextSymbol;
  1691. ELSIF (symbol # symLn) & (symbol # symEnd) THEN
  1692. Error("operand missing");
  1693. END
  1694. END;
  1695. prevFixup := fixup;
  1696. AdaptOperandSizes(operands);
  1697. IF ~emitter.EmitInstruction (mnem, operands, pass = maxPasses) THEN
  1698. RETURN FALSE;
  1699. END;
  1700. IF fixup = prevFixup THEN
  1701. Duplicate ((emitter.code.pc - prevPC) , NIL);
  1702. ELSE
  1703. Duplicate ((emitter.code.pc - prevPC) , fixup);
  1704. END;
  1705. RETURN TRUE;
  1706. END GetInstruction;
  1707. PROCEDURE Reset;
  1708. BEGIN
  1709. position := orgPos;
  1710. reader.SetPos(orgReaderPos);
  1711. emitter.code.SetPC(orgCodePos);
  1712. NextChar;
  1713. END Reset;
  1714. PROCEDURE FindLabels;
  1715. VAR firstInLine : BOOLEAN; label: NamedLabel;
  1716. BEGIN
  1717. IF Trace THEN KernelLog.String("find labels"); KernelLog.Ln; END;
  1718. LOOP
  1719. NextSymbol;
  1720. IF symbol = symLn THEN
  1721. firstInLine := TRUE;
  1722. ELSIF symbol = symLabel THEN
  1723. IF firstInLine THEN
  1724. IF labels.Find(idents) # NIL THEN
  1725. Error("multiply declared identifier")
  1726. ELSE
  1727. NEW(label,MAX(LONGINT),idents);
  1728. labels.Add(label);
  1729. IF Trace THEN KernelLog.String("found label"); KernelLog.String(idents); KernelLog.Ln; END;
  1730. END
  1731. END;
  1732. ELSIF symbol = symEnd THEN
  1733. EXIT
  1734. ELSE
  1735. firstInLine := FALSE;
  1736. END;
  1737. END;
  1738. END FindLabels;
  1739. PROCEDURE FixupLabels;
  1740. VAR label: NamedLabel;
  1741. BEGIN
  1742. IF Trace THEN KernelLog.String("patch fixups "); KernelLog.Ln; END;
  1743. fixup := emitter.code.fixupList.firstFixup;
  1744. WHILE fixup # NIL DO
  1745. IF (fixup.symbol.name = in.name) & (fixup.symbolOffset < 0) THEN
  1746. label := labels.first;
  1747. WHILE (label # NIL) & (label.index # -fixup.symbolOffset) DO label := label.nextNamedLabel END;
  1748. (*
  1749. fixup.SetSymbolOffset(label.offset);
  1750. *)
  1751. fixup.SetSymbol(out.name,0,0,label.offset+fixup.displacement);
  1752. IF Trace THEN
  1753. KernelLog.String("patch fixup: ");
  1754. KernelLog.Hex(fixup.offset,1); KernelLog.String(" "); KernelLog.Hex(-fixup.displacement, 1);
  1755. KernelLog.String(" "); KernelLog.Hex(label.offset, 1); KernelLog.Ln;
  1756. END;
  1757. END;
  1758. fixup := fixup.nextFixup;
  1759. END;
  1760. END FixupLabels;
  1761. BEGIN
  1762. prevSourceName := sourceName;
  1763. prevCpuBits := emitter.cpuBits;
  1764. prevCpuOptions := emitter.cpuOptions;
  1765. IF scope # NIL THEN
  1766. sourceName := scope.ownerModule.sourceName;
  1767. END;
  1768. NEW(labels);
  1769. orgReaderPos := reader.Pos();
  1770. orgCodePos := emitter.code.pc;
  1771. NextChar;
  1772. (* first we have to find all labels as their names might collide with symbol names *)
  1773. FindLabels;
  1774. FOR pass := 1 TO maxPasses DO (*! currently maxPasses = 1 *)
  1775. Reset;
  1776. times := 1;
  1777. prevPC := emitter.code.pc;
  1778. currentLabel := NIL;
  1779. absoluteMode := FALSE;
  1780. orgOffset := 0;
  1781. NextSymbol;
  1782. IF (scope # NIL) THEN
  1783. IF symbol # symLBrace THEN
  1784. (* treat CPU options as an optional limitation and not vice versa *)
  1785. ELSE
  1786. emitter.cpuOptions := {};
  1787. NextSymbol;
  1788. (* parse code flags such as {SYSTEM.i386 .... } *)
  1789. LOOP
  1790. IF ~Ensure (symIdent, 551) THEN
  1791. RETURN
  1792. END;
  1793. IF ident # "SYSTEM" THEN
  1794. Error("unsupported target identifier");
  1795. RETURN
  1796. END;
  1797. IF symbol # symPeriod THEN
  1798. Error("identifier expected");
  1799. RETURN;
  1800. END;
  1801. IF ~GetCPU (TRUE) THEN
  1802. RETURN;
  1803. END;
  1804. IF symbol = symRBrace THEN
  1805. EXIT
  1806. ELSIF symbol = symComma THEN
  1807. NextSymbol
  1808. ELSE
  1809. Error("target specifier expected");
  1810. RETURN;
  1811. END;
  1812. END;
  1813. NextSymbol;
  1814. END
  1815. END;
  1816. LOOP
  1817. IF symbol = symLn THEN
  1818. NextSymbol;
  1819. ELSIF symbol = symLabel THEN
  1820. currentLabel := labels.Find(idents);
  1821. ASSERT(currentLabel # NIL);
  1822. IF absoluteMode THEN
  1823. currentLabel.SetOffset(absoluteOffset);
  1824. ELSE
  1825. currentLabel.SetOffset(emitter.code.pc)
  1826. END;
  1827. NextSymbol;
  1828. ELSIF symbol = symIdent THEN
  1829. IF ident = "END" THEN
  1830. symbol := symNone;
  1831. ELSIF ident = "BITS" THEN
  1832. NextSymbol;
  1833. IF ~Ensure (symNumber, 553) OR ~emitter.SetBits (val) THEN
  1834. SkipLine;
  1835. ELSE
  1836. NextSymbol;
  1837. END;
  1838. ELSIF ~(scope # NIL) & (ident = "CPU") THEN
  1839. IF ~GetCPU (FALSE) THEN
  1840. SkipLine;
  1841. END;
  1842. ELSIF ~(scope # NIL) & (ident = "ABSOLUTE") THEN
  1843. absoluteMode := TRUE;
  1844. NextSymbol;
  1845. IF ~Expression (absoluteOffset, TRUE,type) THEN
  1846. SkipLine;
  1847. END;
  1848. ELSIF ~(scope # NIL) & (ident = "ORG") THEN
  1849. NextSymbol;
  1850. IF (orgOffset # 0) OR ~Expression (orgOffset, TRUE,type) THEN
  1851. SkipLine;
  1852. END;
  1853. ELSIF ~(scope # NIL) & (ident = "RESB") THEN
  1854. NextSymbol;
  1855. IF ~Reserve (1) THEN SkipLine END;
  1856. ELSIF ~(scope # NIL) & (ident = "RESW") THEN
  1857. NextSymbol;
  1858. IF ~Reserve (2) THEN SkipLine END;
  1859. ELSIF ~(scope # NIL) & (ident = "RESD") THEN
  1860. NextSymbol;
  1861. IF ~Reserve (4) THEN SkipLine END;
  1862. (*
  1863. ELSIF ident = "EQU" THEN
  1864. IF currentLabel # NIL THEN
  1865. NextSymbol;
  1866. IF Expression (val2, FALSE) THEN
  1867. currentLabel.pc := val2;
  1868. currentLabel.equ := TRUE;
  1869. ELSE
  1870. SkipLine;
  1871. END;
  1872. ELSE
  1873. Error("???");
  1874. RETURN;
  1875. END;
  1876. *)
  1877. ELSIF ident = "TIMES" THEN
  1878. NextSymbol;
  1879. IF ~Expression (times, TRUE,type) THEN
  1880. SkipLine;
  1881. ELSIF times < 0 THEN
  1882. Error("unsupported negative value"); RETURN;
  1883. ELSE
  1884. prevPC := emitter.code.pc;
  1885. END;
  1886. ELSIF ident = "DB" THEN
  1887. IF ~PutData (bits8) THEN SkipLine END;
  1888. ELSIF ident = "DW" THEN
  1889. IF ~PutData (bits16) THEN SkipLine END;
  1890. ELSIF ident = "DD" THEN
  1891. IF ~PutData (bits32) THEN SkipLine END;
  1892. ELSIF ident = "REP" THEN
  1893. NextSymbol;
  1894. emitter.code.PutByte (InstructionSet.prfREP);
  1895. ELSIF ident = "LOCK" THEN
  1896. NextSymbol;
  1897. emitter.code.PutByte (InstructionSet.prfLOCK);
  1898. ELSIF ident = "REPE" THEN
  1899. NextSymbol;
  1900. emitter.code.PutByte (InstructionSet.prfREPE);
  1901. ELSIF ident = "REPZ" THEN
  1902. NextSymbol;
  1903. emitter.code.PutByte (InstructionSet.prfREPZ);
  1904. ELSIF ident = "REPNE" THEN
  1905. NextSymbol;
  1906. emitter.code.PutByte (InstructionSet.prfREPNE);
  1907. ELSIF ident = "REPNZ" THEN
  1908. NextSymbol;
  1909. emitter.code.PutByte (InstructionSet.prfREPNZ);
  1910. ELSIF ~GetInstruction () THEN
  1911. SkipLine
  1912. END;
  1913. currentLabel := NIL;
  1914. ELSIF (symbol = symNone) OR (symbol = symEnd) THEN
  1915. EXIT
  1916. ELSE
  1917. Error("identifier expected");
  1918. RETURN;
  1919. END;
  1920. END;
  1921. END;
  1922. (*
  1923. FixupLabels();
  1924. *)
  1925. (*! FixupLabels(labels.first,code) *)
  1926. sourceName := prevSourceName;
  1927. emitter.cpuBits := prevCpuBits;
  1928. emitter.cpuOptions := prevCpuOptions;
  1929. END Assemble;
  1930. END Assembly;
  1931. VAR kernelWriter: Streams.Writer;
  1932. PROCEDURE Ord (ch: CHAR): INTEGER;
  1933. BEGIN RETURN ORD (ch) - ORD ("0")
  1934. END Ord;
  1935. PROCEDURE HexOrd (ch: CHAR): INTEGER;
  1936. BEGIN
  1937. IF ch <= "9" THEN RETURN ORD (ch) - ORD ("0")
  1938. ELSE RETURN ORD (CAP (ch)) - ORD ("A") + 10
  1939. END
  1940. END HexOrd;
  1941. PROCEDURE IsRegisterOperand*(CONST op: Operand): BOOLEAN;
  1942. BEGIN
  1943. RETURN op.type IN {reg8, reg16, reg32, reg64, CRn, DRn, segReg, sti, mmx, xmm}
  1944. END IsRegisterOperand;
  1945. PROCEDURE IsMemoryOperand*(CONST op: Operand): BOOLEAN;
  1946. BEGIN RETURN op.type = mem
  1947. END IsMemoryOperand;
  1948. PROCEDURE IsImmediateOperand*(CONST op: Operand): BOOLEAN;
  1949. BEGIN RETURN op.type = imm
  1950. END IsImmediateOperand;
  1951. PROCEDURE DumpType*(w: Streams.Writer; type: LONGINT);
  1952. BEGIN
  1953. CASE type OF
  1954. reg8: w.String("reg8")
  1955. |reg16: w.String("reg16");
  1956. |reg32: w.String("reg32");
  1957. |reg64: w.String("reg64");
  1958. |CRn: w.String("CRn");
  1959. |DRn: w.String("DRn");
  1960. |segReg: w.String("segReg");
  1961. |mmx: w.String("mmx");
  1962. |xmm: w.String("xmm");
  1963. |mem: w.String("mem");
  1964. |sti: w.String("sti");
  1965. |imm: w.String("imm");
  1966. |ioffset: w.String("ioffset");
  1967. |pntr1616: w.String("pntr1616");
  1968. |pntr1632: w.String("pntr1632");
  1969. ELSE
  1970. w.String("?"); w.Int(type,1); w.String("?");
  1971. END;
  1972. END DumpType;
  1973. PROCEDURE DumpOperand*(w: Streams.Writer; CONST operand: Operand);
  1974. BEGIN
  1975. CASE operand.type OF
  1976. |reg8, reg16, reg32, reg64, CRn, DRn, segReg, sti, mmx, xmm:
  1977. w.String(InstructionSet.registers[operand.register].name);
  1978. |mem:
  1979. IF operand.sizeInBytes = 1 THEN w.String("BYTE ")
  1980. ELSIF operand.sizeInBytes= 2 THEN w.String("WORD ")
  1981. ELSIF operand.sizeInBytes = 4 THEN w.String("DWORD ")
  1982. ELSIF operand.sizeInBytes = 8 THEN w.String("QWORD ")
  1983. END;
  1984. w.String("[");
  1985. IF operand.register # none THEN
  1986. w.String(InstructionSet.registers[operand.register].name);
  1987. IF operand.index # none THEN w.String("+") END;
  1988. END;
  1989. IF operand.index # none THEN
  1990. w.String(InstructionSet.registers[operand.index].name);
  1991. IF operand.scale # 1 THEN
  1992. w.String("*"); w.Int(operand.scale,1);
  1993. END;
  1994. END;
  1995. IF operand.symbol.name # "" THEN
  1996. Basic.WriteSegmentedName(w, operand.symbol.name); w.String(":"); w.Int(operand.displacement,1);
  1997. IF operand.symbolOffset # 0 THEN w.String("(@"); w.Int(operand.symbolOffset,1); w.String(")") END;
  1998. ELSIF operand.displacement # 0 THEN
  1999. IF (operand.displacement > 0) & ((operand.register # none) OR (operand.index # none)) THEN w.String("+");END;
  2000. w.Int(operand.displacement,1);
  2001. END;
  2002. w.String("]");
  2003. |imm,ioffset:
  2004. IF operand.symbol.name # "" THEN
  2005. Basic.WriteSegmentedName(w, operand.symbol.name); w.String(":"); w.Int(operand.displacement,1);
  2006. IF operand.symbolOffset # 0 THEN w.String("(@"); w.Int(operand.symbolOffset,1); w.String(")") END;
  2007. ELSE
  2008. IF (operand.val > MAX(LONGINT)) OR (operand.val < MIN(LONGINT)) THEN
  2009. w.Hex(operand.val,1); w.String("H");
  2010. ELSE
  2011. w.Int(SHORT(operand.val),1);
  2012. END;
  2013. END;
  2014. |pntr1616:
  2015. |pntr1632:
  2016. ELSE
  2017. HALT(100)
  2018. END;
  2019. END DumpOperand;
  2020. PROCEDURE DumpInstruction(w: Streams.Writer; mnemonic: LONGINT; CONST operands: ARRAY OF Operand);
  2021. VAR i: LONGINT;
  2022. CONST DebugSize = FALSE;
  2023. BEGIN
  2024. IF mnemonic # none THEN
  2025. w.String(InstructionSet.mnemonics[mnemonic].name);
  2026. i := 0;
  2027. WHILE(i<maxNumberOperands) & (operands[i].type # none) DO
  2028. IF i = 0 THEN w.Char(09X) ELSE w.String(", ") END;
  2029. DumpOperand(w,operands[i]);
  2030. IF DebugSize THEN
  2031. w.String("(*"); DumpType(w,operands[i].type); w.String(":"); w.Int(operands[i].sizeInBytes,1); w.String("*)");
  2032. END;
  2033. INC(i);
  2034. END;
  2035. w.String("; ");
  2036. END;
  2037. END DumpInstruction;
  2038. PROCEDURE Matches(CONST operand: Operand; type: InstructionSet.OperandType): BOOLEAN;
  2039. PROCEDURE IsMemReg(regIndex: LONGINT): BOOLEAN;
  2040. BEGIN
  2041. RETURN InstructionSet.RegisterType(regIndex) IN {reg16, reg32, reg64}
  2042. END IsMemReg;
  2043. BEGIN
  2044. CASE operand.type OF
  2045. |reg8:
  2046. CASE type OF
  2047. InstructionSet.reg8, InstructionSet.regmem8:
  2048. RETURN TRUE;
  2049. | InstructionSet.AL, InstructionSet.rAX:
  2050. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2051. | InstructionSet.CL:
  2052. RETURN InstructionSet.RegisterIndex(operand.register) = RCX;
  2053. ELSE
  2054. RETURN FALSE;
  2055. END;
  2056. |reg16:
  2057. CASE type OF
  2058. InstructionSet.reg16, InstructionSet.regmem16:
  2059. RETURN TRUE;
  2060. | InstructionSet.AX, InstructionSet.rAX:
  2061. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2062. | InstructionSet.DX:
  2063. RETURN InstructionSet.RegisterIndex(operand.register) = RDX;
  2064. ELSE
  2065. RETURN FALSE;
  2066. END;
  2067. |reg32:
  2068. CASE type OF
  2069. InstructionSet.reg32, InstructionSet.regmem32:
  2070. RETURN TRUE;
  2071. | InstructionSet.EAX, InstructionSet.rAX:
  2072. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2073. ELSE
  2074. RETURN FALSE;
  2075. END;
  2076. |reg64:
  2077. CASE type OF
  2078. InstructionSet.reg64, InstructionSet.regmem64:
  2079. RETURN TRUE;
  2080. | InstructionSet.RAX, InstructionSet.rAX:
  2081. RETURN InstructionSet.RegisterIndex(operand.register) = RAX;
  2082. ELSE
  2083. RETURN FALSE;
  2084. END;
  2085. |CRn:
  2086. CASE type OF
  2087. InstructionSet.CRn:
  2088. RETURN TRUE;
  2089. | InstructionSet.CR8:
  2090. RETURN InstructionSet.RegisterIndex(operand.register) = 8;
  2091. ELSE
  2092. RETURN FALSE;
  2093. END;
  2094. |DRn:
  2095. RETURN type = InstructionSet.DRn;
  2096. |segReg:
  2097. CASE type OF
  2098. InstructionSet.segReg:
  2099. RETURN TRUE;
  2100. | InstructionSet.ES:
  2101. RETURN InstructionSet.RegisterIndex(operand.register) = segES;
  2102. | InstructionSet.CS:
  2103. RETURN InstructionSet.RegisterIndex(operand.register) = segCS;
  2104. | InstructionSet.SS:
  2105. RETURN InstructionSet.RegisterIndex(operand.register) = segSS;
  2106. | InstructionSet.DS:
  2107. RETURN InstructionSet.RegisterIndex(operand.register) = segDS;
  2108. | InstructionSet.FS:
  2109. RETURN InstructionSet.RegisterIndex(operand.register) = segFS;
  2110. | InstructionSet.GS:
  2111. RETURN InstructionSet.RegisterIndex(operand.register) = segGS;
  2112. ELSE
  2113. RETURN FALSE;
  2114. END
  2115. |sti:
  2116. CASE type OF
  2117. InstructionSet.sti:
  2118. RETURN TRUE;
  2119. | InstructionSet.st0:
  2120. RETURN InstructionSet.RegisterIndex(operand.register) = 0;
  2121. ELSE
  2122. RETURN FALSE;
  2123. END
  2124. |mmx:
  2125. CASE type OF
  2126. InstructionSet.mmx, InstructionSet.mmxmem32, InstructionSet.mmxmem64:
  2127. RETURN TRUE;
  2128. ELSE
  2129. RETURN FALSE;
  2130. END
  2131. |xmm:
  2132. CASE type OF
  2133. InstructionSet.xmm, InstructionSet.xmmmem32, InstructionSet.xmmmem64, InstructionSet.xmmmem128:
  2134. RETURN TRUE;
  2135. ELSE
  2136. RETURN FALSE;
  2137. END
  2138. |mem:
  2139. CASE type OF
  2140. | InstructionSet.mem:
  2141. RETURN TRUE;
  2142. | InstructionSet.mem8:
  2143. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8);
  2144. | InstructionSet.regmem8:
  2145. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2146. | InstructionSet.mem16:
  2147. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16);
  2148. | InstructionSet.regmem16:
  2149. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2150. | InstructionSet.mem32:
  2151. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32);
  2152. | InstructionSet.regmem32, InstructionSet.mmxmem32, InstructionSet.xmmmem32:
  2153. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2154. | InstructionSet.mem64:
  2155. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64);
  2156. | InstructionSet.regmem64, InstructionSet.mmxmem64, InstructionSet.xmmmem64:
  2157. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2158. | InstructionSet.mem128:
  2159. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits128);
  2160. | InstructionSet.xmmmem128:
  2161. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits128)) & ((operand.register= none) OR (IsMemReg(operand.register)));
  2162. | InstructionSet.moffset8:
  2163. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.register= none);
  2164. | InstructionSet.moffset16:
  2165. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.register= none);
  2166. | InstructionSet.moffset32:
  2167. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) & (operand.register= none);
  2168. | InstructionSet.moffset64:
  2169. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64)) & (operand.register= none);
  2170. ELSE
  2171. RETURN FALSE;
  2172. END;
  2173. |imm,ioffset:
  2174. CASE type OF
  2175. InstructionSet.one:
  2176. RETURN operand.val = 1
  2177. | InstructionSet.three:
  2178. RETURN operand.val = 3
  2179. | InstructionSet.rel8off:
  2180. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)
  2181. | InstructionSet.imm8:
  2182. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.val >= -80H) & (operand.val < 100H)
  2183. | InstructionSet.simm8:
  2184. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.val >= -80H) & (operand.val < 80H)
  2185. | InstructionSet.uimm8:
  2186. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits8)) & (operand.val >= 0H) & (operand.val < 100H)
  2187. | InstructionSet.rel16off:
  2188. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16) & FALSE (* do not allow 16 bit jumps *)
  2189. | InstructionSet.imm16:
  2190. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.val >= -8000H) & (operand.val < 10000H)
  2191. | InstructionSet.simm16:
  2192. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.val >= -8000H) & (operand.val < 8000H)
  2193. | InstructionSet.uimm16:
  2194. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits16)) & (operand.val >= 0H) & (operand.val < 10000H)
  2195. | InstructionSet.rel32off:
  2196. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32) (* & & (operand.val >= -80000000H) & (operand.val < 100000000H) PACO confused? *)
  2197. | InstructionSet.imm32:
  2198. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) (* & & (operand.val >= -80000000H) & (operand.val < 100000000H) PACO confused? *)
  2199. | InstructionSet.simm32:
  2200. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) (* & & (operand.val >= -80000000H) & (operand.val < 80000000H) PACO confused? *)
  2201. | InstructionSet.uimm32:
  2202. RETURN ((operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits32)) & (operand.val >= 0H) (* & (operand.val < 100000000H) PACO confused? *)
  2203. | InstructionSet.imm64:
  2204. RETURN (operand.sizeInBytes = bitsDefault) OR (operand.sizeInBytes = bits64)
  2205. ELSE
  2206. RETURN FALSE
  2207. END
  2208. |pntr1616:
  2209. RETURN type = InstructionSet.pntr1616;
  2210. |pntr1632:
  2211. RETURN type = InstructionSet.pntr1632;
  2212. ELSE
  2213. HALT(100)
  2214. END;
  2215. END Matches;
  2216. PROCEDURE ValueInByteRange (value: HUGEINT): BOOLEAN;
  2217. BEGIN RETURN SYSTEM.VAL (SHORTINT, value) = value
  2218. END ValueInByteRange;
  2219. PROCEDURE ValueInWordRange (value: HUGEINT): BOOLEAN;
  2220. BEGIN RETURN SYSTEM.VAL (INTEGER, value) = value
  2221. END ValueInWordRange;
  2222. PROCEDURE InitOperand*(VAR operand: Operand);
  2223. BEGIN
  2224. operand.type := none;
  2225. operand.index := none;
  2226. operand.register:= none;
  2227. operand.segment:= none;
  2228. operand.sizeInBytes := none;
  2229. operand.scale := 1;
  2230. operand.displacement := 0;
  2231. operand.val := 0;
  2232. operand.pc := none;
  2233. operand.symbol.name := "";
  2234. operand.symbol.fingerprint := 0;
  2235. operand.selector := none;
  2236. operand.offset := 0;
  2237. END InitOperand;
  2238. PROCEDURE InitRegister* (VAR operand: Operand; register: Register);
  2239. BEGIN
  2240. InitOperand(operand);
  2241. operand.type := InstructionSet.RegisterType(register);
  2242. operand.register :=register;
  2243. CASE operand.type OF
  2244. reg8,reg16,reg32,reg64,segReg,CRn,DRn,sti,xmm,mmx: (* ok *)
  2245. |InstructionSet.st0: operand.type := InstructionSet.sti;
  2246. ELSE
  2247. HALT(100);
  2248. END;
  2249. operand.sizeInBytes := InstructionSet.registers[register].sizeInBytes
  2250. END InitRegister;
  2251. PROCEDURE NewRegister*(register: Register): Operand;
  2252. VAR operand: Operand;
  2253. BEGIN InitRegister(operand,register); RETURN operand
  2254. END NewRegister;
  2255. PROCEDURE InitMem*(VAR operand: Operand; size: Size; reg: Register; displacement: LONGINT);
  2256. BEGIN
  2257. InitOperand(operand);
  2258. operand.type := mem;
  2259. operand.sizeInBytes := size;
  2260. operand.register:= reg;
  2261. operand.displacement := displacement;
  2262. operand.scale := 1;
  2263. END InitMem;
  2264. PROCEDURE SetIndexScale*(VAR operand: Operand; index: Register; scale: LONGINT);
  2265. BEGIN
  2266. operand.index := index;
  2267. operand.scale := scale
  2268. END SetIndexScale;
  2269. PROCEDURE NewMem*(size: Size; reg: Register; displacement: LONGINT): Operand;
  2270. VAR operand: Operand;
  2271. BEGIN
  2272. InitMem(operand,size,reg,displacement); RETURN operand
  2273. END NewMem;
  2274. PROCEDURE InitMem8* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2275. BEGIN InitMem (operand, bits8, reg, displacement);
  2276. END InitMem8;
  2277. PROCEDURE NewMem8* (reg: Register; displacement: LONGINT): Operand;
  2278. VAR operand: Operand;
  2279. BEGIN InitMem8 (operand,reg, displacement); RETURN operand
  2280. END NewMem8;
  2281. PROCEDURE InitMem16* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2282. BEGIN InitMem (operand,bits16, reg, displacement);
  2283. END InitMem16;
  2284. PROCEDURE NewMem16* (reg: Register; displacement: LONGINT): Operand;
  2285. VAR operand: Operand;
  2286. BEGIN InitMem16 (operand,reg, displacement); RETURN operand
  2287. END NewMem16;
  2288. PROCEDURE InitMem32* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2289. BEGIN InitMem (operand,bits32, reg, displacement);
  2290. END InitMem32;
  2291. PROCEDURE NewMem32* (reg: Register; displacement: LONGINT): Operand;
  2292. VAR operand: Operand;
  2293. BEGIN InitMem32 (operand,reg, displacement); RETURN operand
  2294. END NewMem32;
  2295. PROCEDURE InitMem64* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2296. BEGIN InitMem (operand,bits64, reg, displacement);
  2297. END InitMem64;
  2298. PROCEDURE NewMem64* (reg: Register; displacement: LONGINT): Operand;
  2299. VAR operand: Operand;
  2300. BEGIN InitMem64 (operand,reg, displacement); RETURN operand
  2301. END NewMem64;
  2302. PROCEDURE InitMem128* (VAR operand: Operand; reg: Register; displacement: LONGINT);
  2303. BEGIN InitMem (operand,bits128, reg, displacement);
  2304. END InitMem128;
  2305. PROCEDURE NewMem128* (reg: Register; displacement: LONGINT): Operand;
  2306. VAR operand: Operand;
  2307. BEGIN InitMem128 (operand,reg, displacement); RETURN operand
  2308. END NewMem128;
  2309. PROCEDURE SetSymbol*(VAR operand: Operand; symbol: Sections.SectionName; fingerprint: LONGINT; symbolOffset, displacement: LONGINT);
  2310. BEGIN
  2311. operand.symbol.name := symbol;
  2312. operand.symbol.fingerprint := fingerprint;
  2313. operand.symbolOffset := symbolOffset; operand.displacement := displacement;
  2314. END SetSymbol;
  2315. PROCEDURE InitImm* (VAR operand: Operand; size: SHORTINT; val: HUGEINT);
  2316. BEGIN InitOperand(operand); operand.type := imm; operand.sizeInBytes := size; operand.val := val;
  2317. END InitImm;
  2318. PROCEDURE InitImm8* (VAR operand: Operand; val: HUGEINT);
  2319. BEGIN InitImm (operand, bits8, val);
  2320. END InitImm8;
  2321. PROCEDURE NewImm8*(val: HUGEINT): Operand;
  2322. VAR operand: Operand;
  2323. BEGIN InitImm8(operand,val); RETURN operand
  2324. END NewImm8;
  2325. PROCEDURE InitImm16* (VAR operand: Operand; val: HUGEINT);
  2326. BEGIN InitImm (operand, bits16, val);
  2327. END InitImm16;
  2328. PROCEDURE NewImm16*(val: HUGEINT): Operand;
  2329. VAR operand:Operand;
  2330. BEGIN InitImm16(operand,val); RETURN operand
  2331. END NewImm16;
  2332. PROCEDURE InitImm32* (VAR operand: Operand; val: HUGEINT);
  2333. BEGIN InitImm (operand, bits32, val);
  2334. END InitImm32;
  2335. PROCEDURE NewImm32*(val: HUGEINT): Operand;
  2336. VAR operand: Operand;
  2337. BEGIN InitImm32(operand,val); RETURN operand
  2338. END NewImm32;
  2339. PROCEDURE InitImm64* (VAR operand: Operand; val: HUGEINT);
  2340. BEGIN InitImm (operand, bits64, val);
  2341. END InitImm64;
  2342. PROCEDURE NewImm64*(val: HUGEINT): Operand;
  2343. VAR operand: Operand;
  2344. BEGIN InitImm64(operand,val); RETURN operand
  2345. END NewImm64;
  2346. PROCEDURE InitOffset* (VAR operand: Operand; size: SHORTINT; val: HUGEINT);
  2347. BEGIN InitOperand(operand); operand.type := ioffset; operand.sizeInBytes := size; operand.val := val;
  2348. END InitOffset;
  2349. PROCEDURE InitOffset8* (VAR operand: Operand; val: HUGEINT);
  2350. BEGIN InitOffset (operand, bits8, val);
  2351. END InitOffset8;
  2352. PROCEDURE NewOffset8*(val: HUGEINT): Operand;
  2353. VAR operand: Operand;
  2354. BEGIN InitOffset8(operand,val); RETURN operand
  2355. END NewOffset8;
  2356. PROCEDURE InitOffset16* (VAR operand: Operand; val: HUGEINT);
  2357. BEGIN InitOffset (operand, bits16, val);
  2358. END InitOffset16;
  2359. PROCEDURE NewOffset16*(val: HUGEINT): Operand;
  2360. VAR operand: Operand;
  2361. BEGIN InitOffset16(operand,val); RETURN operand
  2362. END NewOffset16;
  2363. PROCEDURE InitOffset32* (VAR operand: Operand; val: HUGEINT);
  2364. BEGIN InitOffset (operand, bits32, val);
  2365. END InitOffset32;
  2366. PROCEDURE NewOffset32*(val: HUGEINT): Operand;
  2367. VAR operand: Operand;
  2368. BEGIN InitOffset32(operand,val); RETURN operand
  2369. END NewOffset32;
  2370. PROCEDURE InitOffset64* (VAR operand: Operand; val: HUGEINT);
  2371. BEGIN InitOffset (operand, bits64, val);
  2372. END InitOffset64;
  2373. PROCEDURE NewOffset64*(val: HUGEINT): Operand;
  2374. VAR operand: Operand;
  2375. BEGIN InitOffset64(operand,val); RETURN operand
  2376. END NewOffset64;
  2377. PROCEDURE InitPntr1616* (VAR operand: Operand; s, o: LONGINT);
  2378. BEGIN InitOperand(operand); operand.type := pntr1616; operand.selector := s; operand.offset := o;
  2379. END InitPntr1616;
  2380. PROCEDURE InitPntr1632* (VAR operand: Operand; s, o: LONGINT);
  2381. BEGIN InitOperand(operand); operand.type := pntr1632; operand.selector := s; operand.offset := o;
  2382. END InitPntr1632;
  2383. PROCEDURE SetSize*(VAR operand: Operand;sizeInBytes: Size);
  2384. BEGIN operand.sizeInBytes := sizeInBytes
  2385. END SetSize;
  2386. PROCEDURE SameOperand*(CONST left,right: Operand): BOOLEAN;
  2387. BEGIN
  2388. IF (left.type # right.type) OR (left.sizeInBytes # right.sizeInBytes) OR (left.symbol # right.symbol) THEN RETURN FALSE END;
  2389. CASE left.type OF
  2390. reg8,reg16,reg32,reg64,segReg,CRn,DRn,sti,xmm,mmx: RETURN left.register = right.register
  2391. | imm,ioffset: RETURN (left.val = right.val) & ((left.symbol.name="") OR (left.displacement = right.displacement))
  2392. | mem:RETURN (left.register = right.register) & (left.displacement = right.displacement) & (left.index = right.index) & (left.scale = right.scale)
  2393. | pntr1616,pntr1632: RETURN (left.selector=right.selector) & (left.offset=right.offset)
  2394. END;
  2395. RETURN FALSE
  2396. END SameOperand;
  2397. PROCEDURE Test*(context: Commands.Context);
  2398. VAR assembly: Emitter;
  2399. (*errorHandler: ErrorHandler; *)
  2400. op1,op2,op3: Operand;
  2401. diagnostics: Diagnostics.StreamDiagnostics;
  2402. code: Code;
  2403. pooledName: Basic.SegmentedName;
  2404. PROCEDURE Op(CONST name: ARRAY OF CHAR): LONGINT;
  2405. BEGIN
  2406. RETURN InstructionSet.FindMnemonic(name)
  2407. END Op;
  2408. BEGIN
  2409. InitOperand(op1); InitOperand(op2); InitOperand(op3);
  2410. NEW(diagnostics,context.error);
  2411. Basic.ToSegmentedName("test", pooledName);
  2412. NEW(code,Sections.CodeSection,8,0,pooledName,TRUE,TRUE);
  2413. NEW(assembly,diagnostics);
  2414. assembly.SetCode(code);
  2415. InitRegister(op1,InstructionSet.regEAX);
  2416. InitImm32(op2,10);
  2417. assembly.Emit2(Op("MOV"),op1,op2);
  2418. context.out.Update;
  2419. code.Dump(context.out);
  2420. END Test;
  2421. BEGIN
  2422. IF Trace THEN
  2423. NEW(kernelWriter,KernelLog.Send,1000);
  2424. END;
  2425. END FoxAMD64Assembler.
  2426. OCAMD64Assembler.Test ~