rax86int.pas 95 KB

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