rax86int.pas 91 KB

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