rax86int.pas 90 KB

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