rax86int.pas 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297
  1. {
  2. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  3. Does the parsing process for the intel styled inline assembler.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. ****************************************************************************
  16. }
  17. Unit Rax86int;
  18. {$i fpcdefs.inc}
  19. interface
  20. uses
  21. cclasses,
  22. cpubase,
  23. globtype,
  24. aasmbase,
  25. rasm,
  26. rax86;
  27. type
  28. tasmtoken = (
  29. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  30. AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  31. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  32. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,
  33. {------------------ Assembler directives --------------------}
  34. AS_ALIGN,AS_DB,AS_DW,AS_DD,AS_DQ,AS_END,
  35. {------------------ Assembler Operators --------------------}
  36. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_DQWORD,AS_NEAR,AS_FAR,
  37. AS_HIGH,AS_LOW,AS_OFFSET,AS_SIZEOF,AS_VMTOFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  38. AS_AND,AS_OR,AS_XOR,AS_WRT,AS___GOTPCREL);
  39. type
  40. tx86intreader = class(tasmreader)
  41. actasmtoken : tasmtoken;
  42. prevasmtoken : tasmtoken;
  43. ActOpsize : topsize;
  44. constructor create;override;
  45. function is_asmopcode(const s: string):boolean;
  46. function is_asmoperator(const s: string):boolean;
  47. function is_asmdirective(const s: string):boolean;
  48. function is_register(const s:string):boolean;
  49. function is_locallabel(const s:string):boolean;
  50. function Assemble: tlinkedlist;override;
  51. procedure GetToken;
  52. function consume(t : tasmtoken):boolean;
  53. procedure RecoverConsume(allowcomma:boolean);
  54. procedure BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint; var mangledname: string; needvmtofs: boolean);
  55. procedure BuildConstSymbolExpression(needofs,isref,startingminus:boolean;var value:aint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  56. function BuildConstExpression:aint;
  57. function BuildRefConstExpression:aint;
  58. procedure BuildReference(oper : tx86operand);
  59. procedure BuildOperand(oper: tx86operand;istypecast:boolean);
  60. procedure BuildConstantOperand(oper: tx86operand);
  61. procedure BuildOpCode(instr : tx86instruction);
  62. procedure BuildConstant(constsize: byte);
  63. end;
  64. implementation
  65. uses
  66. { common }
  67. cutils,
  68. { global }
  69. globals,verbose,
  70. systems,
  71. { aasm }
  72. aasmtai,aasmdata,aasmcpu,
  73. { symtable }
  74. symconst,symbase,symtype,symsym,symdef,symtable,
  75. { parser }
  76. scanner,
  77. { register allocator }
  78. rabase,rautils,itx86int,
  79. { codegen }
  80. cgbase,cgobj,procinfo
  81. ;
  82. type
  83. tasmkeyword = string[9];
  84. const
  85. { These tokens should be modified accordingly to the modifications }
  86. { in the different enumerations. }
  87. firstdirective = AS_ALIGN;
  88. lastdirective = AS_END;
  89. firstoperator = AS_BYTE;
  90. lastoperator = AS___GOTPCREL;
  91. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  92. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  93. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  94. ('ALIGN','DB','DW','DD','DQ','END');
  95. { problems with shl,shr,not,and,or and xor, they are }
  96. { context sensitive. }
  97. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  98. 'BYTE','WORD','DWORD','QWORD','TBYTE','DQWORD','NEAR','FAR','HIGH',
  99. 'LOW','OFFSET','SIZEOF','VMTOFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  100. 'OR','XOR','WRT','GOTPCREL');
  101. token2str : array[tasmtoken] of string[10] = (
  102. '','Label','LLabel','String','Integer',
  103. ',','[',']','(',
  104. ')',':','.','+','-','*',
  105. ';','identifier','register','opcode','/',
  106. '','','','','','END',
  107. '','','','','','','','','',
  108. '','','sizeof','vmtoffset','','type','ptr','mod','shl','shr','not',
  109. 'and','or','xor','wrt','..gotpcrel'
  110. );
  111. var
  112. inexpression : boolean;
  113. constructor tx86intreader.create;
  114. var
  115. i : tasmop;
  116. Begin
  117. inherited create;
  118. iasmops:=TFPHashList.create;
  119. for i:=firstop to lastop do
  120. iasmops.Add(upper(std_op2str[i]),Pointer(PtrInt(i)));
  121. end;
  122. {---------------------------------------------------------------------}
  123. { Routines for the tokenizing }
  124. {---------------------------------------------------------------------}
  125. function tx86intreader.is_asmopcode(const s: string):boolean;
  126. var
  127. cond : string[4];
  128. cnd : tasmcond;
  129. j: longint;
  130. Begin
  131. is_asmopcode:=FALSE;
  132. actopcode:=A_None;
  133. actcondition:=C_None;
  134. actopsize:=S_NO;
  135. { Search opcodes }
  136. actopcode:=tasmop(PtrUInt(iasmops.Find(s)));
  137. if actopcode<>A_NONE then
  138. begin
  139. actasmtoken:=AS_OPCODE;
  140. result:=TRUE;
  141. exit;
  142. end;
  143. { not found yet, check condition opcodes }
  144. j:=0;
  145. while (j<CondAsmOps) do
  146. begin
  147. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  148. begin
  149. cond:=Copy(s,Length(CondAsmOpStr[j])+1,255);
  150. if cond<>'' then
  151. begin
  152. for cnd:=low(TasmCond) to high(TasmCond) do
  153. if Cond=Upper(cond2str[cnd]) then
  154. begin
  155. actopcode:=CondASmOp[j];
  156. actcondition:=cnd;
  157. is_asmopcode:=TRUE;
  158. actasmtoken:=AS_OPCODE;
  159. exit
  160. end;
  161. end;
  162. end;
  163. inc(j);
  164. end;
  165. end;
  166. function tx86intreader.is_asmoperator(const s: string):boolean;
  167. var
  168. i : longint;
  169. Begin
  170. for i:=0 to _count_asmoperators do
  171. if s=_asmoperators[i] then
  172. begin
  173. actasmtoken:=tasmtoken(longint(firstoperator)+i);
  174. is_asmoperator:=true;
  175. exit;
  176. end;
  177. is_asmoperator:=false;
  178. end;
  179. Function tx86intreader.is_asmdirective(const s: string):boolean;
  180. var
  181. i : longint;
  182. Begin
  183. for i:=0 to _count_asmdirectives do
  184. if s=_asmdirectives[i] then
  185. begin
  186. actasmtoken:=tasmtoken(longint(firstdirective)+i);
  187. is_asmdirective:=true;
  188. exit;
  189. end;
  190. is_asmdirective:=false;
  191. end;
  192. function tx86intreader.is_register(const s:string):boolean;
  193. var
  194. entry: TSymEntry;
  195. begin
  196. is_register:=false;
  197. actasmregister:=masm_regnum_search(lower(s));
  198. if (actasmregister=NR_NO) and
  199. (current_procinfo.procdef.proccalloption=pocall_register) and
  200. (po_assembler in current_procinfo.procdef.procoptions) then
  201. begin
  202. entry:=current_procinfo.procdef.parast.Find(s);
  203. if assigned(entry) and
  204. (entry.typ=paravarsym) and
  205. assigned(tparavarsym(entry).paraloc[calleeside].Location) and
  206. (tparavarsym(entry).paraloc[calleeside].Location^.Loc=LOC_REGISTER) then
  207. actasmregister:=tparavarsym(entry).paraloc[calleeside].Location^.register;
  208. end;
  209. if actasmregister<>NR_NO then
  210. begin
  211. is_register:=true;
  212. actasmtoken:=AS_REGISTER;
  213. end;
  214. end;
  215. function tx86intreader.is_locallabel(const s:string):boolean;
  216. begin
  217. is_locallabel:=(length(s)>1) and (s[1]='@');
  218. end;
  219. Procedure tx86intreader.GetToken;
  220. var
  221. len : longint;
  222. forcelabel : boolean;
  223. srsym : tsym;
  224. srsymtable : TSymtable;
  225. begin
  226. { save old token and reset new token }
  227. prevasmtoken:=actasmtoken;
  228. actasmtoken:=AS_NONE;
  229. { reset }
  230. forcelabel:=FALSE;
  231. actasmpattern:='';
  232. { while space and tab , continue scan... }
  233. while (c in [' ',#9]) do
  234. c:=current_scanner.asmgetchar;
  235. { get token pos }
  236. if not (c in [#10,#13,'{',';']) then
  237. current_scanner.gettokenpos;
  238. { Local Label, Label, Directive, Prefix or Opcode }
  239. if firsttoken and not (c in [#10,#13,'{',';']) then
  240. begin
  241. firsttoken:=FALSE;
  242. len:=0;
  243. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  244. begin
  245. { if there is an at_sign, then this must absolutely be a label }
  246. if c = '@' then
  247. forcelabel:=TRUE;
  248. inc(len);
  249. actasmpattern[len]:=c;
  250. c:=current_scanner.asmgetchar;
  251. end;
  252. actasmpattern[0]:=chr(len);
  253. uppervar(actasmpattern);
  254. { allow spaces }
  255. while (c in [' ',#9]) do
  256. c:=current_scanner.asmgetchar;
  257. { label ? }
  258. if c = ':' then
  259. begin
  260. if actasmpattern[1]='@' then
  261. actasmtoken:=AS_LLABEL
  262. else
  263. actasmtoken:=AS_LABEL;
  264. { let us point to the next character }
  265. c:=current_scanner.asmgetchar;
  266. firsttoken:=true;
  267. exit;
  268. end;
  269. { Are we trying to create an identifier with }
  270. { an at-sign...? }
  271. if forcelabel then
  272. Message(asmr_e_none_label_contain_at);
  273. { opcode ? }
  274. If is_asmopcode(actasmpattern) then
  275. Begin
  276. { check if we are in an expression }
  277. { then continue with asm directives }
  278. if not inexpression then
  279. exit;
  280. end;
  281. if is_asmdirective(actasmpattern) then
  282. exit;
  283. message1(asmr_e_unknown_opcode,actasmpattern);
  284. actasmtoken:=AS_NONE;
  285. exit;
  286. end
  287. else { else firsttoken }
  288. begin
  289. case c of
  290. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  291. { - @Result, @Code or @Data special variables. }
  292. begin
  293. actasmpattern:=c;
  294. c:=current_scanner.asmgetchar;
  295. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  296. begin
  297. actasmpattern:=actasmpattern + c;
  298. c:=current_scanner.asmgetchar;
  299. end;
  300. uppervar(actasmpattern);
  301. actasmtoken:=AS_ID;
  302. exit;
  303. end;
  304. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  305. begin
  306. actasmpattern:=c;
  307. c:=current_scanner.asmgetchar;
  308. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  309. begin
  310. actasmpattern:=actasmpattern + c;
  311. c:=current_scanner.asmgetchar;
  312. end;
  313. uppervar(actasmpattern);
  314. { after prefix we allow also a new opcode }
  315. If is_prefix(actopcode) and is_asmopcode(actasmpattern) then
  316. Begin
  317. { if we are not in a constant }
  318. { expression than this is an }
  319. { opcode. }
  320. if not inexpression then
  321. exit;
  322. end;
  323. { support st(X) for fpu registers }
  324. if (actasmpattern = 'ST') and (c='(') then
  325. Begin
  326. actasmpattern:=actasmpattern+c;
  327. c:=current_scanner.asmgetchar;
  328. { allow spaces }
  329. while (c in [' ',#9]) do
  330. c:=current_scanner.asmgetchar;
  331. if c in ['0'..'7'] then
  332. actasmpattern:=actasmpattern + c
  333. else
  334. Message(asmr_e_invalid_fpu_register);
  335. c:=current_scanner.asmgetchar;
  336. { allow spaces }
  337. while (c in [' ',#9]) do
  338. c:=current_scanner.asmgetchar;
  339. if c <> ')' then
  340. Message(asmr_e_invalid_fpu_register)
  341. else
  342. Begin
  343. actasmpattern:=actasmpattern + c;
  344. c:=current_scanner.asmgetchar;
  345. end;
  346. end;
  347. if is_asmdirective(actasmpattern) then
  348. exit;
  349. if is_asmoperator(actasmpattern) then
  350. exit;
  351. if is_register(actasmpattern) then
  352. exit;
  353. { allow spaces }
  354. while (c in [' ',#9]) do
  355. c:=current_scanner.asmgetchar;
  356. { if next is a '.' and this is a unitsym then we also need to
  357. parse the identifier }
  358. if (c='.') then
  359. begin
  360. asmsearchsym(actasmpattern,srsym,srsymtable);
  361. if assigned(srsym) and
  362. (srsym.typ=unitsym) and
  363. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  364. srsym.owner.iscurrentunit then
  365. begin
  366. { Add . to create System.Identifier }
  367. actasmpattern:=actasmpattern+c;
  368. c:=current_scanner.asmgetchar;
  369. { Delphi allows System.@Halt, just ignore the @ }
  370. if c='@' then
  371. c:=current_scanner.asmgetchar;
  372. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  373. begin
  374. actasmpattern:=actasmpattern + upcase(c);
  375. c:=current_scanner.asmgetchar;
  376. end;
  377. end;
  378. end;
  379. actasmtoken:=AS_ID;
  380. exit;
  381. end;
  382. '''' : { string or character }
  383. begin
  384. actasmpattern:='';
  385. current_scanner.in_asm_string:=true;
  386. repeat
  387. if c = '''' then
  388. begin
  389. c:=current_scanner.asmgetchar;
  390. if c in [#10,#13] then
  391. begin
  392. Message(scan_f_string_exceeds_line);
  393. break;
  394. end;
  395. repeat
  396. if c='''' then
  397. begin
  398. c:=current_scanner.asmgetchar;
  399. if c='''' then
  400. begin
  401. actasmpattern:=actasmpattern+'''';
  402. c:=current_scanner.asmgetchar;
  403. if c in [#10,#13] then
  404. begin
  405. Message(scan_f_string_exceeds_line);
  406. break;
  407. end;
  408. end
  409. else
  410. break;
  411. end
  412. else
  413. begin
  414. actasmpattern:=actasmpattern+c;
  415. c:=current_scanner.asmgetchar;
  416. if c in [#10,#13] then
  417. begin
  418. Message(scan_f_string_exceeds_line);
  419. break
  420. end;
  421. end;
  422. until false; { end repeat }
  423. end
  424. else
  425. break; { end if }
  426. until false;
  427. current_scanner.in_asm_string:=false;
  428. actasmtoken:=AS_STRING;
  429. exit;
  430. end;
  431. '"' : { string or character }
  432. begin
  433. current_scanner.in_asm_string:=true;
  434. actasmpattern:='';
  435. repeat
  436. if c = '"' then
  437. begin
  438. c:=current_scanner.asmgetchar;
  439. if c in [#10,#13] then
  440. begin
  441. Message(scan_f_string_exceeds_line);
  442. break;
  443. end;
  444. repeat
  445. if c='"' then
  446. begin
  447. c:=current_scanner.asmgetchar;
  448. if c='"' then
  449. begin
  450. actasmpattern:=actasmpattern+'"';
  451. c:=current_scanner.asmgetchar;
  452. if c in [#10,#13] then
  453. begin
  454. Message(scan_f_string_exceeds_line);
  455. break;
  456. end;
  457. end
  458. else
  459. break;
  460. end
  461. else
  462. begin
  463. actasmpattern:=actasmpattern+c;
  464. c:=current_scanner.asmgetchar;
  465. if c in [#10,#13] then
  466. begin
  467. Message(scan_f_string_exceeds_line);
  468. break
  469. end;
  470. end;
  471. until false; { end repeat }
  472. end
  473. else
  474. break; { end if }
  475. until false;
  476. current_scanner.in_asm_string:=false;
  477. actasmtoken:=AS_STRING;
  478. exit;
  479. end;
  480. '$' :
  481. begin
  482. c:=current_scanner.asmgetchar;
  483. while c in ['0'..'9','A'..'F','a'..'f'] do
  484. begin
  485. actasmpattern:=actasmpattern + c;
  486. c:=current_scanner.asmgetchar;
  487. end;
  488. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  489. actasmtoken:=AS_INTNUM;
  490. exit;
  491. end;
  492. '&' : { identifier }
  493. begin
  494. actasmpattern:='';
  495. c:=current_scanner.asmgetchar;
  496. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  497. begin
  498. actasmpattern:=actasmpattern + c;
  499. c:=current_scanner.asmgetchar;
  500. end;
  501. uppervar(actasmpattern);
  502. actasmtoken:=AS_ID;
  503. exit;
  504. end;
  505. ',' :
  506. begin
  507. actasmtoken:=AS_COMMA;
  508. c:=current_scanner.asmgetchar;
  509. exit;
  510. end;
  511. '[' :
  512. begin
  513. actasmtoken:=AS_LBRACKET;
  514. c:=current_scanner.asmgetchar;
  515. exit;
  516. end;
  517. ']' :
  518. begin
  519. actasmtoken:=AS_RBRACKET;
  520. c:=current_scanner.asmgetchar;
  521. exit;
  522. end;
  523. '(' :
  524. begin
  525. actasmtoken:=AS_LPAREN;
  526. c:=current_scanner.asmgetchar;
  527. exit;
  528. end;
  529. ')' :
  530. begin
  531. actasmtoken:=AS_RPAREN;
  532. c:=current_scanner.asmgetchar;
  533. exit;
  534. end;
  535. ':' :
  536. begin
  537. actasmtoken:=AS_COLON;
  538. c:=current_scanner.asmgetchar;
  539. exit;
  540. end;
  541. '.' :
  542. begin
  543. c:=current_scanner.asmgetchar;
  544. {$ifdef x86_64}
  545. if c='.' then
  546. begin
  547. actasmpattern:='..';
  548. c:=current_scanner.asmgetchar;
  549. repeat
  550. actasmpattern:=actasmpattern+c;
  551. c:=current_scanner.asmgetchar;
  552. until not(c in ['A'..'Z','a'..'z','0'..'9','_']);
  553. if upper(actasmpattern)<>'..GOTPCREL' then
  554. begin
  555. actasmtoken:=AS_ID;
  556. consume(AS___GOTPCREL);
  557. end;
  558. actasmtoken:=AS___GOTPCREL
  559. end
  560. else
  561. {$endif x86_64}
  562. actasmtoken:=AS_DOT;
  563. exit;
  564. end;
  565. '+' :
  566. begin
  567. actasmtoken:=AS_PLUS;
  568. c:=current_scanner.asmgetchar;
  569. exit;
  570. end;
  571. '-' :
  572. begin
  573. actasmtoken:=AS_MINUS;
  574. c:=current_scanner.asmgetchar;
  575. exit;
  576. end;
  577. '*' :
  578. begin
  579. actasmtoken:=AS_STAR;
  580. c:=current_scanner.asmgetchar;
  581. exit;
  582. end;
  583. '/' :
  584. begin
  585. actasmtoken:=AS_SLASH;
  586. c:=current_scanner.asmgetchar;
  587. exit;
  588. end;
  589. '0'..'9':
  590. begin
  591. actasmpattern:=c;
  592. c:=current_scanner.asmgetchar;
  593. { Get the possible characters }
  594. while c in ['0'..'9','A'..'F','a'..'f'] do
  595. begin
  596. actasmpattern:=actasmpattern + c;
  597. c:=current_scanner.asmgetchar;
  598. end;
  599. { Get ending character }
  600. uppervar(actasmpattern);
  601. c:=upcase(c);
  602. { possibly a binary number. }
  603. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  604. Begin
  605. { Delete the last binary specifier }
  606. delete(actasmpattern,length(actasmpattern),1);
  607. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  608. actasmtoken:=AS_INTNUM;
  609. exit;
  610. end
  611. else
  612. Begin
  613. case c of
  614. 'O' :
  615. Begin
  616. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  617. actasmtoken:=AS_INTNUM;
  618. c:=current_scanner.asmgetchar;
  619. exit;
  620. end;
  621. 'H' :
  622. Begin
  623. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  624. actasmtoken:=AS_INTNUM;
  625. c:=current_scanner.asmgetchar;
  626. exit;
  627. end;
  628. else { must be an integer number }
  629. begin
  630. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  631. actasmtoken:=AS_INTNUM;
  632. exit;
  633. end;
  634. end;
  635. end;
  636. end;
  637. ';','{',#13,#10 :
  638. begin
  639. c:=current_scanner.asmgetchar;
  640. firsttoken:=TRUE;
  641. actasmtoken:=AS_SEPARATOR;
  642. exit;
  643. end;
  644. else
  645. current_scanner.illegal_char(c);
  646. end;
  647. end;
  648. end;
  649. function tx86intreader.consume(t : tasmtoken):boolean;
  650. begin
  651. Consume:=true;
  652. if t<>actasmtoken then
  653. begin
  654. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  655. Consume:=false;
  656. end;
  657. repeat
  658. gettoken;
  659. until actasmtoken<>AS_NONE;
  660. end;
  661. procedure tx86intreader.RecoverConsume(allowcomma:boolean);
  662. begin
  663. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  664. begin
  665. if allowcomma and (actasmtoken=AS_COMMA) then
  666. break;
  667. Consume(actasmtoken);
  668. end;
  669. end;
  670. {*****************************************************************************
  671. Parsing Helpers
  672. *****************************************************************************}
  673. { This routine builds up a record offset after a AS_DOT
  674. token is encountered.
  675. On entry actasmtoken should be equal to AS_DOT }
  676. Procedure tx86intreader.BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint; var mangledname: string; needvmtofs: boolean);
  677. var
  678. s: string;
  679. Begin
  680. offset:=0;
  681. size:=0;
  682. s:=expr;
  683. while (actasmtoken=AS_DOT) do
  684. begin
  685. Consume(AS_DOT);
  686. if actasmtoken in [AS_BYTE,AS_ID,AS_WORD,AS_DWORD,AS_QWORD,AS_REGISTER] then
  687. begin
  688. s:=s+'.'+actasmpattern;
  689. consume(actasmtoken);
  690. end
  691. else
  692. begin
  693. Consume(AS_ID);
  694. RecoverConsume(true);
  695. break;
  696. end;
  697. end;
  698. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs) then
  699. Message(asmr_e_building_record_offset);
  700. end;
  701. Procedure tx86intreader.BuildConstSymbolExpression(needofs,isref,startingminus:boolean;var value:aint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  702. var
  703. tempstr,expr,hs,mangledname : string;
  704. parenlevel : longint;
  705. l,k : aint;
  706. hasparen,
  707. errorflag,
  708. needvmtofs : boolean;
  709. prevtok : tasmtoken;
  710. hl : tasmlabel;
  711. hssymtyp : Tasmsymtype;
  712. def : tdef;
  713. sym : tsym;
  714. srsymtable : TSymtable;
  715. Begin
  716. { reset }
  717. value:=0;
  718. asmsym:='';
  719. asmsymtyp:=AT_DATA;
  720. errorflag:=FALSE;
  721. tempstr:='';
  722. expr:='';
  723. if startingminus then
  724. expr:='-';
  725. inexpression:=TRUE;
  726. parenlevel:=0;
  727. sym:=nil;
  728. needvmtofs:=FALSE;
  729. Repeat
  730. { Support ugly delphi constructs like: [ECX].1+2[EDX] }
  731. if isref and (actasmtoken=AS_LBRACKET) then
  732. break;
  733. Case actasmtoken of
  734. AS_LPAREN:
  735. Begin
  736. Consume(AS_LPAREN);
  737. expr:=expr + '(';
  738. inc(parenlevel);
  739. end;
  740. AS_RPAREN:
  741. Begin
  742. { Keep the AS_PAREN in actasmtoken, it is maybe a typecast }
  743. if parenlevel=0 then
  744. break;
  745. Consume(AS_RPAREN);
  746. expr:=expr + ')';
  747. dec(parenlevel);
  748. end;
  749. AS_SHL:
  750. Begin
  751. Consume(AS_SHL);
  752. expr:=expr + '<';
  753. end;
  754. AS_SHR:
  755. Begin
  756. Consume(AS_SHR);
  757. expr:=expr + '>';
  758. end;
  759. AS_SLASH:
  760. Begin
  761. Consume(AS_SLASH);
  762. expr:=expr + '/';
  763. end;
  764. AS_MOD:
  765. Begin
  766. Consume(AS_MOD);
  767. expr:=expr + '%';
  768. end;
  769. AS_STAR:
  770. Begin
  771. Consume(AS_STAR);
  772. if isref and (actasmtoken=AS_REGISTER) then
  773. break;
  774. expr:=expr + '*';
  775. end;
  776. AS_PLUS:
  777. Begin
  778. Consume(AS_PLUS);
  779. if isref and (actasmtoken=AS_REGISTER) then
  780. break;
  781. expr:=expr + '+';
  782. end;
  783. AS_MINUS:
  784. Begin
  785. Consume(AS_MINUS);
  786. expr:=expr + '-';
  787. end;
  788. AS_AND:
  789. Begin
  790. Consume(AS_AND);
  791. expr:=expr + '&';
  792. end;
  793. AS_NOT:
  794. Begin
  795. Consume(AS_NOT);
  796. expr:=expr + '~';
  797. end;
  798. AS_XOR:
  799. Begin
  800. Consume(AS_XOR);
  801. expr:=expr + '^';
  802. end;
  803. AS_OR:
  804. Begin
  805. Consume(AS_OR);
  806. expr:=expr + '|';
  807. end;
  808. AS_INTNUM:
  809. Begin
  810. expr:=expr + actasmpattern;
  811. Consume(AS_INTNUM);
  812. end;
  813. AS_VMTOFFSET,
  814. AS_OFFSET:
  815. begin
  816. if (actasmtoken = AS_OFFSET) then
  817. needofs:=true
  818. else
  819. needvmtofs:=true;
  820. Consume(actasmtoken);
  821. if actasmtoken<>AS_ID then
  822. Message(asmr_e_offset_without_identifier);
  823. end;
  824. AS_SIZEOF,
  825. AS_TYPE:
  826. begin
  827. l:=0;
  828. hasparen:=false;
  829. Consume(actasmtoken);
  830. if actasmtoken=AS_LPAREN then
  831. begin
  832. hasparen:=true;
  833. Consume(AS_LPAREN);
  834. end;
  835. if actasmtoken<>AS_ID then
  836. Message(asmr_e_type_without_identifier)
  837. else
  838. begin
  839. tempstr:=actasmpattern;
  840. Consume(AS_ID);
  841. if actasmtoken=AS_DOT then
  842. begin
  843. BuildRecordOffsetSize(tempstr,k,l,mangledname,false);
  844. if mangledname<>'' then
  845. { procsym }
  846. Message(asmr_e_wrong_sym_type);
  847. end
  848. else
  849. begin
  850. asmsearchsym(tempstr,sym,srsymtable);
  851. if assigned(sym) then
  852. begin
  853. case sym.typ of
  854. staticvarsym,
  855. localvarsym,
  856. paravarsym :
  857. l:=tabstractvarsym(sym).getsize;
  858. typesym :
  859. l:=ttypesym(sym).typedef.size;
  860. else
  861. Message(asmr_e_wrong_sym_type);
  862. end;
  863. end
  864. else
  865. Message1(sym_e_unknown_id,tempstr);
  866. end;
  867. end;
  868. str(l, tempstr);
  869. expr:=expr + tempstr;
  870. if hasparen then
  871. Consume(AS_RPAREN);
  872. end;
  873. AS_PTR :
  874. begin
  875. { Support ugly delphi constructs like <constant> PTR [ref] }
  876. break;
  877. end;
  878. AS_STRING:
  879. begin
  880. l:=0;
  881. case Length(actasmpattern) of
  882. 1 :
  883. l:=ord(actasmpattern[1]);
  884. 2 :
  885. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  886. 3 :
  887. l:=ord(actasmpattern[3]) +
  888. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  889. 4 :
  890. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  891. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  892. else
  893. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  894. end;
  895. str(l, tempstr);
  896. expr:=expr + tempstr;
  897. Consume(AS_STRING);
  898. end;
  899. AS_ID:
  900. begin
  901. hs:='';
  902. hssymtyp:=AT_DATA;
  903. def:=nil;
  904. tempstr:=actasmpattern;
  905. prevtok:=prevasmtoken;
  906. { stop parsing a constant expression if we find an opcode after a
  907. non-operator like "db $66 mov eax,ebx" }
  908. if (prevtok in [AS_ID,AS_INTNUM,AS_RPAREN]) and
  909. is_asmopcode(actasmpattern) then
  910. break;
  911. consume(AS_ID);
  912. if SearchIConstant(tempstr,l) then
  913. begin
  914. str(l, tempstr);
  915. expr:=expr + tempstr;
  916. end
  917. else
  918. begin
  919. if is_locallabel(tempstr) then
  920. begin
  921. CreateLocalLabel(tempstr,hl,false);
  922. hs:=hl.name;
  923. hssymtyp:=AT_FUNCTION;
  924. end
  925. else
  926. if SearchLabel(tempstr,hl,false) then
  927. begin
  928. hs:=hl.name;
  929. hssymtyp:=AT_FUNCTION;
  930. end
  931. else
  932. begin
  933. asmsearchsym(tempstr,sym,srsymtable);
  934. if assigned(sym) then
  935. begin
  936. case sym.typ of
  937. staticvarsym :
  938. begin
  939. hs:=tstaticvarsym(sym).mangledname;
  940. def:=tstaticvarsym(sym).vardef;
  941. end;
  942. localvarsym,
  943. paravarsym :
  944. begin
  945. Message(asmr_e_no_local_or_para_allowed);
  946. end;
  947. procsym :
  948. begin
  949. if Tprocsym(sym).ProcdefList.Count>1 then
  950. Message(asmr_w_calling_overload_func);
  951. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  952. hssymtyp:=AT_FUNCTION;
  953. end;
  954. typesym :
  955. begin
  956. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  957. Message(asmr_e_wrong_sym_type);
  958. end;
  959. fieldvarsym :
  960. begin
  961. tempstr:=upper(tdef(sym.owner.defowner).GetTypeName)+'.'+tempstr;
  962. end;
  963. else
  964. Message(asmr_e_wrong_sym_type);
  965. end;
  966. end
  967. else
  968. Message1(sym_e_unknown_id,tempstr);
  969. end;
  970. { symbol found? }
  971. if hs<>'' then
  972. begin
  973. if asmsym='' then
  974. begin
  975. asmsym:=hs;
  976. asmsymtyp:=hssymtyp;
  977. end
  978. else
  979. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  980. if (expr='') or (expr[length(expr)]='+') then
  981. begin
  982. { don't remove the + if there could be a record field }
  983. if actasmtoken<>AS_DOT then
  984. delete(expr,length(expr),1);
  985. end
  986. else
  987. if needofs then
  988. begin
  989. if (prevtok<>AS_OFFSET) then
  990. Message(asmr_e_need_offset);
  991. end
  992. else
  993. Message(asmr_e_only_add_relocatable_symbol);
  994. end;
  995. if (actasmtoken=AS_DOT) or
  996. (assigned(sym) and
  997. (sym.typ = fieldvarsym)) then
  998. begin
  999. BuildRecordOffsetSize(tempstr,l,k,hs,needvmtofs);
  1000. if hs <> '' then
  1001. hssymtyp:=AT_FUNCTION
  1002. else
  1003. begin
  1004. str(l, tempstr);
  1005. expr:=expr + tempstr;
  1006. end
  1007. end
  1008. else
  1009. begin
  1010. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1011. delete(expr,length(expr),1);
  1012. end;
  1013. if (actasmtoken=AS_LBRACKET) and
  1014. assigned(def) and
  1015. (def.typ=arraydef) then
  1016. begin
  1017. consume(AS_LBRACKET);
  1018. l:=BuildConstExpression;
  1019. if l<tarraydef(def).lowrange then
  1020. begin
  1021. Message(asmr_e_constant_out_of_bounds);
  1022. l:=0;
  1023. end
  1024. else
  1025. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  1026. str(l, tempstr);
  1027. expr:=expr + '+' + tempstr;
  1028. consume(AS_RBRACKET);
  1029. end;
  1030. end;
  1031. { check if there are wrong operator used like / or mod etc. }
  1032. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  1033. Message(asmr_e_only_add_relocatable_symbol);
  1034. end;
  1035. AS_ALIGN,
  1036. AS_DB,
  1037. AS_DW,
  1038. AS_DD,
  1039. AS_DQ,
  1040. AS_END,
  1041. AS_RBRACKET,
  1042. AS_SEPARATOR,
  1043. AS_COMMA,
  1044. AS_COLON:
  1045. break;
  1046. else
  1047. begin
  1048. { write error only once. }
  1049. if not errorflag then
  1050. Message(asmr_e_invalid_constant_expression);
  1051. { consume tokens until we find COMMA or SEPARATOR }
  1052. Consume(actasmtoken);
  1053. errorflag:=TRUE;
  1054. end;
  1055. end;
  1056. Until false;
  1057. { calculate expression }
  1058. if not ErrorFlag then
  1059. value:=CalculateExpression(expr)
  1060. else
  1061. value:=0;
  1062. { no longer in an expression }
  1063. inexpression:=FALSE;
  1064. end;
  1065. Function tx86intreader.BuildConstExpression:aint;
  1066. var
  1067. l : aint;
  1068. hs : string;
  1069. hssymtyp : TAsmsymtype;
  1070. begin
  1071. BuildConstSymbolExpression(false,false,false,l,hs,hssymtyp);
  1072. if hs<>'' then
  1073. Message(asmr_e_relocatable_symbol_not_allowed);
  1074. BuildConstExpression:=l;
  1075. end;
  1076. Function tx86intreader.BuildRefConstExpression:aint;
  1077. var
  1078. l : aint;
  1079. hs : string;
  1080. hssymtyp : TAsmsymtype;
  1081. begin
  1082. BuildConstSymbolExpression(false,true,false,l,hs,hssymtyp);
  1083. if hs<>'' then
  1084. Message(asmr_e_relocatable_symbol_not_allowed);
  1085. BuildRefConstExpression:=l;
  1086. end;
  1087. procedure tx86intreader.BuildReference(oper : tx86operand);
  1088. var
  1089. scale : byte;
  1090. k,l : aint;
  1091. tempstr,hs : string;
  1092. tempsymtyp : tasmsymtype;
  1093. code : integer;
  1094. hreg : tregister;
  1095. GotStar,GotOffset,HadVar,
  1096. GotPlus,Negative : boolean;
  1097. hl : tasmlabel;
  1098. Begin
  1099. Consume(AS_LBRACKET);
  1100. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  1101. oper.InitRef;
  1102. GotStar:=false;
  1103. GotPlus:=true;
  1104. GotOffset:=false;
  1105. Negative:=false;
  1106. Scale:=0;
  1107. repeat
  1108. if GotOffset and (actasmtoken<>AS_ID) then
  1109. Message(asmr_e_invalid_reference_syntax);
  1110. Case actasmtoken of
  1111. AS_ID, { Constant reference expression OR variable reference expression }
  1112. AS_VMTOFFSET:
  1113. Begin
  1114. if not GotPlus then
  1115. Message(asmr_e_invalid_reference_syntax);
  1116. GotStar:=false;
  1117. GotPlus:=false;
  1118. if (actasmtoken = AS_VMTOFFSET) or
  1119. (SearchIConstant(actasmpattern,l) or
  1120. SearchRecordType(actasmpattern)) then
  1121. begin
  1122. l:=BuildRefConstExpression;
  1123. GotPlus:=(prevasmtoken=AS_PLUS);
  1124. GotStar:=(prevasmtoken=AS_STAR);
  1125. case oper.opr.typ of
  1126. OPR_LOCAL :
  1127. begin
  1128. if GotStar then
  1129. Message(asmr_e_invalid_reference_syntax);
  1130. if negative then
  1131. Dec(oper.opr.localsymofs,l)
  1132. else
  1133. Inc(oper.opr.localsymofs,l);
  1134. end;
  1135. OPR_REFERENCE :
  1136. begin
  1137. if GotStar then
  1138. oper.opr.ref.scalefactor:=l
  1139. else
  1140. begin
  1141. if negative then
  1142. Dec(oper.opr.ref.offset,l)
  1143. else
  1144. Inc(oper.opr.ref.offset,l);
  1145. end;
  1146. end;
  1147. end;
  1148. end
  1149. else
  1150. Begin
  1151. if negative and not oper.hasvar then
  1152. Message(asmr_e_only_add_relocatable_symbol)
  1153. else if oper.hasvar and not GotOffset and
  1154. (not negative or assigned(oper.opr.ref.relsymbol)) then
  1155. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1156. HadVar:=oper.hasvar and GotOffset;
  1157. tempstr:=actasmpattern;
  1158. Consume(AS_ID);
  1159. { typecasting? }
  1160. if (actasmtoken=AS_LPAREN) and
  1161. SearchType(tempstr,l) then
  1162. begin
  1163. oper.hastype:=true;
  1164. oper.typesize:=l;
  1165. Consume(AS_LPAREN);
  1166. BuildOperand(oper,true);
  1167. Consume(AS_RPAREN);
  1168. end
  1169. else
  1170. if is_locallabel(tempstr) then
  1171. begin
  1172. CreateLocalLabel(tempstr,hl,false);
  1173. oper.InitRef;
  1174. if not negative then
  1175. begin
  1176. oper.opr.ref.symbol:=hl;
  1177. oper.hasvar:=true;
  1178. end
  1179. else
  1180. oper.opr.ref.relsymbol:=hl;
  1181. end
  1182. else
  1183. if oper.SetupVar(tempstr,GotOffset) then
  1184. begin
  1185. { force OPR_LOCAL to be a reference }
  1186. if oper.opr.typ=OPR_LOCAL then
  1187. oper.opr.localforceref:=true
  1188. else
  1189. begin
  1190. {$ifdef x86_64}
  1191. if actasmtoken=AS_WRT then
  1192. begin
  1193. if (oper.opr.typ=OPR_REFERENCE) then
  1194. begin
  1195. Consume(AS_WRT);
  1196. Consume(AS___GOTPCREL);
  1197. if (oper.opr.ref.base<>NR_NO) or
  1198. (oper.opr.ref.index<>NR_NO) or
  1199. (oper.opr.ref.offset<>0) then
  1200. Message(asmr_e_wrong_gotpcrel_intel_syntax);
  1201. if tf_no_pic_supported in target_info.flags then
  1202. Message(asmr_e_no_gotpcrel_support);
  1203. oper.opr.ref.refaddr:=addr_pic;
  1204. oper.opr.ref.base:=NR_RIP;
  1205. end
  1206. else
  1207. message(asmr_e_invalid_reference_syntax);
  1208. end;
  1209. {$endif x86_64}
  1210. end;
  1211. end
  1212. else
  1213. Message1(sym_e_unknown_id,tempstr);
  1214. { record.field ? }
  1215. if actasmtoken=AS_DOT then
  1216. begin
  1217. BuildRecordOffsetSize(tempstr,l,k,hs,false);
  1218. if (hs<>'') then
  1219. Message(asmr_e_invalid_symbol_ref);
  1220. case oper.opr.typ of
  1221. OPR_LOCAL :
  1222. inc(oper.opr.localsymofs,l);
  1223. OPR_REFERENCE :
  1224. inc(oper.opr.ref.offset,l);
  1225. end;
  1226. oper.SetSize(k,false);
  1227. end;
  1228. if GotOffset then
  1229. begin
  1230. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1231. begin
  1232. if (oper.opr.typ=OPR_REFERENCE) then
  1233. oper.opr.ref.base:=NR_NO;
  1234. oper.hasvar:=hadvar;
  1235. end
  1236. else
  1237. begin
  1238. if oper.hasvar and hadvar then
  1239. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1240. { should we allow ?? }
  1241. end;
  1242. end;
  1243. end;
  1244. GotOffset:=false;
  1245. end;
  1246. AS_PLUS :
  1247. Begin
  1248. Consume(AS_PLUS);
  1249. Negative:=false;
  1250. GotPlus:=true;
  1251. GotStar:=false;
  1252. Scale:=0;
  1253. end;
  1254. AS_DOT :
  1255. Begin
  1256. { Handle like a + }
  1257. Consume(AS_DOT);
  1258. Negative:=false;
  1259. GotPlus:=true;
  1260. GotStar:=false;
  1261. Scale:=0;
  1262. end;
  1263. AS_MINUS :
  1264. begin
  1265. Consume(AS_MINUS);
  1266. Negative:=true;
  1267. GotPlus:=true;
  1268. GotStar:=false;
  1269. Scale:=0;
  1270. end;
  1271. AS_STAR : { Scaling, with eax*4 order }
  1272. begin
  1273. Consume(AS_STAR);
  1274. hs:='';
  1275. l:=0;
  1276. case actasmtoken of
  1277. AS_LPAREN :
  1278. l:=BuildConstExpression;
  1279. AS_INTNUM:
  1280. Begin
  1281. hs:=actasmpattern;
  1282. Consume(AS_INTNUM);
  1283. end;
  1284. AS_REGISTER :
  1285. begin
  1286. case oper.opr.typ of
  1287. OPR_REFERENCE :
  1288. begin
  1289. if oper.opr.ref.scalefactor=0 then
  1290. begin
  1291. if scale<>0 then
  1292. begin
  1293. oper.opr.ref.scalefactor:=scale;
  1294. scale:=0;
  1295. end
  1296. else
  1297. Message(asmr_e_wrong_scale_factor);
  1298. end
  1299. else
  1300. Message(asmr_e_invalid_reference_syntax);
  1301. end;
  1302. OPR_LOCAL :
  1303. begin
  1304. if oper.opr.localscale=0 then
  1305. begin
  1306. if scale<>0 then
  1307. begin
  1308. oper.opr.localscale:=scale;
  1309. scale:=0;
  1310. end
  1311. else
  1312. Message(asmr_e_wrong_scale_factor);
  1313. end
  1314. else
  1315. Message(asmr_e_invalid_reference_syntax);
  1316. end;
  1317. end;
  1318. end;
  1319. else
  1320. Message(asmr_e_invalid_reference_syntax);
  1321. end;
  1322. if actasmtoken<>AS_REGISTER then
  1323. begin
  1324. if hs<>'' then
  1325. val(hs,l,code);
  1326. case oper.opr.typ of
  1327. OPR_REFERENCE :
  1328. oper.opr.ref.scalefactor:=l;
  1329. OPR_LOCAL :
  1330. oper.opr.localscale:=l;
  1331. end;
  1332. if l>9 then
  1333. Message(asmr_e_wrong_scale_factor);
  1334. end;
  1335. GotPlus:=false;
  1336. GotStar:=false;
  1337. end;
  1338. AS_REGISTER :
  1339. begin
  1340. if not((GotPlus and (not Negative)) or
  1341. GotStar) then
  1342. Message(asmr_e_invalid_reference_syntax);
  1343. hreg:=actasmregister;
  1344. Consume(AS_REGISTER);
  1345. { this register will be the index:
  1346. 1. just read a *
  1347. 2. next token is a *
  1348. 3. base register is already used }
  1349. case oper.opr.typ of
  1350. OPR_LOCAL :
  1351. begin
  1352. if (oper.opr.localindexreg<>NR_NO) then
  1353. Message(asmr_e_multiple_index);
  1354. oper.opr.localindexreg:=hreg;
  1355. if scale<>0 then
  1356. begin
  1357. oper.opr.localscale:=scale;
  1358. scale:=0;
  1359. end;
  1360. end;
  1361. OPR_REFERENCE :
  1362. begin
  1363. if (GotStar) or
  1364. (actasmtoken=AS_STAR) or
  1365. (oper.opr.ref.base<>NR_NO) then
  1366. begin
  1367. if (oper.opr.ref.index<>NR_NO) then
  1368. Message(asmr_e_multiple_index);
  1369. oper.opr.ref.index:=hreg;
  1370. if scale<>0 then
  1371. begin
  1372. oper.opr.ref.scalefactor:=scale;
  1373. scale:=0;
  1374. end;
  1375. end
  1376. else
  1377. begin
  1378. oper.opr.ref.base:=hreg;
  1379. {$ifdef x86_64}
  1380. { non-GOT based RIP-relative accesses are also position-independent }
  1381. if (oper.opr.ref.base=NR_RIP) and
  1382. (oper.opr.ref.refaddr<>addr_pic) then
  1383. oper.opr.ref.refaddr:=addr_pic_no_got;
  1384. {$endif x86_64}
  1385. end;
  1386. end;
  1387. end;
  1388. GotPlus:=false;
  1389. GotStar:=false;
  1390. end;
  1391. AS_OFFSET :
  1392. begin
  1393. Consume(AS_OFFSET);
  1394. GotOffset:=true;
  1395. end;
  1396. AS_TYPE,
  1397. AS_NOT,
  1398. AS_STRING,
  1399. AS_INTNUM,
  1400. AS_LPAREN : { Constant reference expression }
  1401. begin
  1402. if not GotPlus and not GotStar then
  1403. Message(asmr_e_invalid_reference_syntax);
  1404. BuildConstSymbolExpression(true,true,GotPlus and negative,l,tempstr,tempsymtyp);
  1405. { already handled by BuildConstSymbolExpression(); must be
  1406. handled there to avoid [reg-1+1] being interpreted as
  1407. [reg-(1+1)] }
  1408. negative:=false;
  1409. if tempstr<>'' then
  1410. begin
  1411. if GotStar then
  1412. Message(asmr_e_only_add_relocatable_symbol);
  1413. if not assigned(oper.opr.ref.symbol) then
  1414. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr)
  1415. else
  1416. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1417. end;
  1418. case oper.opr.typ of
  1419. OPR_REFERENCE :
  1420. begin
  1421. if GotStar then
  1422. oper.opr.ref.scalefactor:=l
  1423. else if (prevasmtoken = AS_STAR) then
  1424. begin
  1425. if scale<>0 then
  1426. scale:=l*scale
  1427. else
  1428. scale:=l;
  1429. end
  1430. else
  1431. Inc(oper.opr.ref.offset,l);
  1432. end;
  1433. OPR_LOCAL :
  1434. begin
  1435. if GotStar then
  1436. oper.opr.localscale:=l
  1437. else if (prevasmtoken = AS_STAR) then
  1438. begin
  1439. if scale<>0 then
  1440. scale:=l*scale
  1441. else
  1442. scale:=l;
  1443. end
  1444. else
  1445. Inc(oper.opr.localsymofs,l);
  1446. end;
  1447. end;
  1448. GotPlus:=(prevasmtoken=AS_PLUS) or
  1449. (prevasmtoken=AS_MINUS);
  1450. if GotPlus then
  1451. negative := prevasmtoken = AS_MINUS;
  1452. GotStar:=(prevasmtoken=AS_STAR);
  1453. end;
  1454. AS_RBRACKET :
  1455. begin
  1456. if GotPlus or GotStar then
  1457. Message(asmr_e_invalid_reference_syntax);
  1458. Consume(AS_RBRACKET);
  1459. break;
  1460. end;
  1461. else
  1462. Begin
  1463. Message(asmr_e_invalid_reference_syntax);
  1464. RecoverConsume(true);
  1465. break;
  1466. end;
  1467. end;
  1468. until false;
  1469. end;
  1470. Procedure tx86intreader.BuildConstantOperand(oper: tx86operand);
  1471. var
  1472. l : aint;
  1473. tempstr : string;
  1474. tempsymtyp : tasmsymtype;
  1475. begin
  1476. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1477. Message(asmr_e_invalid_operand_type);
  1478. BuildConstSymbolExpression(true,false,false,l,tempstr,tempsymtyp);
  1479. if tempstr<>'' then
  1480. begin
  1481. oper.opr.typ:=OPR_SYMBOL;
  1482. oper.opr.symofs:=l;
  1483. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr);
  1484. end
  1485. else
  1486. if oper.opr.typ=OPR_NONE then
  1487. begin
  1488. oper.opr.typ:=OPR_CONSTANT;
  1489. oper.opr.val:=l;
  1490. end
  1491. else
  1492. inc(oper.opr.val,l);
  1493. end;
  1494. Procedure tx86intreader.BuildOperand(oper: tx86operand;istypecast:boolean);
  1495. procedure AddLabelOperand(hl:tasmlabel);
  1496. begin
  1497. if (oper.opr.typ=OPR_NONE) and
  1498. is_calljmp(actopcode) then
  1499. begin
  1500. oper.opr.typ:=OPR_SYMBOL;
  1501. oper.opr.symbol:=hl;
  1502. end
  1503. else
  1504. begin
  1505. oper.InitRef;
  1506. oper.opr.ref.symbol:=hl;
  1507. end;
  1508. end;
  1509. var
  1510. expr,
  1511. hs : string;
  1512. tempreg : tregister;
  1513. l : aint;
  1514. hl : tasmlabel;
  1515. toffset,
  1516. tsize : aint;
  1517. begin
  1518. expr:='';
  1519. repeat
  1520. if actasmtoken=AS_DOT then
  1521. begin
  1522. if expr<>'' then
  1523. begin
  1524. BuildRecordOffsetSize(expr,toffset,tsize,hs,false);
  1525. if (oper.opr.typ<>OPR_NONE) and
  1526. (hs<>'') then
  1527. Message(asmr_e_wrong_sym_type);
  1528. oper.SetSize(tsize,true);
  1529. { we have used the size of a field. Reset the typesize of the record }
  1530. oper.typesize:=0;
  1531. case oper.opr.typ of
  1532. OPR_LOCAL :
  1533. begin
  1534. { don't allow direct access to fields of parameters, because that
  1535. will generate buggy code. Allow it only for explicit typecasting
  1536. and when the parameter is in a register (delphi compatible) }
  1537. if (not oper.hastype) and
  1538. (oper.opr.localsym.owner.symtabletype=parasymtable) and
  1539. (current_procinfo.procdef.proccalloption<>pocall_register) then
  1540. Message(asmr_e_cannot_access_field_directly_for_parameters);
  1541. inc(oper.opr.localsymofs,toffset)
  1542. end;
  1543. OPR_CONSTANT :
  1544. inc(oper.opr.val,toffset);
  1545. OPR_REFERENCE :
  1546. inc(oper.opr.ref.offset,toffset);
  1547. OPR_NONE :
  1548. begin
  1549. if (hs <> '') then
  1550. begin
  1551. oper.opr.typ:=OPR_SYMBOL;
  1552. oper.opr.symbol:=current_asmdata.RefAsmSymbol(hs);
  1553. end
  1554. else
  1555. begin
  1556. oper.opr.typ:=OPR_CONSTANT;
  1557. oper.opr.val:=toffset;
  1558. end;
  1559. end;
  1560. OPR_REGISTER :
  1561. Message(asmr_e_invalid_reference_syntax);
  1562. OPR_SYMBOL:
  1563. Message(asmr_e_invalid_symbol_ref);
  1564. else
  1565. internalerror(200309222);
  1566. end;
  1567. expr:='';
  1568. end
  1569. else
  1570. begin
  1571. { See it as a separator }
  1572. Consume(AS_DOT);
  1573. end;
  1574. end;
  1575. case actasmtoken of
  1576. AS_OFFSET,
  1577. AS_SIZEOF,
  1578. AS_VMTOFFSET,
  1579. AS_TYPE,
  1580. AS_NOT,
  1581. AS_STRING,
  1582. AS_PLUS,
  1583. AS_MINUS,
  1584. AS_LPAREN,
  1585. AS_INTNUM :
  1586. begin
  1587. case oper.opr.typ of
  1588. OPR_REFERENCE :
  1589. {$ifndef x86_64}
  1590. { this is for the i386 scenario where you have
  1591. <load got into ebx>
  1592. mov eax, [ebx].offset globalvar
  1593. x86-64 uses RIP-based addresses (both for GOT and non-GOT
  1594. relative accesses)
  1595. }
  1596. if (actasmtoken=AS_OFFSET) and
  1597. (cs_create_pic in current_settings.moduleswitches) then
  1598. begin
  1599. Consume(AS_OFFSET);
  1600. oper.opr.ref.refaddr:=addr_pic;
  1601. BuildOperand(oper,false);
  1602. end
  1603. else
  1604. {$endif x86_64}
  1605. inc(oper.opr.ref.offset,BuildRefConstExpression);
  1606. OPR_LOCAL :
  1607. inc(oper.opr.localsymofs,BuildConstExpression);
  1608. OPR_NONE,
  1609. OPR_CONSTANT :
  1610. BuildConstantOperand(oper);
  1611. else
  1612. Message(asmr_e_invalid_operand_type);
  1613. end;
  1614. end;
  1615. AS_PTR :
  1616. begin
  1617. if not oper.hastype then
  1618. begin
  1619. if (oper.opr.typ=OPR_CONSTANT) then
  1620. begin
  1621. oper.typesize:=oper.opr.val;
  1622. { reset constant value of operand }
  1623. oper.opr.typ:=OPR_NONE;
  1624. oper.opr.val:=0;
  1625. end
  1626. else
  1627. Message(asmr_e_syn_operand);
  1628. end;
  1629. Consume(AS_PTR);
  1630. oper.InitRef;
  1631. { if the operand subscripts a record, the typesize will be
  1632. rest -> save it here and restore it afterwards }
  1633. l:=oper.typesize;
  1634. BuildOperand(oper,false);
  1635. oper.setsize(l,true);
  1636. end;
  1637. AS_ID : { A constant expression, or a Variable ref. }
  1638. Begin
  1639. { Label or Special symbol reference? }
  1640. if actasmpattern[1] = '@' then
  1641. Begin
  1642. if actasmpattern = '@RESULT' then
  1643. Begin
  1644. oper.SetupResult;
  1645. Consume(AS_ID);
  1646. end
  1647. else
  1648. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1649. begin
  1650. Message(asmr_w_CODE_and_DATA_not_supported);
  1651. Consume(AS_ID);
  1652. end
  1653. else
  1654. { Local Label }
  1655. begin
  1656. CreateLocalLabel(actasmpattern,hl,false);
  1657. Consume(AS_ID);
  1658. AddLabelOperand(hl);
  1659. end;
  1660. end
  1661. else
  1662. { support result for delphi modes }
  1663. if (m_objpas in current_settings.modeswitches) and (actasmpattern='RESULT') then
  1664. begin
  1665. oper.SetUpResult;
  1666. Consume(AS_ID);
  1667. end
  1668. { probably a variable or normal expression }
  1669. { or a procedure (such as in CALL ID) }
  1670. else
  1671. Begin
  1672. { is it a constant ? }
  1673. if SearchIConstant(actasmpattern,l) then
  1674. Begin
  1675. case oper.opr.typ of
  1676. OPR_REFERENCE :
  1677. inc(oper.opr.ref.offset,BuildRefConstExpression);
  1678. OPR_LOCAL :
  1679. inc(oper.opr.localsymofs,BuildRefConstExpression);
  1680. OPR_NONE,
  1681. OPR_CONSTANT :
  1682. BuildConstantOperand(oper);
  1683. else
  1684. Message(asmr_e_invalid_operand_type);
  1685. end;
  1686. end
  1687. else
  1688. { Check for pascal label }
  1689. if SearchLabel(actasmpattern,hl,false) then
  1690. begin
  1691. Consume(AS_ID);
  1692. AddLabelOperand(hl);
  1693. end
  1694. else
  1695. { is it a normal variable ? }
  1696. Begin
  1697. expr:=actasmpattern;
  1698. Consume(AS_ID);
  1699. { typecasting? }
  1700. if SearchType(expr,l) then
  1701. begin
  1702. oper.hastype:=true;
  1703. oper.typesize:=l;
  1704. case actasmtoken of
  1705. AS_LPAREN :
  1706. begin
  1707. { Support Type([Reference]) }
  1708. Consume(AS_LPAREN);
  1709. BuildOperand(oper,true);
  1710. { Delphi also supports Type(Register) and
  1711. interprets it the same as Type([Register]). }
  1712. if (oper.opr.typ = OPR_REGISTER) then
  1713. { This also sets base to the register. }
  1714. oper.InitRef;
  1715. Consume(AS_RPAREN);
  1716. end;
  1717. AS_LBRACKET :
  1718. begin
  1719. { Support Var.Type[Index] }
  1720. { Convert @label.Byte[1] to reference }
  1721. if oper.opr.typ=OPR_SYMBOL then
  1722. oper.initref;
  1723. end;
  1724. end;
  1725. end
  1726. else
  1727. begin
  1728. if not oper.SetupVar(expr,false) then
  1729. Begin
  1730. { not a variable, check special variables.. }
  1731. if expr = 'SELF' then
  1732. oper.SetupSelf
  1733. else
  1734. Message1(sym_e_unknown_id,expr);
  1735. expr:='';
  1736. end;
  1737. { indexed access to variable? }
  1738. if actasmtoken=AS_LBRACKET then
  1739. begin
  1740. { ... then the operand size is not known anymore }
  1741. oper.size:=OS_NO;
  1742. BuildReference(oper);
  1743. end;
  1744. end;
  1745. end;
  1746. end;
  1747. end;
  1748. AS_REGISTER : { Register, a variable reference or a constant reference }
  1749. begin
  1750. { save the type of register used. }
  1751. tempreg:=actasmregister;
  1752. Consume(AS_REGISTER);
  1753. if actasmtoken = AS_COLON then
  1754. Begin
  1755. Consume(AS_COLON);
  1756. oper.InitRef;
  1757. oper.opr.ref.segment:=tempreg;
  1758. BuildReference(oper);
  1759. end
  1760. else
  1761. { Simple register }
  1762. begin
  1763. if (oper.opr.typ <> OPR_NONE) then
  1764. Message(asmr_e_syn_operand);
  1765. oper.opr.typ:=OPR_REGISTER;
  1766. oper.opr.reg:=tempreg;
  1767. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  1768. end;
  1769. end;
  1770. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1771. Begin
  1772. BuildReference(oper);
  1773. end;
  1774. AS_SEG :
  1775. Begin
  1776. Message(asmr_e_seg_not_supported);
  1777. Consume(actasmtoken);
  1778. end;
  1779. AS_DWORD,
  1780. AS_BYTE,
  1781. AS_WORD,
  1782. AS_TBYTE,
  1783. AS_DQWORD,
  1784. AS_QWORD :
  1785. begin
  1786. { Type specifier }
  1787. oper.hastype:=true;
  1788. oper.typesize:=0;
  1789. case actasmtoken of
  1790. AS_DWORD : oper.typesize:=4;
  1791. AS_WORD : oper.typesize:=2;
  1792. AS_BYTE : oper.typesize:=1;
  1793. AS_QWORD : oper.typesize:=8;
  1794. AS_DQWORD : oper.typesize:=16;
  1795. AS_TBYTE : oper.typesize:=10;
  1796. else
  1797. internalerror(2010061101);
  1798. end;
  1799. Consume(actasmtoken);
  1800. if (actasmtoken=AS_LPAREN) then
  1801. begin
  1802. { Support "xxx ptr [Reference]" }
  1803. { in case the expression subscripts a record, the typesize
  1804. is reset, so save the explicit size we set above }
  1805. l:=oper.typesize;
  1806. Consume(AS_LPAREN);
  1807. BuildOperand(oper,true);
  1808. Consume(AS_RPAREN);
  1809. oper.setsize(l,true);
  1810. end;
  1811. end;
  1812. AS_SEPARATOR,
  1813. AS_END,
  1814. AS_COMMA,
  1815. AS_COLON:
  1816. begin
  1817. break;
  1818. end;
  1819. AS_RPAREN:
  1820. begin
  1821. if not istypecast then
  1822. begin
  1823. Message(asmr_e_syn_operand);
  1824. Consume(AS_RPAREN);
  1825. end
  1826. else
  1827. break;
  1828. end;
  1829. else
  1830. begin
  1831. Message(asmr_e_syn_operand);
  1832. RecoverConsume(true);
  1833. break;
  1834. end;
  1835. end;
  1836. until false;
  1837. { End of operand, update size if a typecast is forced }
  1838. if (oper.typesize<>0) and
  1839. (oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL]) then
  1840. oper.SetSize(oper.typesize,true);
  1841. end;
  1842. Procedure tx86intreader.BuildOpCode(instr : tx86instruction);
  1843. var
  1844. PrefixOp,OverrideOp: tasmop;
  1845. operandnum : longint;
  1846. is_far_const:boolean;
  1847. i:byte;
  1848. begin
  1849. PrefixOp:=A_None;
  1850. OverrideOp:=A_None;
  1851. is_far_const:=false;
  1852. { prefix seg opcode / prefix opcode }
  1853. repeat
  1854. if is_prefix(actopcode) then
  1855. with instr do
  1856. begin
  1857. OpOrder:=op_intel;
  1858. PrefixOp:=ActOpcode;
  1859. opcode:=ActOpcode;
  1860. condition:=ActCondition;
  1861. opsize:=ActOpsize;
  1862. ConcatInstruction(curlist);
  1863. consume(AS_OPCODE);
  1864. end
  1865. else
  1866. if is_override(actopcode) then
  1867. with instr do
  1868. begin
  1869. OpOrder:=op_intel;
  1870. OverrideOp:=ActOpcode;
  1871. opcode:=ActOpcode;
  1872. condition:=ActCondition;
  1873. opsize:=ActOpsize;
  1874. ConcatInstruction(curlist);
  1875. consume(AS_OPCODE);
  1876. end
  1877. else
  1878. break;
  1879. { allow for newline after prefix or override }
  1880. while actasmtoken=AS_SEPARATOR do
  1881. consume(AS_SEPARATOR);
  1882. until (actasmtoken<>AS_OPCODE);
  1883. { opcode }
  1884. if (actasmtoken <> AS_OPCODE) then
  1885. begin
  1886. Message(asmr_e_invalid_or_missing_opcode);
  1887. RecoverConsume(false);
  1888. exit;
  1889. end;
  1890. { Fill the instr object with the current state }
  1891. with instr do
  1892. begin
  1893. OpOrder:=op_intel;
  1894. Opcode:=ActOpcode;
  1895. condition:=ActCondition;
  1896. opsize:=ActOpsize;
  1897. { Valid combination of prefix/override and instruction ? }
  1898. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1899. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1900. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1901. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1902. end;
  1903. { pushf/popf/pusha/popa have to default to 16 bit in Intel mode
  1904. (Intel manual and Delphi-compatbile) -- setting the opsize for
  1905. these instructions doesn't change anything in the internal assember,
  1906. so change the opcode }
  1907. if (instr.opcode=A_POPF) then
  1908. instr.opcode:=A_POPFW
  1909. else if (instr.opcode=A_PUSHF) then
  1910. instr.opcode:=A_PUSHFW
  1911. else if (instr.opcode=A_PUSHA) then
  1912. instr.opcode:=A_PUSHAW
  1913. else if (instr.opcode=A_POPA) then
  1914. instr.opcode:=A_POPAW;
  1915. { We are reading operands, so opcode will be an AS_ID }
  1916. operandnum:=1;
  1917. is_far_const:=false;
  1918. Consume(AS_OPCODE);
  1919. { Zero operand opcode ? }
  1920. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1921. begin
  1922. operandnum:=0;
  1923. exit;
  1924. end;
  1925. { Read Operands }
  1926. repeat
  1927. case actasmtoken of
  1928. { End of asm operands for this opcode }
  1929. AS_END,
  1930. AS_SEPARATOR :
  1931. break;
  1932. { Operand delimiter }
  1933. AS_COMMA :
  1934. begin
  1935. if operandnum > Max_Operands then
  1936. Message(asmr_e_too_many_operands)
  1937. else
  1938. Inc(operandnum);
  1939. Consume(AS_COMMA);
  1940. end;
  1941. {Far constant, i.e. jmp $0000:$11111111.}
  1942. AS_COLON:
  1943. begin
  1944. is_far_const:=true;
  1945. if operandnum>1 then
  1946. message(asmr_e_too_many_operands)
  1947. else
  1948. inc(operandnum);
  1949. consume(AS_COLON);
  1950. end;
  1951. { Type specifier }
  1952. AS_NEAR,
  1953. AS_FAR :
  1954. begin
  1955. if actasmtoken = AS_NEAR then
  1956. begin
  1957. Message(asmr_w_near_ignored);
  1958. instr.opsize:=S_NEAR;
  1959. end
  1960. else
  1961. begin
  1962. Message(asmr_w_far_ignored);
  1963. instr.opsize:=S_FAR;
  1964. end;
  1965. Consume(actasmtoken);
  1966. if actasmtoken=AS_PTR then
  1967. begin
  1968. Consume(AS_PTR);
  1969. instr.Operands[operandnum].InitRef;
  1970. end;
  1971. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  1972. end;
  1973. else
  1974. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  1975. end; { end case }
  1976. until false;
  1977. instr.ops:=operandnum;
  1978. { Check operands }
  1979. for i:=1 to operandnum do
  1980. begin
  1981. if is_far_const and
  1982. (instr.operands[i].opr.typ<>OPR_CONSTANT) then
  1983. message(asmr_e_expr_illegal)
  1984. else
  1985. if instr.operands[i].opr.typ=OPR_NONE then
  1986. Message(asmr_e_syntax_error);
  1987. end;
  1988. { e.g. for "push dword 1", "push word 6" }
  1989. if (instr.ops=1) and
  1990. (instr.operands[1].typesize<>0) then
  1991. instr.operands[1].setsize(instr.operands[1].typesize,false);
  1992. end;
  1993. Procedure tx86intreader.BuildConstant(constsize: byte);
  1994. var
  1995. asmsymtyp : tasmsymtype;
  1996. asmsym,
  1997. expr: string;
  1998. value : aint;
  1999. Begin
  2000. Repeat
  2001. Case actasmtoken of
  2002. AS_STRING:
  2003. Begin
  2004. { DD and DW cases }
  2005. if constsize <> 1 then
  2006. Begin
  2007. if Not PadZero(actasmpattern,constsize) then
  2008. Message(scan_f_string_exceeds_line);
  2009. end;
  2010. expr:=actasmpattern;
  2011. Consume(AS_STRING);
  2012. Case actasmtoken of
  2013. AS_COMMA:
  2014. Consume(AS_COMMA);
  2015. AS_END,
  2016. AS_SEPARATOR: ;
  2017. else
  2018. Message(asmr_e_invalid_string_expression);
  2019. end;
  2020. ConcatString(curlist,expr);
  2021. end;
  2022. AS_PLUS,
  2023. AS_MINUS,
  2024. AS_LPAREN,
  2025. AS_NOT,
  2026. AS_INTNUM,
  2027. AS_ID :
  2028. Begin
  2029. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  2030. if asmsym<>'' then
  2031. begin
  2032. if constsize<>sizeof(pint) then
  2033. Message1(asmr_w_const32bit_for_address,asmsym);
  2034. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value)
  2035. end
  2036. else
  2037. ConcatConstant(curlist,value,constsize);
  2038. end;
  2039. AS_COMMA:
  2040. begin
  2041. Consume(AS_COMMA);
  2042. end;
  2043. AS_ALIGN,
  2044. AS_DB,
  2045. AS_DW,
  2046. AS_DD,
  2047. AS_DQ,
  2048. AS_OPCODE,
  2049. AS_END,
  2050. AS_SEPARATOR:
  2051. break;
  2052. else
  2053. begin
  2054. Message(asmr_e_syn_constant);
  2055. RecoverConsume(false);
  2056. end
  2057. end;
  2058. Until false;
  2059. end;
  2060. function tx86intreader.Assemble: tlinkedlist;
  2061. Var
  2062. hl : tasmlabel;
  2063. instr : Tx86Instruction;
  2064. Begin
  2065. Message1(asmr_d_start_reading,'intel');
  2066. inexpression:=FALSE;
  2067. firsttoken:=TRUE;
  2068. { sets up all opcode and register tables in uppercase
  2069. done in the construtor now
  2070. if not _asmsorted then
  2071. Begin
  2072. SetupTables;
  2073. _asmsorted:=TRUE;
  2074. end;
  2075. }
  2076. curlist:=TAsmList.Create;
  2077. { setup label linked list }
  2078. LocalLabelList:=TLocalLabelList.Create;
  2079. { we might need to know which parameters are passed in registers }
  2080. current_procinfo.generate_parameter_info;
  2081. { start tokenizer }
  2082. c:=current_scanner.asmgetcharstart;
  2083. gettoken;
  2084. { main loop }
  2085. repeat
  2086. case actasmtoken of
  2087. AS_LLABEL:
  2088. Begin
  2089. if CreateLocalLabel(actasmpattern,hl,true) then
  2090. ConcatLabel(curlist,hl);
  2091. Consume(AS_LLABEL);
  2092. end;
  2093. AS_LABEL:
  2094. Begin
  2095. if SearchLabel(upper(actasmpattern),hl,true) then
  2096. ConcatLabel(curlist,hl)
  2097. else
  2098. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2099. Consume(AS_LABEL);
  2100. end;
  2101. AS_DW :
  2102. Begin
  2103. inexpression:=true;
  2104. Consume(AS_DW);
  2105. BuildConstant(2);
  2106. inexpression:=false;
  2107. end;
  2108. AS_DB :
  2109. Begin
  2110. inexpression:=true;
  2111. Consume(AS_DB);
  2112. BuildConstant(1);
  2113. inexpression:=false;
  2114. end;
  2115. AS_DD :
  2116. Begin
  2117. inexpression:=true;
  2118. Consume(AS_DD);
  2119. BuildConstant(4);
  2120. inexpression:=false;
  2121. end;
  2122. {$ifdef cpu64bitaddr}
  2123. AS_DQ:
  2124. Begin
  2125. inexpression:=true;
  2126. Consume(AS_DQ);
  2127. BuildConstant(8);
  2128. inexpression:=false;
  2129. end;
  2130. {$endif cpu64bitaddr}
  2131. AS_ALIGN:
  2132. Begin
  2133. Consume(AS_ALIGN);
  2134. ConcatAlign(curlist,BuildConstExpression);
  2135. if actasmtoken<>AS_SEPARATOR then
  2136. Consume(AS_SEPARATOR);
  2137. end;
  2138. AS_OPCODE :
  2139. Begin
  2140. instr:=Tx86Instruction.Create(Tx86Operand);
  2141. BuildOpcode(instr);
  2142. with instr do
  2143. begin
  2144. { We need AT&T style operands }
  2145. Swapoperands;
  2146. { Must be done with args in ATT order }
  2147. CheckNonCommutativeOpcodes;
  2148. AddReferenceSizes;
  2149. SetInstructionOpsize;
  2150. CheckOperandSizes;
  2151. ConcatInstruction(curlist);
  2152. end;
  2153. instr.Free;
  2154. end;
  2155. AS_SEPARATOR :
  2156. Begin
  2157. Consume(AS_SEPARATOR);
  2158. end;
  2159. AS_END :
  2160. break; { end assembly block }
  2161. else
  2162. Begin
  2163. Message(asmr_e_syntax_error);
  2164. RecoverConsume(false);
  2165. end;
  2166. end; { end case }
  2167. until false;
  2168. { Check LocalLabelList }
  2169. LocalLabelList.CheckEmitted;
  2170. LocalLabelList.Free;
  2171. { Return the list in an asmnode }
  2172. assemble:=curlist;
  2173. Message1(asmr_d_finish_reading,'intel');
  2174. end;
  2175. end.