FoxAMD64Assembler.Mod 88 KB

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