rax86int.pas 91 KB

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