rax86int.pas 74 KB

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