rax86int.pas 73 KB

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