rax86int.pas 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201
  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);
  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_XOR;
  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');
  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'
  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_register(actasmpattern) then
  348. exit;
  349. if is_asmdirective(actasmpattern) then
  350. exit;
  351. if is_asmoperator(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. searchsym(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. actasmtoken:=AS_DOT;
  544. c:=current_scanner.asmgetchar;
  545. exit;
  546. end;
  547. '+' :
  548. begin
  549. actasmtoken:=AS_PLUS;
  550. c:=current_scanner.asmgetchar;
  551. exit;
  552. end;
  553. '-' :
  554. begin
  555. actasmtoken:=AS_MINUS;
  556. c:=current_scanner.asmgetchar;
  557. exit;
  558. end;
  559. '*' :
  560. begin
  561. actasmtoken:=AS_STAR;
  562. c:=current_scanner.asmgetchar;
  563. exit;
  564. end;
  565. '/' :
  566. begin
  567. actasmtoken:=AS_SLASH;
  568. c:=current_scanner.asmgetchar;
  569. exit;
  570. end;
  571. '0'..'9':
  572. begin
  573. actasmpattern:=c;
  574. c:=current_scanner.asmgetchar;
  575. { Get the possible characters }
  576. while c in ['0'..'9','A'..'F','a'..'f'] do
  577. begin
  578. actasmpattern:=actasmpattern + c;
  579. c:=current_scanner.asmgetchar;
  580. end;
  581. { Get ending character }
  582. uppervar(actasmpattern);
  583. c:=upcase(c);
  584. { possibly a binary number. }
  585. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  586. Begin
  587. { Delete the last binary specifier }
  588. delete(actasmpattern,length(actasmpattern),1);
  589. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  590. actasmtoken:=AS_INTNUM;
  591. exit;
  592. end
  593. else
  594. Begin
  595. case c of
  596. 'O' :
  597. Begin
  598. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  599. actasmtoken:=AS_INTNUM;
  600. c:=current_scanner.asmgetchar;
  601. exit;
  602. end;
  603. 'H' :
  604. Begin
  605. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  606. actasmtoken:=AS_INTNUM;
  607. c:=current_scanner.asmgetchar;
  608. exit;
  609. end;
  610. else { must be an integer number }
  611. begin
  612. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  613. actasmtoken:=AS_INTNUM;
  614. exit;
  615. end;
  616. end;
  617. end;
  618. end;
  619. ';','{',#13,#10 :
  620. begin
  621. c:=current_scanner.asmgetchar;
  622. firsttoken:=TRUE;
  623. actasmtoken:=AS_SEPARATOR;
  624. exit;
  625. end;
  626. else
  627. current_scanner.illegal_char(c);
  628. end;
  629. end;
  630. end;
  631. function tx86intreader.consume(t : tasmtoken):boolean;
  632. begin
  633. Consume:=true;
  634. if t<>actasmtoken then
  635. begin
  636. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  637. Consume:=false;
  638. end;
  639. repeat
  640. gettoken;
  641. until actasmtoken<>AS_NONE;
  642. end;
  643. procedure tx86intreader.RecoverConsume(allowcomma:boolean);
  644. begin
  645. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  646. begin
  647. if allowcomma and (actasmtoken=AS_COMMA) then
  648. break;
  649. Consume(actasmtoken);
  650. end;
  651. end;
  652. {*****************************************************************************
  653. Parsing Helpers
  654. *****************************************************************************}
  655. { This routine builds up a record offset after a AS_DOT
  656. token is encountered.
  657. On entry actasmtoken should be equal to AS_DOT }
  658. Procedure tx86intreader.BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint; var mangledname: string; needvmtofs: boolean);
  659. var
  660. s: string;
  661. Begin
  662. offset:=0;
  663. size:=0;
  664. s:=expr;
  665. while (actasmtoken=AS_DOT) do
  666. begin
  667. Consume(AS_DOT);
  668. if actasmtoken in [AS_BYTE,AS_ID,AS_WORD,AS_DWORD,AS_QWORD] then
  669. begin
  670. s:=s+'.'+actasmpattern;
  671. consume(actasmtoken);
  672. end
  673. else
  674. begin
  675. Consume(AS_ID);
  676. RecoverConsume(true);
  677. break;
  678. end;
  679. end;
  680. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs) then
  681. Message(asmr_e_building_record_offset);
  682. end;
  683. Procedure tx86intreader.BuildConstSymbolExpression(needofs,isref,startingminus:boolean;var value:aint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  684. var
  685. tempstr,expr,hs,mangledname : string;
  686. parenlevel : longint;
  687. l,k : aint;
  688. hasparen,
  689. errorflag,
  690. needvmtofs : boolean;
  691. prevtok : tasmtoken;
  692. hl : tasmlabel;
  693. hssymtyp : Tasmsymtype;
  694. def : tdef;
  695. sym : tsym;
  696. srsymtable : TSymtable;
  697. Begin
  698. { reset }
  699. value:=0;
  700. asmsym:='';
  701. asmsymtyp:=AT_DATA;
  702. errorflag:=FALSE;
  703. tempstr:='';
  704. expr:='';
  705. if startingminus then
  706. expr:='-';
  707. inexpression:=TRUE;
  708. parenlevel:=0;
  709. sym:=nil;
  710. needvmtofs:=FALSE;
  711. Repeat
  712. { Support ugly delphi constructs like: [ECX].1+2[EDX] }
  713. if isref and (actasmtoken=AS_LBRACKET) then
  714. break;
  715. Case actasmtoken of
  716. AS_LPAREN:
  717. Begin
  718. Consume(AS_LPAREN);
  719. expr:=expr + '(';
  720. inc(parenlevel);
  721. end;
  722. AS_RPAREN:
  723. Begin
  724. { Keep the AS_PAREN in actasmtoken, it is maybe a typecast }
  725. if parenlevel=0 then
  726. break;
  727. Consume(AS_RPAREN);
  728. expr:=expr + ')';
  729. dec(parenlevel);
  730. end;
  731. AS_SHL:
  732. Begin
  733. Consume(AS_SHL);
  734. expr:=expr + '<';
  735. end;
  736. AS_SHR:
  737. Begin
  738. Consume(AS_SHR);
  739. expr:=expr + '>';
  740. end;
  741. AS_SLASH:
  742. Begin
  743. Consume(AS_SLASH);
  744. expr:=expr + '/';
  745. end;
  746. AS_MOD:
  747. Begin
  748. Consume(AS_MOD);
  749. expr:=expr + '%';
  750. end;
  751. AS_STAR:
  752. Begin
  753. Consume(AS_STAR);
  754. if isref and (actasmtoken=AS_REGISTER) then
  755. break;
  756. expr:=expr + '*';
  757. end;
  758. AS_PLUS:
  759. Begin
  760. Consume(AS_PLUS);
  761. if isref and (actasmtoken=AS_REGISTER) then
  762. break;
  763. expr:=expr + '+';
  764. end;
  765. AS_MINUS:
  766. Begin
  767. Consume(AS_MINUS);
  768. expr:=expr + '-';
  769. end;
  770. AS_AND:
  771. Begin
  772. Consume(AS_AND);
  773. expr:=expr + '&';
  774. end;
  775. AS_NOT:
  776. Begin
  777. Consume(AS_NOT);
  778. expr:=expr + '~';
  779. end;
  780. AS_XOR:
  781. Begin
  782. Consume(AS_XOR);
  783. expr:=expr + '^';
  784. end;
  785. AS_OR:
  786. Begin
  787. Consume(AS_OR);
  788. expr:=expr + '|';
  789. end;
  790. AS_INTNUM:
  791. Begin
  792. expr:=expr + actasmpattern;
  793. Consume(AS_INTNUM);
  794. end;
  795. AS_VMTOFFSET,
  796. AS_OFFSET:
  797. begin
  798. if (actasmtoken = AS_OFFSET) then
  799. needofs:=true
  800. else
  801. needvmtofs:=true;
  802. Consume(actasmtoken);
  803. if actasmtoken<>AS_ID then
  804. Message(asmr_e_offset_without_identifier);
  805. end;
  806. AS_SIZEOF,
  807. AS_TYPE:
  808. begin
  809. l:=0;
  810. hasparen:=false;
  811. Consume(actasmtoken);
  812. if actasmtoken=AS_LPAREN then
  813. begin
  814. hasparen:=true;
  815. Consume(AS_LPAREN);
  816. end;
  817. if actasmtoken<>AS_ID then
  818. Message(asmr_e_type_without_identifier)
  819. else
  820. begin
  821. tempstr:=actasmpattern;
  822. Consume(AS_ID);
  823. if actasmtoken=AS_DOT then
  824. begin
  825. BuildRecordOffsetSize(tempstr,k,l,mangledname,false);
  826. if mangledname<>'' then
  827. { procsym }
  828. Message(asmr_e_wrong_sym_type);
  829. end
  830. else
  831. begin
  832. searchsym(tempstr,sym,srsymtable);
  833. if assigned(sym) then
  834. begin
  835. case sym.typ of
  836. staticvarsym,
  837. localvarsym,
  838. paravarsym :
  839. l:=tabstractvarsym(sym).getsize;
  840. typesym :
  841. l:=ttypesym(sym).typedef.size;
  842. else
  843. Message(asmr_e_wrong_sym_type);
  844. end;
  845. end
  846. else
  847. Message1(sym_e_unknown_id,tempstr);
  848. end;
  849. end;
  850. str(l, tempstr);
  851. expr:=expr + tempstr;
  852. if hasparen then
  853. Consume(AS_RPAREN);
  854. end;
  855. AS_PTR :
  856. begin
  857. { Support ugly delphi constructs like <constant> PTR [ref] }
  858. break;
  859. end;
  860. AS_STRING:
  861. begin
  862. l:=0;
  863. case Length(actasmpattern) of
  864. 1 :
  865. l:=ord(actasmpattern[1]);
  866. 2 :
  867. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  868. 3 :
  869. l:=ord(actasmpattern[3]) +
  870. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  871. 4 :
  872. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  873. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  874. else
  875. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  876. end;
  877. str(l, tempstr);
  878. expr:=expr + tempstr;
  879. Consume(AS_STRING);
  880. end;
  881. AS_ID:
  882. begin
  883. hs:='';
  884. hssymtyp:=AT_DATA;
  885. def:=nil;
  886. tempstr:=actasmpattern;
  887. prevtok:=prevasmtoken;
  888. { stop parsing a constant expression if we find an opcode after a
  889. non-operator like "db $66 mov eax,ebx" }
  890. if (prevtok in [AS_ID,AS_INTNUM,AS_RPAREN]) and
  891. is_asmopcode(actasmpattern) then
  892. break;
  893. consume(AS_ID);
  894. if SearchIConstant(tempstr,l) then
  895. begin
  896. str(l, tempstr);
  897. expr:=expr + tempstr;
  898. end
  899. else
  900. begin
  901. if is_locallabel(tempstr) then
  902. begin
  903. CreateLocalLabel(tempstr,hl,false);
  904. hs:=hl.name;
  905. hssymtyp:=AT_FUNCTION;
  906. end
  907. else
  908. if SearchLabel(tempstr,hl,false) then
  909. begin
  910. hs:=hl.name;
  911. hssymtyp:=AT_FUNCTION;
  912. end
  913. else
  914. begin
  915. searchsym(tempstr,sym,srsymtable);
  916. if assigned(sym) then
  917. begin
  918. case sym.typ of
  919. staticvarsym :
  920. begin
  921. hs:=tstaticvarsym(sym).mangledname;
  922. def:=tstaticvarsym(sym).vardef;
  923. end;
  924. localvarsym,
  925. paravarsym :
  926. begin
  927. Message(asmr_e_no_local_or_para_allowed);
  928. end;
  929. procsym :
  930. begin
  931. if Tprocsym(sym).ProcdefList.Count>1 then
  932. Message(asmr_w_calling_overload_func);
  933. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  934. hssymtyp:=AT_FUNCTION;
  935. end;
  936. typesym :
  937. begin
  938. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  939. Message(asmr_e_wrong_sym_type);
  940. end;
  941. fieldvarsym :
  942. begin
  943. tempstr:=upper(tdef(sym.owner.defowner).GetTypeName)+'.'+tempstr;
  944. end;
  945. else
  946. Message(asmr_e_wrong_sym_type);
  947. end;
  948. end
  949. else
  950. Message1(sym_e_unknown_id,tempstr);
  951. end;
  952. { symbol found? }
  953. if hs<>'' then
  954. begin
  955. if asmsym='' then
  956. begin
  957. asmsym:=hs;
  958. asmsymtyp:=hssymtyp;
  959. end
  960. else
  961. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  962. if (expr='') or (expr[length(expr)]='+') then
  963. begin
  964. { don't remove the + if there could be a record field }
  965. if actasmtoken<>AS_DOT then
  966. delete(expr,length(expr),1);
  967. end
  968. else
  969. if needofs then
  970. begin
  971. if (prevtok<>AS_OFFSET) then
  972. Message(asmr_e_need_offset);
  973. end
  974. else
  975. Message(asmr_e_only_add_relocatable_symbol);
  976. end;
  977. if (actasmtoken=AS_DOT) or
  978. (assigned(sym) and
  979. (sym.typ = fieldvarsym)) then
  980. begin
  981. BuildRecordOffsetSize(tempstr,l,k,hs,needvmtofs);
  982. if hs <> '' then
  983. hssymtyp:=AT_FUNCTION
  984. else
  985. begin
  986. str(l, tempstr);
  987. expr:=expr + tempstr;
  988. end
  989. end
  990. else
  991. begin
  992. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  993. delete(expr,length(expr),1);
  994. end;
  995. if (actasmtoken=AS_LBRACKET) and
  996. assigned(def) and
  997. (def.typ=arraydef) then
  998. begin
  999. consume(AS_LBRACKET);
  1000. l:=BuildConstExpression;
  1001. if l<tarraydef(def).lowrange then
  1002. begin
  1003. Message(asmr_e_constant_out_of_bounds);
  1004. l:=0;
  1005. end
  1006. else
  1007. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  1008. str(l, tempstr);
  1009. expr:=expr + '+' + tempstr;
  1010. consume(AS_RBRACKET);
  1011. end;
  1012. end;
  1013. { check if there are wrong operator used like / or mod etc. }
  1014. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  1015. Message(asmr_e_only_add_relocatable_symbol);
  1016. end;
  1017. AS_ALIGN,
  1018. AS_DB,
  1019. AS_DW,
  1020. AS_DD,
  1021. AS_DQ,
  1022. AS_END,
  1023. AS_RBRACKET,
  1024. AS_SEPARATOR,
  1025. AS_COMMA,
  1026. AS_COLON:
  1027. break;
  1028. else
  1029. begin
  1030. { write error only once. }
  1031. if not errorflag then
  1032. Message(asmr_e_invalid_constant_expression);
  1033. { consume tokens until we find COMMA or SEPARATOR }
  1034. Consume(actasmtoken);
  1035. errorflag:=TRUE;
  1036. end;
  1037. end;
  1038. Until false;
  1039. { calculate expression }
  1040. if not ErrorFlag then
  1041. value:=CalculateExpression(expr)
  1042. else
  1043. value:=0;
  1044. { no longer in an expression }
  1045. inexpression:=FALSE;
  1046. end;
  1047. Function tx86intreader.BuildConstExpression:aint;
  1048. var
  1049. l : aint;
  1050. hs : string;
  1051. hssymtyp : TAsmsymtype;
  1052. begin
  1053. BuildConstSymbolExpression(false,false,false,l,hs,hssymtyp);
  1054. if hs<>'' then
  1055. Message(asmr_e_relocatable_symbol_not_allowed);
  1056. BuildConstExpression:=l;
  1057. end;
  1058. Function tx86intreader.BuildRefConstExpression:aint;
  1059. var
  1060. l : aint;
  1061. hs : string;
  1062. hssymtyp : TAsmsymtype;
  1063. begin
  1064. BuildConstSymbolExpression(false,true,false,l,hs,hssymtyp);
  1065. if hs<>'' then
  1066. Message(asmr_e_relocatable_symbol_not_allowed);
  1067. BuildRefConstExpression:=l;
  1068. end;
  1069. procedure tx86intreader.BuildReference(oper : tx86operand);
  1070. var
  1071. scale : byte;
  1072. k,l : aint;
  1073. tempstr,hs : string;
  1074. tempsymtyp : tasmsymtype;
  1075. code : integer;
  1076. hreg : tregister;
  1077. GotStar,GotOffset,HadVar,
  1078. GotPlus,Negative : boolean;
  1079. hl : tasmlabel;
  1080. Begin
  1081. Consume(AS_LBRACKET);
  1082. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  1083. oper.InitRef;
  1084. GotStar:=false;
  1085. GotPlus:=true;
  1086. GotOffset:=false;
  1087. Negative:=false;
  1088. Scale:=0;
  1089. repeat
  1090. if GotOffset and (actasmtoken<>AS_ID) then
  1091. Message(asmr_e_invalid_reference_syntax);
  1092. Case actasmtoken of
  1093. AS_ID, { Constant reference expression OR variable reference expression }
  1094. AS_VMTOFFSET:
  1095. Begin
  1096. if not GotPlus then
  1097. Message(asmr_e_invalid_reference_syntax);
  1098. GotStar:=false;
  1099. GotPlus:=false;
  1100. if (actasmtoken = AS_VMTOFFSET) or
  1101. (SearchIConstant(actasmpattern,l) or
  1102. SearchRecordType(actasmpattern)) then
  1103. begin
  1104. l:=BuildRefConstExpression;
  1105. GotPlus:=(prevasmtoken=AS_PLUS);
  1106. GotStar:=(prevasmtoken=AS_STAR);
  1107. case oper.opr.typ of
  1108. OPR_LOCAL :
  1109. begin
  1110. if GotStar then
  1111. Message(asmr_e_invalid_reference_syntax);
  1112. if negative then
  1113. Dec(oper.opr.localsymofs,l)
  1114. else
  1115. Inc(oper.opr.localsymofs,l);
  1116. end;
  1117. OPR_REFERENCE :
  1118. begin
  1119. if GotStar then
  1120. oper.opr.ref.scalefactor:=l
  1121. else
  1122. begin
  1123. if negative then
  1124. Dec(oper.opr.ref.offset,l)
  1125. else
  1126. Inc(oper.opr.ref.offset,l);
  1127. end;
  1128. end;
  1129. end;
  1130. end
  1131. else
  1132. Begin
  1133. if negative and not oper.hasvar then
  1134. Message(asmr_e_only_add_relocatable_symbol)
  1135. else if oper.hasvar and not GotOffset and
  1136. (not negative or assigned(oper.opr.ref.relsymbol)) then
  1137. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1138. HadVar:=oper.hasvar and GotOffset;
  1139. tempstr:=actasmpattern;
  1140. Consume(AS_ID);
  1141. { typecasting? }
  1142. if (actasmtoken=AS_LPAREN) and
  1143. SearchType(tempstr,l) then
  1144. begin
  1145. oper.hastype:=true;
  1146. oper.typesize:=l;
  1147. Consume(AS_LPAREN);
  1148. BuildOperand(oper,true);
  1149. Consume(AS_RPAREN);
  1150. end
  1151. else
  1152. if is_locallabel(tempstr) then
  1153. begin
  1154. CreateLocalLabel(tempstr,hl,false);
  1155. oper.InitRef;
  1156. if not negative then
  1157. begin
  1158. oper.opr.ref.symbol:=hl;
  1159. oper.hasvar:=true;
  1160. end
  1161. else
  1162. oper.opr.ref.relsymbol:=hl;
  1163. end
  1164. else
  1165. if oper.SetupVar(tempstr,GotOffset) then
  1166. begin
  1167. { force OPR_LOCAL to be a reference }
  1168. if oper.opr.typ=OPR_LOCAL then
  1169. oper.opr.localforceref:=true;
  1170. end
  1171. else
  1172. Message1(sym_e_unknown_id,tempstr);
  1173. { record.field ? }
  1174. if actasmtoken=AS_DOT then
  1175. begin
  1176. BuildRecordOffsetSize(tempstr,l,k,hs,false);
  1177. if (hs<>'') then
  1178. Message(asmr_e_invalid_symbol_ref);
  1179. case oper.opr.typ of
  1180. OPR_LOCAL :
  1181. inc(oper.opr.localsymofs,l);
  1182. OPR_REFERENCE :
  1183. inc(oper.opr.ref.offset,l);
  1184. end;
  1185. end;
  1186. if GotOffset then
  1187. begin
  1188. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1189. begin
  1190. if (oper.opr.typ=OPR_REFERENCE) then
  1191. oper.opr.ref.base:=NR_NO;
  1192. oper.hasvar:=hadvar;
  1193. end
  1194. else
  1195. begin
  1196. if oper.hasvar and hadvar then
  1197. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1198. { should we allow ?? }
  1199. end;
  1200. end;
  1201. end;
  1202. GotOffset:=false;
  1203. end;
  1204. AS_PLUS :
  1205. Begin
  1206. Consume(AS_PLUS);
  1207. Negative:=false;
  1208. GotPlus:=true;
  1209. GotStar:=false;
  1210. Scale:=0;
  1211. end;
  1212. AS_DOT :
  1213. Begin
  1214. { Handle like a + }
  1215. Consume(AS_DOT);
  1216. Negative:=false;
  1217. GotPlus:=true;
  1218. GotStar:=false;
  1219. Scale:=0;
  1220. end;
  1221. AS_MINUS :
  1222. begin
  1223. Consume(AS_MINUS);
  1224. Negative:=true;
  1225. GotPlus:=true;
  1226. GotStar:=false;
  1227. Scale:=0;
  1228. end;
  1229. AS_STAR : { Scaling, with eax*4 order }
  1230. begin
  1231. Consume(AS_STAR);
  1232. hs:='';
  1233. l:=0;
  1234. case actasmtoken of
  1235. AS_LPAREN :
  1236. l:=BuildConstExpression;
  1237. AS_INTNUM:
  1238. Begin
  1239. hs:=actasmpattern;
  1240. Consume(AS_INTNUM);
  1241. end;
  1242. AS_REGISTER :
  1243. begin
  1244. case oper.opr.typ of
  1245. OPR_REFERENCE :
  1246. begin
  1247. if oper.opr.ref.scalefactor=0 then
  1248. begin
  1249. if scale<>0 then
  1250. begin
  1251. oper.opr.ref.scalefactor:=scale;
  1252. scale:=0;
  1253. end
  1254. else
  1255. Message(asmr_e_wrong_scale_factor);
  1256. end
  1257. else
  1258. Message(asmr_e_invalid_reference_syntax);
  1259. end;
  1260. OPR_LOCAL :
  1261. begin
  1262. if oper.opr.localscale=0 then
  1263. begin
  1264. if scale<>0 then
  1265. begin
  1266. oper.opr.localscale:=scale;
  1267. scale:=0;
  1268. end
  1269. else
  1270. Message(asmr_e_wrong_scale_factor);
  1271. end
  1272. else
  1273. Message(asmr_e_invalid_reference_syntax);
  1274. end;
  1275. end;
  1276. end;
  1277. else
  1278. Message(asmr_e_invalid_reference_syntax);
  1279. end;
  1280. if actasmtoken<>AS_REGISTER then
  1281. begin
  1282. if hs<>'' then
  1283. val(hs,l,code);
  1284. case oper.opr.typ of
  1285. OPR_REFERENCE :
  1286. oper.opr.ref.scalefactor:=l;
  1287. OPR_LOCAL :
  1288. oper.opr.localscale:=l;
  1289. end;
  1290. if l>9 then
  1291. Message(asmr_e_wrong_scale_factor);
  1292. end;
  1293. GotPlus:=false;
  1294. GotStar:=false;
  1295. end;
  1296. AS_REGISTER :
  1297. begin
  1298. if not((GotPlus and (not Negative)) or
  1299. GotStar) then
  1300. Message(asmr_e_invalid_reference_syntax);
  1301. hreg:=actasmregister;
  1302. Consume(AS_REGISTER);
  1303. { this register will be the index:
  1304. 1. just read a *
  1305. 2. next token is a *
  1306. 3. base register is already used }
  1307. case oper.opr.typ of
  1308. OPR_LOCAL :
  1309. begin
  1310. if (oper.opr.localindexreg<>NR_NO) then
  1311. Message(asmr_e_multiple_index);
  1312. oper.opr.localindexreg:=hreg;
  1313. if scale<>0 then
  1314. begin
  1315. oper.opr.localscale:=scale;
  1316. scale:=0;
  1317. end;
  1318. end;
  1319. OPR_REFERENCE :
  1320. begin
  1321. if (GotStar) or
  1322. (actasmtoken=AS_STAR) or
  1323. (oper.opr.ref.base<>NR_NO) then
  1324. begin
  1325. if (oper.opr.ref.index<>NR_NO) then
  1326. Message(asmr_e_multiple_index);
  1327. oper.opr.ref.index:=hreg;
  1328. if scale<>0 then
  1329. begin
  1330. oper.opr.ref.scalefactor:=scale;
  1331. scale:=0;
  1332. end;
  1333. end
  1334. else
  1335. oper.opr.ref.base:=hreg;
  1336. end;
  1337. end;
  1338. GotPlus:=false;
  1339. GotStar:=false;
  1340. end;
  1341. AS_OFFSET :
  1342. begin
  1343. Consume(AS_OFFSET);
  1344. GotOffset:=true;
  1345. end;
  1346. AS_TYPE,
  1347. AS_NOT,
  1348. AS_STRING,
  1349. AS_INTNUM,
  1350. AS_LPAREN : { Constant reference expression }
  1351. begin
  1352. if not GotPlus and not GotStar then
  1353. Message(asmr_e_invalid_reference_syntax);
  1354. BuildConstSymbolExpression(true,true,GotPlus and negative,l,tempstr,tempsymtyp);
  1355. { already handled by BuildConstSymbolExpression(); must be
  1356. handled there to avoid [reg-1+1] being interpreted as
  1357. [reg-(1+1)] }
  1358. negative:=false;
  1359. if tempstr<>'' then
  1360. begin
  1361. if GotStar then
  1362. Message(asmr_e_only_add_relocatable_symbol);
  1363. if not assigned(oper.opr.ref.symbol) then
  1364. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr)
  1365. else
  1366. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1367. end;
  1368. case oper.opr.typ of
  1369. OPR_REFERENCE :
  1370. begin
  1371. if GotStar then
  1372. oper.opr.ref.scalefactor:=l
  1373. else if (prevasmtoken = AS_STAR) then
  1374. begin
  1375. if scale<>0 then
  1376. scale:=l*scale
  1377. else
  1378. scale:=l;
  1379. end
  1380. else
  1381. Inc(oper.opr.ref.offset,l);
  1382. end;
  1383. OPR_LOCAL :
  1384. begin
  1385. if GotStar then
  1386. oper.opr.localscale:=l
  1387. else if (prevasmtoken = AS_STAR) then
  1388. begin
  1389. if scale<>0 then
  1390. scale:=l*scale
  1391. else
  1392. scale:=l;
  1393. end
  1394. else
  1395. Inc(oper.opr.localsymofs,l);
  1396. end;
  1397. end;
  1398. GotPlus:=(prevasmtoken=AS_PLUS) or
  1399. (prevasmtoken=AS_MINUS);
  1400. if GotPlus then
  1401. negative := prevasmtoken = AS_MINUS;
  1402. GotStar:=(prevasmtoken=AS_STAR);
  1403. end;
  1404. AS_RBRACKET :
  1405. begin
  1406. if GotPlus or GotStar then
  1407. Message(asmr_e_invalid_reference_syntax);
  1408. Consume(AS_RBRACKET);
  1409. break;
  1410. end;
  1411. else
  1412. Begin
  1413. Message(asmr_e_invalid_reference_syntax);
  1414. RecoverConsume(true);
  1415. break;
  1416. end;
  1417. end;
  1418. until false;
  1419. end;
  1420. Procedure tx86intreader.BuildConstantOperand(oper: tx86operand);
  1421. var
  1422. l : aint;
  1423. tempstr : string;
  1424. tempsymtyp : tasmsymtype;
  1425. begin
  1426. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1427. Message(asmr_e_invalid_operand_type);
  1428. BuildConstSymbolExpression(true,false,false,l,tempstr,tempsymtyp);
  1429. if tempstr<>'' then
  1430. begin
  1431. oper.opr.typ:=OPR_SYMBOL;
  1432. oper.opr.symofs:=l;
  1433. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr);
  1434. end
  1435. else
  1436. if oper.opr.typ=OPR_NONE then
  1437. begin
  1438. oper.opr.typ:=OPR_CONSTANT;
  1439. oper.opr.val:=l;
  1440. end
  1441. else
  1442. inc(oper.opr.val,l);
  1443. end;
  1444. Procedure tx86intreader.BuildOperand(oper: tx86operand;istypecast:boolean);
  1445. procedure AddLabelOperand(hl:tasmlabel);
  1446. begin
  1447. if (oper.opr.typ=OPR_NONE) and
  1448. is_calljmp(actopcode) then
  1449. begin
  1450. oper.opr.typ:=OPR_SYMBOL;
  1451. oper.opr.symbol:=hl;
  1452. end
  1453. else
  1454. begin
  1455. oper.InitRef;
  1456. oper.opr.ref.symbol:=hl;
  1457. end;
  1458. end;
  1459. var
  1460. expr,
  1461. hs : string;
  1462. tempreg : tregister;
  1463. l : aint;
  1464. hl : tasmlabel;
  1465. toffset,
  1466. tsize : aint;
  1467. begin
  1468. expr:='';
  1469. repeat
  1470. if actasmtoken=AS_DOT then
  1471. begin
  1472. if expr<>'' then
  1473. begin
  1474. BuildRecordOffsetSize(expr,toffset,tsize,hs,false);
  1475. if (oper.opr.typ<>OPR_NONE) and
  1476. (hs<>'') then
  1477. Message(asmr_e_wrong_sym_type);
  1478. oper.SetSize(tsize,true);
  1479. { we have used the size of a field. Reset the typesize of the record }
  1480. oper.typesize:=0;
  1481. case oper.opr.typ of
  1482. OPR_LOCAL :
  1483. begin
  1484. { don't allow direct access to fields of parameters, becuase that
  1485. will generate buggy code. Allow it only for explicit typecasting
  1486. and when the parameter is in a register (delphi compatible) }
  1487. if (not oper.hastype) and
  1488. (oper.opr.localsym.owner.symtabletype=parasymtable) and
  1489. (current_procinfo.procdef.proccalloption<>pocall_register) then
  1490. Message(asmr_e_cannot_access_field_directly_for_parameters);
  1491. inc(oper.opr.localsymofs,toffset)
  1492. end;
  1493. OPR_CONSTANT :
  1494. inc(oper.opr.val,toffset);
  1495. OPR_REFERENCE :
  1496. inc(oper.opr.ref.offset,toffset);
  1497. OPR_NONE :
  1498. begin
  1499. if (hs <> '') then
  1500. begin
  1501. oper.opr.typ:=OPR_SYMBOL;
  1502. oper.opr.symbol:=current_asmdata.RefAsmSymbol(hs);
  1503. end
  1504. else
  1505. begin
  1506. oper.opr.typ:=OPR_CONSTANT;
  1507. oper.opr.val:=toffset;
  1508. end;
  1509. end;
  1510. OPR_REGISTER :
  1511. Message(asmr_e_invalid_reference_syntax);
  1512. OPR_SYMBOL:
  1513. Message(asmr_e_invalid_symbol_ref);
  1514. else
  1515. internalerror(200309222);
  1516. end;
  1517. expr:='';
  1518. end
  1519. else
  1520. begin
  1521. { See it as a separator }
  1522. Consume(AS_DOT);
  1523. end;
  1524. end;
  1525. case actasmtoken of
  1526. AS_OFFSET,
  1527. AS_SIZEOF,
  1528. AS_VMTOFFSET,
  1529. AS_TYPE,
  1530. AS_NOT,
  1531. AS_STRING,
  1532. AS_PLUS,
  1533. AS_MINUS,
  1534. AS_LPAREN,
  1535. AS_INTNUM :
  1536. begin
  1537. case oper.opr.typ of
  1538. OPR_REFERENCE :
  1539. inc(oper.opr.ref.offset,BuildRefConstExpression);
  1540. OPR_LOCAL :
  1541. inc(oper.opr.localsymofs,BuildConstExpression);
  1542. OPR_NONE,
  1543. OPR_CONSTANT :
  1544. BuildConstantOperand(oper);
  1545. else
  1546. Message(asmr_e_invalid_operand_type);
  1547. end;
  1548. end;
  1549. AS_PTR :
  1550. begin
  1551. if not oper.hastype then
  1552. begin
  1553. if (oper.opr.typ=OPR_CONSTANT) then
  1554. begin
  1555. oper.typesize:=oper.opr.val;
  1556. { reset constant value of operand }
  1557. oper.opr.typ:=OPR_NONE;
  1558. oper.opr.val:=0;
  1559. end
  1560. else
  1561. Message(asmr_e_syn_operand);
  1562. end;
  1563. Consume(AS_PTR);
  1564. oper.InitRef;
  1565. BuildOperand(oper,false);
  1566. end;
  1567. AS_ID : { A constant expression, or a Variable ref. }
  1568. Begin
  1569. { Label or Special symbol reference? }
  1570. if actasmpattern[1] = '@' then
  1571. Begin
  1572. if actasmpattern = '@RESULT' then
  1573. Begin
  1574. oper.SetupResult;
  1575. Consume(AS_ID);
  1576. end
  1577. else
  1578. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1579. begin
  1580. Message(asmr_w_CODE_and_DATA_not_supported);
  1581. Consume(AS_ID);
  1582. end
  1583. else
  1584. { Local Label }
  1585. begin
  1586. CreateLocalLabel(actasmpattern,hl,false);
  1587. Consume(AS_ID);
  1588. AddLabelOperand(hl);
  1589. end;
  1590. end
  1591. else
  1592. { support result for delphi modes }
  1593. if (m_objpas in current_settings.modeswitches) and (actasmpattern='RESULT') then
  1594. begin
  1595. oper.SetUpResult;
  1596. Consume(AS_ID);
  1597. end
  1598. { probably a variable or normal expression }
  1599. { or a procedure (such as in CALL ID) }
  1600. else
  1601. Begin
  1602. { is it a constant ? }
  1603. if SearchIConstant(actasmpattern,l) then
  1604. Begin
  1605. case oper.opr.typ of
  1606. OPR_REFERENCE :
  1607. inc(oper.opr.ref.offset,BuildRefConstExpression);
  1608. OPR_LOCAL :
  1609. inc(oper.opr.localsymofs,BuildRefConstExpression);
  1610. OPR_NONE,
  1611. OPR_CONSTANT :
  1612. BuildConstantOperand(oper);
  1613. else
  1614. Message(asmr_e_invalid_operand_type);
  1615. end;
  1616. end
  1617. else
  1618. { Check for pascal label }
  1619. if SearchLabel(actasmpattern,hl,false) then
  1620. begin
  1621. Consume(AS_ID);
  1622. AddLabelOperand(hl);
  1623. end
  1624. else
  1625. { is it a normal variable ? }
  1626. Begin
  1627. expr:=actasmpattern;
  1628. Consume(AS_ID);
  1629. { typecasting? }
  1630. if SearchType(expr,l) then
  1631. begin
  1632. oper.hastype:=true;
  1633. oper.typesize:=l;
  1634. case actasmtoken of
  1635. AS_LPAREN :
  1636. begin
  1637. { Support Type([Reference]) }
  1638. Consume(AS_LPAREN);
  1639. BuildOperand(oper,true);
  1640. { Delphi also supports Type(Register) and
  1641. interprets it the same as Type([Register]). }
  1642. if (oper.opr.typ = OPR_REGISTER) then
  1643. { This also sets base to the register. }
  1644. oper.InitRef;
  1645. Consume(AS_RPAREN);
  1646. end;
  1647. AS_LBRACKET :
  1648. begin
  1649. { Support Var.Type[Index] }
  1650. { Convert @label.Byte[1] to reference }
  1651. if oper.opr.typ=OPR_SYMBOL then
  1652. oper.initref;
  1653. end;
  1654. end;
  1655. end
  1656. else
  1657. begin
  1658. if not oper.SetupVar(expr,false) then
  1659. Begin
  1660. { not a variable, check special variables.. }
  1661. if expr = 'SELF' then
  1662. oper.SetupSelf
  1663. else
  1664. Message1(sym_e_unknown_id,expr);
  1665. expr:='';
  1666. end;
  1667. end;
  1668. end;
  1669. end;
  1670. end;
  1671. AS_REGISTER : { Register, a variable reference or a constant reference }
  1672. begin
  1673. { save the type of register used. }
  1674. tempreg:=actasmregister;
  1675. Consume(AS_REGISTER);
  1676. if actasmtoken = AS_COLON then
  1677. Begin
  1678. Consume(AS_COLON);
  1679. oper.InitRef;
  1680. oper.opr.ref.segment:=tempreg;
  1681. BuildReference(oper);
  1682. end
  1683. else
  1684. { Simple register }
  1685. begin
  1686. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1687. Message(asmr_e_invalid_operand_type);
  1688. oper.opr.typ:=OPR_REGISTER;
  1689. oper.opr.reg:=tempreg;
  1690. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  1691. end;
  1692. end;
  1693. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1694. Begin
  1695. BuildReference(oper);
  1696. end;
  1697. AS_SEG :
  1698. Begin
  1699. Message(asmr_e_seg_not_supported);
  1700. Consume(actasmtoken);
  1701. end;
  1702. AS_DWORD,
  1703. AS_BYTE,
  1704. AS_WORD,
  1705. AS_TBYTE,
  1706. AS_DQWORD,
  1707. AS_QWORD :
  1708. begin
  1709. { Type specifier }
  1710. oper.hastype:=true;
  1711. oper.typesize:=0;
  1712. case actasmtoken of
  1713. AS_DWORD : oper.typesize:=4;
  1714. AS_WORD : oper.typesize:=2;
  1715. AS_BYTE : oper.typesize:=1;
  1716. AS_QWORD : oper.typesize:=8;
  1717. AS_DQWORD : oper.typesize:=16;
  1718. AS_TBYTE : oper.typesize:=10;
  1719. end;
  1720. Consume(actasmtoken);
  1721. if (actasmtoken=AS_LPAREN) then
  1722. begin
  1723. { Support Type([Reference]) }
  1724. Consume(AS_LPAREN);
  1725. BuildOperand(oper,true);
  1726. Consume(AS_RPAREN);
  1727. end;
  1728. end;
  1729. AS_SEPARATOR,
  1730. AS_END,
  1731. AS_COMMA,
  1732. AS_COLON:
  1733. begin
  1734. break;
  1735. end;
  1736. AS_RPAREN:
  1737. begin
  1738. if not istypecast then
  1739. begin
  1740. Message(asmr_e_syn_operand);
  1741. Consume(AS_RPAREN);
  1742. end
  1743. else
  1744. break;
  1745. end;
  1746. else
  1747. begin
  1748. Message(asmr_e_syn_operand);
  1749. RecoverConsume(true);
  1750. break;
  1751. end;
  1752. end;
  1753. until false;
  1754. { End of operand, update size if a typecast is forced }
  1755. if (oper.typesize<>0) and
  1756. (oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL]) then
  1757. oper.SetSize(oper.typesize,true);
  1758. end;
  1759. Procedure tx86intreader.BuildOpCode(instr : tx86instruction);
  1760. var
  1761. PrefixOp,OverrideOp: tasmop;
  1762. operandnum : longint;
  1763. is_far_const:boolean;
  1764. i:byte;
  1765. begin
  1766. PrefixOp:=A_None;
  1767. OverrideOp:=A_None;
  1768. is_far_const:=false;
  1769. { prefix seg opcode / prefix opcode }
  1770. repeat
  1771. if is_prefix(actopcode) then
  1772. with instr do
  1773. begin
  1774. OpOrder:=op_intel;
  1775. PrefixOp:=ActOpcode;
  1776. opcode:=ActOpcode;
  1777. condition:=ActCondition;
  1778. opsize:=ActOpsize;
  1779. ConcatInstruction(curlist);
  1780. consume(AS_OPCODE);
  1781. end
  1782. else
  1783. if is_override(actopcode) then
  1784. with instr do
  1785. begin
  1786. OpOrder:=op_intel;
  1787. OverrideOp:=ActOpcode;
  1788. opcode:=ActOpcode;
  1789. condition:=ActCondition;
  1790. opsize:=ActOpsize;
  1791. ConcatInstruction(curlist);
  1792. consume(AS_OPCODE);
  1793. end
  1794. else
  1795. break;
  1796. { allow for newline after prefix or override }
  1797. while actasmtoken=AS_SEPARATOR do
  1798. consume(AS_SEPARATOR);
  1799. until (actasmtoken<>AS_OPCODE);
  1800. { opcode }
  1801. if (actasmtoken <> AS_OPCODE) then
  1802. begin
  1803. Message(asmr_e_invalid_or_missing_opcode);
  1804. RecoverConsume(false);
  1805. exit;
  1806. end;
  1807. { Fill the instr object with the current state }
  1808. with instr do
  1809. begin
  1810. OpOrder:=op_intel;
  1811. Opcode:=ActOpcode;
  1812. condition:=ActCondition;
  1813. opsize:=ActOpsize;
  1814. { Valid combination of prefix/override and instruction ? }
  1815. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1816. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1817. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1818. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1819. end;
  1820. { We are reading operands, so opcode will be an AS_ID }
  1821. operandnum:=1;
  1822. is_far_const:=false;
  1823. Consume(AS_OPCODE);
  1824. { Zero operand opcode ? }
  1825. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1826. begin
  1827. operandnum:=0;
  1828. exit;
  1829. end;
  1830. { Read Operands }
  1831. repeat
  1832. case actasmtoken of
  1833. { End of asm operands for this opcode }
  1834. AS_END,
  1835. AS_SEPARATOR :
  1836. break;
  1837. { Operand delimiter }
  1838. AS_COMMA :
  1839. begin
  1840. if operandnum > Max_Operands then
  1841. Message(asmr_e_too_many_operands)
  1842. else
  1843. Inc(operandnum);
  1844. Consume(AS_COMMA);
  1845. end;
  1846. {Far constant, i.e. jmp $0000:$11111111.}
  1847. AS_COLON:
  1848. begin
  1849. is_far_const:=true;
  1850. if operandnum>1 then
  1851. message(asmr_e_too_many_operands)
  1852. else
  1853. inc(operandnum);
  1854. consume(AS_COLON);
  1855. end;
  1856. { Type specifier }
  1857. AS_NEAR,
  1858. AS_FAR :
  1859. begin
  1860. if actasmtoken = AS_NEAR then
  1861. begin
  1862. Message(asmr_w_near_ignored);
  1863. instr.opsize:=S_NEAR;
  1864. end
  1865. else
  1866. begin
  1867. Message(asmr_w_far_ignored);
  1868. instr.opsize:=S_FAR;
  1869. end;
  1870. Consume(actasmtoken);
  1871. if actasmtoken=AS_PTR then
  1872. begin
  1873. Consume(AS_PTR);
  1874. instr.Operands[operandnum].InitRef;
  1875. end;
  1876. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  1877. end;
  1878. else
  1879. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  1880. end; { end case }
  1881. until false;
  1882. instr.ops:=operandnum;
  1883. { Check operands }
  1884. for i:=1 to operandnum do
  1885. begin
  1886. if is_far_const and
  1887. (instr.operands[i].opr.typ<>OPR_CONSTANT) then
  1888. message(asmr_e_expr_illegal)
  1889. else
  1890. if instr.operands[i].opr.typ=OPR_NONE then
  1891. Message(asmr_e_syntax_error);
  1892. end;
  1893. { e.g. for "push dword 1", "push word 6" }
  1894. if (instr.ops=1) and
  1895. (instr.operands[1].typesize<>0) then
  1896. instr.operands[1].setsize(instr.operands[1].typesize,false);
  1897. end;
  1898. Procedure tx86intreader.BuildConstant(constsize: byte);
  1899. var
  1900. asmsymtyp : tasmsymtype;
  1901. asmsym,
  1902. expr: string;
  1903. value : aint;
  1904. Begin
  1905. Repeat
  1906. Case actasmtoken of
  1907. AS_STRING:
  1908. Begin
  1909. { DD and DW cases }
  1910. if constsize <> 1 then
  1911. Begin
  1912. if Not PadZero(actasmpattern,constsize) then
  1913. Message(scan_f_string_exceeds_line);
  1914. end;
  1915. expr:=actasmpattern;
  1916. Consume(AS_STRING);
  1917. Case actasmtoken of
  1918. AS_COMMA:
  1919. Consume(AS_COMMA);
  1920. AS_END,
  1921. AS_SEPARATOR: ;
  1922. else
  1923. Message(asmr_e_invalid_string_expression);
  1924. end;
  1925. ConcatString(curlist,expr);
  1926. end;
  1927. AS_PLUS,
  1928. AS_MINUS,
  1929. AS_LPAREN,
  1930. AS_NOT,
  1931. AS_INTNUM,
  1932. AS_ID :
  1933. Begin
  1934. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  1935. if asmsym<>'' then
  1936. begin
  1937. if constsize<>sizeof(pint) then
  1938. Message1(asmr_w_const32bit_for_address,asmsym);
  1939. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value)
  1940. end
  1941. else
  1942. ConcatConstant(curlist,value,constsize);
  1943. end;
  1944. AS_COMMA:
  1945. begin
  1946. Consume(AS_COMMA);
  1947. end;
  1948. AS_ALIGN,
  1949. AS_DB,
  1950. AS_DW,
  1951. AS_DD,
  1952. AS_DQ,
  1953. AS_OPCODE,
  1954. AS_END,
  1955. AS_SEPARATOR:
  1956. break;
  1957. else
  1958. begin
  1959. Message(asmr_e_syn_constant);
  1960. RecoverConsume(false);
  1961. end
  1962. end;
  1963. Until false;
  1964. end;
  1965. function tx86intreader.Assemble: tlinkedlist;
  1966. Var
  1967. hl : tasmlabel;
  1968. instr : Tx86Instruction;
  1969. Begin
  1970. Message1(asmr_d_start_reading,'intel');
  1971. inexpression:=FALSE;
  1972. firsttoken:=TRUE;
  1973. { sets up all opcode and register tables in uppercase
  1974. done in the construtor now
  1975. if not _asmsorted then
  1976. Begin
  1977. SetupTables;
  1978. _asmsorted:=TRUE;
  1979. end;
  1980. }
  1981. curlist:=TAsmList.Create;
  1982. { setup label linked list }
  1983. LocalLabelList:=TLocalLabelList.Create;
  1984. { we might need to know which parameters are passed in registers }
  1985. current_procinfo.generate_parameter_info;
  1986. { start tokenizer }
  1987. c:=current_scanner.asmgetcharstart;
  1988. gettoken;
  1989. { main loop }
  1990. repeat
  1991. case actasmtoken of
  1992. AS_LLABEL:
  1993. Begin
  1994. if CreateLocalLabel(actasmpattern,hl,true) then
  1995. ConcatLabel(curlist,hl);
  1996. Consume(AS_LLABEL);
  1997. end;
  1998. AS_LABEL:
  1999. Begin
  2000. if SearchLabel(upper(actasmpattern),hl,true) then
  2001. ConcatLabel(curlist,hl)
  2002. else
  2003. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2004. Consume(AS_LABEL);
  2005. end;
  2006. AS_DW :
  2007. Begin
  2008. inexpression:=true;
  2009. Consume(AS_DW);
  2010. BuildConstant(2);
  2011. inexpression:=false;
  2012. end;
  2013. AS_DB :
  2014. Begin
  2015. inexpression:=true;
  2016. Consume(AS_DB);
  2017. BuildConstant(1);
  2018. inexpression:=false;
  2019. end;
  2020. AS_DD :
  2021. Begin
  2022. inexpression:=true;
  2023. Consume(AS_DD);
  2024. BuildConstant(4);
  2025. inexpression:=false;
  2026. end;
  2027. {$ifdef cpu64bitaddr}
  2028. AS_DQ:
  2029. Begin
  2030. inexpression:=true;
  2031. Consume(AS_DQ);
  2032. BuildConstant(8);
  2033. inexpression:=false;
  2034. end;
  2035. {$endif cpu64bitaddr}
  2036. AS_ALIGN:
  2037. Begin
  2038. Consume(AS_ALIGN);
  2039. ConcatAlign(curlist,BuildConstExpression);
  2040. if actasmtoken<>AS_SEPARATOR then
  2041. Consume(AS_SEPARATOR);
  2042. end;
  2043. AS_OPCODE :
  2044. Begin
  2045. instr:=Tx86Instruction.Create(Tx86Operand);
  2046. BuildOpcode(instr);
  2047. with instr do
  2048. begin
  2049. { We need AT&T style operands }
  2050. Swapoperands;
  2051. { Must be done with args in ATT order }
  2052. CheckNonCommutativeOpcodes;
  2053. AddReferenceSizes;
  2054. SetInstructionOpsize;
  2055. CheckOperandSizes;
  2056. ConcatInstruction(curlist);
  2057. end;
  2058. instr.Free;
  2059. end;
  2060. AS_SEPARATOR :
  2061. Begin
  2062. Consume(AS_SEPARATOR);
  2063. end;
  2064. AS_END :
  2065. break; { end assembly block }
  2066. else
  2067. Begin
  2068. Message(asmr_e_syntax_error);
  2069. RecoverConsume(false);
  2070. end;
  2071. end; { end case }
  2072. until false;
  2073. { Check LocalLabelList }
  2074. LocalLabelList.CheckEmitted;
  2075. LocalLabelList.Free;
  2076. { Return the list in an asmnode }
  2077. assemble:=curlist;
  2078. Message1(asmr_d_finish_reading,'intel');
  2079. end;
  2080. end.