rax86int.pas 90 KB

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