rax86int.pas 74 KB

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