rax86int.pas 82 KB

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