2
0

FoxAMD64Assembler.Mod 88 KB

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