rax86int.pas 76 KB

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