rax86int.pas 74 KB

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