rax86int.pas 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345
  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. cgbase,
  26. rasm,
  27. rax86;
  28. type
  29. tasmtoken = (
  30. AS_NONE,AS_LABEL,AS_LLABEL,AS_STRING,AS_INTNUM,
  31. AS_COMMA,AS_LBRACKET,AS_RBRACKET,AS_LPAREN,
  32. AS_RPAREN,AS_COLON,AS_DOT,AS_PLUS,AS_MINUS,AS_STAR,
  33. AS_SEPARATOR,AS_ID,AS_REGISTER,AS_OPCODE,AS_SLASH,
  34. AS_LOPMASK,AS_VOPMASK,AS_LOPZEROMASK,AS_VOPZEROMASK,AS_LOPBCST,AS_OPBCST1TO2,AS_OPBCST1TO4,AS_OPBCST1TO8,AS_OPBCST1TO16,AS_LOPSAE,AS_OPSAE,
  35. AS_LOPER,AS_OPRNSAE,AS_OPRDSAE,AS_OPRUSAE,AS_OPRZSAE,
  36. {------------------ Assembler directives --------------------}
  37. AS_ALIGN,AS_DB,AS_DW,AS_DD,AS_DQ,AS_PUBLIC,AS_END,
  38. {------------------ Assembler Operators --------------------}
  39. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_DQWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD,AS_ZWORD,AS_ZMMWORD,AS_NEAR,AS_FAR,
  40. AS_HIGH,AS_LOW,AS_OFFSET,AS_SIZEOF,AS_VMTOFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  41. AS_AND,AS_OR,AS_XOR,AS_WRT,AS___GOTPCREL,AS_TARGET_DIRECTIVE,AS_RNSAE
  42. );
  43. type
  44. { input flags for BuildConstSymbolExpression }
  45. tconstsymbolexpressioninputflag = (
  46. cseif_needofs,
  47. cseif_isref,
  48. cseif_startingminus,
  49. { allows using full reference-like syntax for constsymbol expressions,
  50. for example:
  51. Rec.Str[5] -> Rec.Str+5 }
  52. cseif_referencelike
  53. );
  54. tconstsymbolexpressioninputflags = set of tconstsymbolexpressioninputflag;
  55. { output flags for BuildConstSymbolExpression }
  56. tconstsymbolexpressionoutputflag = (
  57. cseof_isseg,
  58. cseof_is_farproc_entry,
  59. cseof_hasofs
  60. );
  61. tconstsymbolexpressionoutputflags = set of tconstsymbolexpressionoutputflag;
  62. { tx86intreader }
  63. tx86intreader = class(tasmreader)
  64. actasmpattern_origcase : string;
  65. actasmtoken : tasmtoken;
  66. actoperextention: string;
  67. prevasmtoken : tasmtoken;
  68. ActOpsize : topsize;
  69. inexpression : boolean;
  70. constructor create;override;
  71. function is_asmopcode(const s: string):boolean;
  72. function is_asmoperator(const s: string):boolean;
  73. function is_asmdirective(const s: string):boolean;
  74. function is_register(const s:string):boolean;
  75. function is_locallabel(const s:string):boolean;
  76. function Assemble: tlinkedlist;override;
  77. procedure GetToken(check_operand_extention: boolean = false);
  78. function consume(t : tasmtoken; check_operand_extention: boolean = false):boolean;
  79. //procedure ConsumeOperExtention(oper: tx86operand; const aOperExtention: string);
  80. procedure RecoverConsume(allowcomma:boolean);
  81. procedure AddReferences(dest,src : tx86operand);
  82. procedure SetSegmentOverride(oper:tx86operand;seg:tregister);
  83. procedure BuildRecordOffsetSize(const expr: string;out offset:tcgint;out size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean);
  84. procedure BuildConstSymbolExpression(in_flags: tconstsymbolexpressioninputflags;out value:tcgint;out asmsym:string;out asmsymtyp:TAsmsymtype;out size:tcgint;out out_flags:tconstsymbolexpressionoutputflags);
  85. function BuildConstExpression:aint;
  86. function BuildRefConstExpression(out size:tcgint;startingminus:boolean=false):aint;
  87. procedure BuildReference(oper : tx86operand);
  88. procedure BuildOperand(oper: tx86operand;istypecast:boolean);
  89. procedure BuildConstantOperand(oper: tx86operand);
  90. procedure BuildOpCode(instr : tx86instruction);
  91. procedure BuildConstant(constsize: byte);
  92. procedure consume_voperand_ext(aop: tx86operand; aConsumeVOpExt: boolean = true);
  93. function is_targetdirective(const s: string): boolean;virtual;
  94. procedure HandleTargetDirective;virtual;
  95. end;
  96. implementation
  97. uses
  98. { common }
  99. cutils,
  100. { global }
  101. globals,verbose,
  102. systems,
  103. { aasm }
  104. aasmdata,aasmcpu,
  105. {$ifdef i8086}
  106. aasmtai,
  107. {$endif i8086}
  108. { symtable }
  109. symconst,symbase,symtype,symsym,symutil,symdef,
  110. {$ifdef i8086}
  111. symcpu,
  112. {$endif i8086}
  113. { parser }
  114. scanner,pbase,
  115. { register allocator }
  116. rautils,itx86int,
  117. { codegen }
  118. procinfo,paramgr
  119. ;
  120. type
  121. tasmkeyword = string[9];
  122. const
  123. { These tokens should be modified accordingly to the modifications }
  124. { in the different enumerations. }
  125. firstdirective = AS_ALIGN;
  126. lastdirective = AS_END;
  127. firstoperator = AS_BYTE;
  128. lastoperator = AS___GOTPCREL;
  129. OPEXT_STARTASMTOKEN: set of tasmtoken = [AS_LOPMASK,AS_LOPZEROMASK,AS_LOPBCST,AS_LOPSAE,AS_LOPER];
  130. _asmdirectives : array[firstdirective..lastdirective] of tasmkeyword =
  131. ('ALIGN','DB','DW','DD','DQ','PUBLIC','END');
  132. { problems with shl,shr,not,and,or and xor, they are }
  133. { context sensitive. }
  134. _asmoperators : array[firstoperator..lastoperator] of tasmkeyword = (
  135. 'BYTE','WORD','DWORD','QWORD','TBYTE','DQWORD','OWORD','XMMWORD','YWORD','YMMWORD','ZWORD','ZMMWORD','NEAR','FAR','HIGH',
  136. 'LOW','OFFSET','SIZEOF','VMTOFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  137. 'OR','XOR','WRT','GOTPCREL');
  138. token2str : array[tasmtoken] of string[10] = (
  139. '','Label','LLabel','String','Integer',
  140. ',','[',']','(',
  141. ')',':','.','+','-','*',
  142. ';','identifier','register','opcode','/',
  143. '','','','','','','','','','','',
  144. '','','','','',
  145. '','','','','','','END',
  146. '','','','','','','','','','','','','','',
  147. '','','','sizeof','vmtoffset','','type','ptr','mod','shl','shr','not',
  148. 'and','or','xor','wrt','..gotpcrel','','{RN-SAE}'
  149. );
  150. constructor tx86intreader.create;
  151. var
  152. i : tasmop;
  153. Begin
  154. inherited create;
  155. iasmops:=TFPHashList.create;
  156. for i:=firstop to lastop do
  157. iasmops.Add(upper(std_op2str[i]),Pointer(PtrInt(i)));
  158. actoperextention := '';
  159. end;
  160. {---------------------------------------------------------------------}
  161. { Routines for the tokenizing }
  162. {---------------------------------------------------------------------}
  163. function tx86intreader.is_asmopcode(const s: string):boolean;
  164. var
  165. cond : string[4];
  166. cnd : tasmcond;
  167. j: longint;
  168. Begin
  169. is_asmopcode:=FALSE;
  170. actopcode:=A_None;
  171. actcondition:=C_None;
  172. actopsize:=S_NO;
  173. { Search opcodes }
  174. actopcode:=tasmop(PtrUInt(iasmops.Find(s)));
  175. if actopcode<>A_NONE then
  176. begin
  177. actasmtoken:=AS_OPCODE;
  178. result:=TRUE;
  179. exit;
  180. end;
  181. { not found yet, check condition opcodes }
  182. j:=0;
  183. while (j<CondAsmOps) do
  184. begin
  185. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  186. begin
  187. cond:=Copy(s,Length(CondAsmOpStr[j])+1,255);
  188. if cond<>'' then
  189. begin
  190. for cnd:=low(TasmCond) to high(TasmCond) do
  191. if Cond=Upper(cond2str[cnd]) then
  192. begin
  193. actopcode:=CondASmOp[j];
  194. actcondition:=cnd;
  195. is_asmopcode:=TRUE;
  196. actasmtoken:=AS_OPCODE;
  197. exit
  198. end;
  199. end;
  200. end;
  201. inc(j);
  202. end;
  203. end;
  204. function tx86intreader.is_asmoperator(const s: string):boolean;
  205. var
  206. i : tasmtoken;
  207. Begin
  208. for i:=firstoperator to lastoperator do
  209. if s=_asmoperators[i] then
  210. begin
  211. actasmtoken:=i;
  212. is_asmoperator:=true;
  213. exit;
  214. end;
  215. is_asmoperator:=false;
  216. end;
  217. Function tx86intreader.is_asmdirective(const s: string):boolean;
  218. var
  219. i : tasmtoken;
  220. Begin
  221. for i:=firstdirective to lastdirective do
  222. if s=_asmdirectives[i] then
  223. begin
  224. actasmtoken:=i;
  225. is_asmdirective:=true;
  226. exit;
  227. end;
  228. is_asmdirective:=false;
  229. end;
  230. function tx86intreader.is_register(const s:string):boolean;
  231. begin
  232. is_register:=false;
  233. actasmregister:=masm_regnum_search(lower(s));
  234. { don't acceps "flags" as register name in an instruction }
  235. if (getsupreg(actasmregister)=RS_DEFAULTFLAGS) and (getregtype(actasmregister)=getregtype(NR_DEFAULTFLAGS)) then
  236. actasmregister:=NR_NO;
  237. if actasmregister<>NR_NO then
  238. begin
  239. is_register:=true;
  240. actasmtoken:=AS_REGISTER;
  241. end;
  242. end;
  243. function tx86intreader.is_locallabel(const s:string):boolean;
  244. begin
  245. is_locallabel:=(length(s)>1) and (s[1]='@');
  246. end;
  247. function tx86intreader.is_targetdirective(const s: string): boolean;
  248. begin
  249. result:=false;
  250. end;
  251. procedure tx86intreader.handletargetdirective;
  252. begin
  253. end;
  254. Procedure tx86intreader.GetToken(check_operand_extention: boolean);
  255. var
  256. len : longint;
  257. forcelabel : boolean;
  258. srsym : tsym;
  259. srsymtable : TSymtable;
  260. scomment: string;
  261. schar: char;
  262. old_commentstyle: tcommentstyle;
  263. begin
  264. actoperextention := '';
  265. c:=scanner.c;
  266. { save old token and reset new token }
  267. prevasmtoken:=actasmtoken;
  268. actasmtoken:=AS_NONE;
  269. { reset }
  270. forcelabel:=FALSE;
  271. actasmpattern:='';
  272. { while space and tab , continue scan... }
  273. while (c in [' ',#9]) do
  274. c:=current_scanner.asmgetchar;
  275. { get token pos }
  276. if not (c in [#10,#13,'{',';','(','/']) then
  277. current_scanner.gettokenpos;
  278. { Local Label, Label, Directive, Prefix or Opcode }
  279. if firsttoken and not (c in [#10,#13,'{',';','(','/']) then
  280. begin
  281. firsttoken:=FALSE;
  282. len:=0;
  283. { directive check }
  284. if c = '.' then
  285. begin
  286. actasmpattern:='.';
  287. c:=current_scanner.asmgetchar;
  288. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  289. begin
  290. actasmpattern:=actasmpattern+c;
  291. c:=current_scanner.asmgetchar;
  292. end;
  293. { directives are case sensitive!! }
  294. if is_asmdirective(actasmpattern) then
  295. exit;
  296. if is_targetdirective(actasmpattern) then
  297. begin
  298. actasmtoken:=AS_TARGET_DIRECTIVE;
  299. exit;
  300. end;
  301. Message1(asmr_e_not_directive_or_local_symbol,actasmpattern);
  302. exit;
  303. end;
  304. while (c in ['A'..'Z','a'..'z','0'..'9','_','@']) or
  305. { TP7 also allows $&? characters in local labels }
  306. (forcelabel and (c in ['$','&','?'])) do
  307. begin
  308. { if there is an at_sign, then this must absolutely be a label }
  309. if c = '@' then
  310. forcelabel:=TRUE;
  311. inc(len);
  312. actasmpattern[len]:=c;
  313. c:=current_scanner.asmgetchar;
  314. end;
  315. actasmpattern[0]:=chr(len);
  316. actasmpattern_origcase:=actasmpattern;
  317. uppervar(actasmpattern);
  318. { allow spaces }
  319. while (c in [' ',#9]) do
  320. c:=current_scanner.asmgetchar;
  321. { label ? }
  322. if c = ':' then
  323. begin
  324. if actasmpattern[1]='@' then
  325. actasmtoken:=AS_LLABEL
  326. else
  327. actasmtoken:=AS_LABEL;
  328. { let us point to the next character }
  329. c:=current_scanner.asmgetchar;
  330. firsttoken:=true;
  331. exit;
  332. end;
  333. { Are we trying to create an identifier with }
  334. { an at-sign...? }
  335. if forcelabel then
  336. Message(asmr_e_none_label_contain_at);
  337. { opcode ? }
  338. If is_asmopcode(actasmpattern) then
  339. Begin
  340. { check if we are in an expression }
  341. { then continue with asm directives }
  342. if not inexpression then
  343. exit;
  344. end;
  345. if is_asmdirective(actasmpattern) then
  346. exit;
  347. message1(asmr_e_unknown_opcode,actasmpattern);
  348. actasmtoken:=AS_NONE;
  349. exit;
  350. end
  351. else { else firsttoken }
  352. begin
  353. case c of
  354. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  355. { - @Result, @Code or @Data special variables. }
  356. begin
  357. actasmpattern:=c;
  358. c:=current_scanner.asmgetchar;
  359. while c in ['A'..'Z','a'..'z','0'..'9','_','@','$','&','?'] do
  360. begin
  361. actasmpattern:=actasmpattern + c;
  362. c:=current_scanner.asmgetchar;
  363. end;
  364. actasmpattern_origcase:=actasmpattern;
  365. uppervar(actasmpattern);
  366. actasmtoken:=AS_ID;
  367. exit;
  368. end;
  369. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  370. begin
  371. actasmpattern:=c;
  372. c:=current_scanner.asmgetchar;
  373. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  374. begin
  375. actasmpattern:=actasmpattern + c;
  376. c:=current_scanner.asmgetchar;
  377. end;
  378. actasmpattern_origcase:=actasmpattern;
  379. uppervar(actasmpattern);
  380. { after prefix (or segment override) we allow also a new opcode }
  381. If (is_prefix(actopcode) or is_override(actopcode)) and is_asmopcode(actasmpattern) then
  382. Begin
  383. { if we are not in a constant }
  384. { expression than this is an }
  385. { opcode. }
  386. if not inexpression then
  387. exit;
  388. end;
  389. { support st(X) for fpu registers }
  390. if (actasmpattern = 'ST') and (c='(') then
  391. Begin
  392. actasmpattern:=actasmpattern+c;
  393. c:=current_scanner.asmgetchar;
  394. { allow spaces }
  395. while (c in [' ',#9]) do
  396. c:=current_scanner.asmgetchar;
  397. if c in ['0'..'7'] then
  398. actasmpattern:=actasmpattern + c
  399. else
  400. Message(asmr_e_invalid_fpu_register);
  401. c:=current_scanner.asmgetchar;
  402. { allow spaces }
  403. while (c in [' ',#9]) do
  404. c:=current_scanner.asmgetchar;
  405. if c <> ')' then
  406. Message(asmr_e_invalid_fpu_register)
  407. else
  408. Begin
  409. actasmpattern:=actasmpattern + c;
  410. c:=current_scanner.asmgetchar;
  411. end;
  412. end;
  413. //if prevasmtoken in [AS_LOPMASK,AS_LOPZEROMASK,AS_LOPBCST,AS_LOPSAE,AS_LOPER] then
  414. if prevasmtoken in OPEXT_STARTASMTOKEN then
  415. begin
  416. if (prevasmtoken = AS_LOPER) and (c = '-') then
  417. begin
  418. actasmpattern := actasmpattern + c;
  419. c:=current_scanner.asmgetchar;
  420. while c in ['A'..'Z','a'..'z'] do
  421. begin
  422. actasmpattern:=actasmpattern + c;
  423. c:=current_scanner.asmgetchar;
  424. end;
  425. end;
  426. { allow spaces }
  427. while (c in [' ',#9]) do
  428. c:=current_scanner.asmgetchar;
  429. if c = '}' then
  430. begin
  431. current_scanner.readchar;
  432. case prevasmtoken of
  433. AS_LOPMASK: if (length(actasmpattern) = 2) and
  434. (actasmpattern[2] in ['1'..'7']) then
  435. begin
  436. actasmtoken := AS_VOPMASK;
  437. end;
  438. AS_LOPZEROMASK:
  439. if (actasmpattern = 'z') or
  440. (actasmpattern = 'Z') then
  441. begin
  442. actasmtoken := AS_VOPZEROMASK;
  443. end;
  444. AS_LOPBCST:
  445. begin
  446. actasmpattern_origcase:=actasmpattern;
  447. uppervar(actasmpattern);
  448. if (actasmpattern = '1TO2') then actasmtoken := AS_OPBCST1TO2
  449. else if (actasmpattern = '1TO4') then actasmtoken := AS_OPBCST1TO4
  450. else if (actasmpattern = '1TO8') then actasmtoken := AS_OPBCST1TO8
  451. else if (actasmpattern = '1TO16') then actasmtoken := AS_OPBCST1TO16
  452. else actasmpattern := actasmpattern_origcase;
  453. end;
  454. AS_LOPSAE:
  455. begin
  456. actasmpattern_origcase:=actasmpattern;
  457. uppervar(actasmpattern);
  458. if (actasmpattern = 'SAE') then actasmtoken := AS_OPSAE
  459. else actasmpattern := actasmpattern_origcase;
  460. end;
  461. AS_LOPER:
  462. begin
  463. actasmpattern_origcase:=actasmpattern;
  464. uppervar(actasmpattern);
  465. if (actasmpattern = 'RD-SAE') then actasmtoken := AS_OPRDSAE
  466. else if (actasmpattern = 'RN-SAE') then actasmtoken := AS_OPRNSAE
  467. else if (actasmpattern = 'RU-SAE') then actasmtoken := AS_OPRUSAE
  468. else if (actasmpattern = 'RZ-SAE') then actasmtoken := AS_OPRZSAE
  469. else actasmpattern := actasmpattern_origcase;
  470. end
  471. else ; // is completely comment =>> nothing todo
  472. end;
  473. exit;
  474. end
  475. else
  476. begin
  477. if c = '{' then current_scanner.inc_comment_level;
  478. current_scanner.skipcomment(false); // is comment
  479. actasmpattern := '';
  480. actasmtoken := AS_NONE;
  481. exit;
  482. end;
  483. end;
  484. if is_asmdirective(actasmpattern) then
  485. exit;
  486. if is_asmoperator(actasmpattern) then
  487. exit;
  488. if is_register(actasmpattern) then
  489. exit;
  490. { allow spaces }
  491. while (c in [' ',#9]) do
  492. c:=current_scanner.asmgetchar;
  493. { if next is a '.' and this is a unitsym then we also need to
  494. parse the identifier }
  495. if (c='.') then
  496. begin
  497. asmsearchsym(actasmpattern,srsym,srsymtable);
  498. if assigned(srsym) and
  499. (srsym.typ=unitsym) and
  500. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  501. srsym.owner.iscurrentunit then
  502. begin
  503. { Add . to create System.Identifier }
  504. actasmpattern:=actasmpattern+c;
  505. c:=current_scanner.asmgetchar;
  506. { Delphi allows System.@Halt, just ignore the @ }
  507. if c='@' then
  508. c:=current_scanner.asmgetchar;
  509. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  510. begin
  511. actasmpattern:=actasmpattern + upcase(c);
  512. c:=current_scanner.asmgetchar;
  513. end;
  514. end;
  515. end;
  516. actasmtoken:=AS_ID;
  517. exit;
  518. end;
  519. '''' : { string or character }
  520. begin
  521. actasmpattern:='';
  522. repeat
  523. if c = '''' then
  524. begin
  525. c:=current_scanner.asmgetchar;
  526. if c in [#10,#13] then
  527. begin
  528. Message(scan_f_string_exceeds_line);
  529. break;
  530. end;
  531. repeat
  532. if c='''' then
  533. begin
  534. c:=current_scanner.asmgetchar;
  535. if c='''' then
  536. begin
  537. actasmpattern:=actasmpattern+'''';
  538. c:=current_scanner.asmgetchar;
  539. if c in [#10,#13] then
  540. begin
  541. Message(scan_f_string_exceeds_line);
  542. break;
  543. end;
  544. end
  545. else
  546. break;
  547. end
  548. else
  549. begin
  550. actasmpattern:=actasmpattern+c;
  551. c:=current_scanner.asmgetchar;
  552. if c in [#10,#13] then
  553. begin
  554. Message(scan_f_string_exceeds_line);
  555. break
  556. end;
  557. end;
  558. until false; { end repeat }
  559. end
  560. else
  561. break; { end if }
  562. until false;
  563. actasmtoken:=AS_STRING;
  564. exit;
  565. end;
  566. '"' : { string or character }
  567. begin
  568. actasmpattern:='';
  569. repeat
  570. if c = '"' then
  571. begin
  572. c:=current_scanner.asmgetchar;
  573. if c in [#10,#13] then
  574. begin
  575. Message(scan_f_string_exceeds_line);
  576. break;
  577. end;
  578. repeat
  579. if c='"' then
  580. begin
  581. c:=current_scanner.asmgetchar;
  582. if c='"' then
  583. begin
  584. actasmpattern:=actasmpattern+'"';
  585. c:=current_scanner.asmgetchar;
  586. if c in [#10,#13] then
  587. begin
  588. Message(scan_f_string_exceeds_line);
  589. break;
  590. end;
  591. end
  592. else
  593. break;
  594. end
  595. else
  596. begin
  597. actasmpattern:=actasmpattern+c;
  598. c:=current_scanner.asmgetchar;
  599. if c in [#10,#13] then
  600. begin
  601. Message(scan_f_string_exceeds_line);
  602. break
  603. end;
  604. end;
  605. until false; { end repeat }
  606. end
  607. else
  608. break; { end if }
  609. until false;
  610. actasmtoken:=AS_STRING;
  611. exit;
  612. end;
  613. '$' :
  614. begin
  615. c:=current_scanner.asmgetchar;
  616. while c in ['0'..'9','A'..'F','a'..'f'] do
  617. begin
  618. actasmpattern:=actasmpattern + c;
  619. c:=current_scanner.asmgetchar;
  620. end;
  621. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  622. actasmtoken:=AS_INTNUM;
  623. exit;
  624. end;
  625. '&' : { identifier }
  626. begin
  627. actasmpattern:='';
  628. c:=current_scanner.asmgetchar;
  629. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  630. begin
  631. actasmpattern:=actasmpattern + c;
  632. c:=current_scanner.asmgetchar;
  633. end;
  634. actasmpattern_origcase:=actasmpattern;
  635. uppervar(actasmpattern);
  636. actasmtoken:=AS_ID;
  637. exit;
  638. end;
  639. ',' :
  640. begin
  641. actasmtoken:=AS_COMMA;
  642. c:=current_scanner.asmgetchar;
  643. exit;
  644. end;
  645. '[' :
  646. begin
  647. actasmtoken:=AS_LBRACKET;
  648. c:=current_scanner.asmgetchar;
  649. exit;
  650. end;
  651. ']' :
  652. begin
  653. actasmtoken:=AS_RBRACKET;
  654. c:=current_scanner.asmgetchar;
  655. exit;
  656. end;
  657. '(' :
  658. begin
  659. c:=current_scanner.asmgetchar;
  660. if c='*' then
  661. begin
  662. current_scanner.skipoldtpcomment(true);
  663. GetToken;
  664. end
  665. else
  666. actasmtoken:=AS_LPAREN;
  667. exit;
  668. end;
  669. ')' :
  670. begin
  671. actasmtoken:=AS_RPAREN;
  672. c:=current_scanner.asmgetchar;
  673. exit;
  674. end;
  675. ':' :
  676. begin
  677. actasmtoken:=AS_COLON;
  678. c:=current_scanner.asmgetchar;
  679. exit;
  680. end;
  681. '.' :
  682. begin
  683. c:=current_scanner.asmgetchar;
  684. {$ifdef x86_64}
  685. if c='.' then
  686. begin
  687. actasmpattern:='..';
  688. c:=current_scanner.asmgetchar;
  689. repeat
  690. actasmpattern:=actasmpattern+c;
  691. c:=current_scanner.asmgetchar;
  692. until not(c in ['A'..'Z','a'..'z','0'..'9','_']);
  693. if upper(actasmpattern)<>'..GOTPCREL' then
  694. begin
  695. actasmtoken:=AS_ID;
  696. consume(AS___GOTPCREL);
  697. end;
  698. actasmtoken:=AS___GOTPCREL
  699. end
  700. else
  701. {$endif x86_64}
  702. actasmtoken:=AS_DOT;
  703. exit;
  704. end;
  705. '+' :
  706. begin
  707. actasmtoken:=AS_PLUS;
  708. c:=current_scanner.asmgetchar;
  709. exit;
  710. end;
  711. '-' :
  712. begin
  713. actasmtoken:=AS_MINUS;
  714. c:=current_scanner.asmgetchar;
  715. exit;
  716. end;
  717. '*' :
  718. begin
  719. actasmtoken:=AS_STAR;
  720. c:=current_scanner.asmgetchar;
  721. exit;
  722. end;
  723. '/' :
  724. begin
  725. c:=current_scanner.asmgetchar;
  726. if c='/' then
  727. begin
  728. current_scanner.skipdelphicomment;
  729. GetToken;
  730. end
  731. else
  732. actasmtoken:=AS_SLASH;
  733. exit;
  734. end;
  735. '0'..'9':
  736. begin
  737. if prevasmtoken = AS_LOPBCST then
  738. begin
  739. actasmpattern:=c;
  740. c:=current_scanner.asmgetchar;
  741. { Get the possible characters }
  742. while c in ['1','2','4','6','8','t','T','o','O'] do
  743. begin
  744. actasmpattern:=actasmpattern + c;
  745. c:=current_scanner.asmgetchar;
  746. end;
  747. while (c in [' ',#9]) do
  748. c:=current_scanner.asmgetchar;
  749. if c = '}' then
  750. begin
  751. actasmpattern_origcase:=actasmpattern;
  752. uppervar(actasmpattern);
  753. if (actasmpattern = '1TO2') then actasmtoken := AS_OPBCST1TO2
  754. else if (actasmpattern = '1TO4') then actasmtoken := AS_OPBCST1TO4
  755. else if (actasmpattern = '1TO8') then actasmtoken := AS_OPBCST1TO8
  756. else if (actasmpattern = '1TO16') then actasmtoken := AS_OPBCST1TO16
  757. else actasmpattern := actasmpattern_origcase;
  758. c:=current_scanner.asmgetchar;
  759. end
  760. else
  761. begin
  762. if c = '{' then current_scanner.inc_comment_level;
  763. current_scanner.skipcomment(false); // is comment
  764. end;
  765. actasmpattern := '';
  766. exit;
  767. end
  768. else
  769. begin
  770. actasmpattern:=c;
  771. c:=current_scanner.asmgetchar;
  772. { Get the possible characters }
  773. while c in ['0'..'9','A'..'F','a'..'f'] do
  774. begin
  775. actasmpattern:=actasmpattern + c;
  776. c:=current_scanner.asmgetchar;
  777. end;
  778. { Get ending character }
  779. actasmpattern_origcase:=actasmpattern;
  780. uppervar(actasmpattern);
  781. c:=upcase(c);
  782. { possibly a binary number. }
  783. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  784. Begin
  785. { Delete the last binary specifier }
  786. delete(actasmpattern,length(actasmpattern),1);
  787. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  788. actasmtoken:=AS_INTNUM;
  789. exit;
  790. end
  791. else
  792. Begin
  793. case c of
  794. 'O' :
  795. Begin
  796. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  797. actasmtoken:=AS_INTNUM;
  798. c:=current_scanner.asmgetchar;
  799. exit;
  800. end;
  801. 'H' :
  802. Begin
  803. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  804. actasmtoken:=AS_INTNUM;
  805. c:=current_scanner.asmgetchar;
  806. exit;
  807. end;
  808. else { must be an integer number }
  809. begin
  810. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  811. actasmtoken:=AS_INTNUM;
  812. exit;
  813. end;
  814. end;
  815. end;
  816. end;
  817. end;
  818. #13,#10:
  819. begin
  820. current_scanner.linebreak;
  821. c:=current_scanner.asmgetchar;
  822. firsttoken:=TRUE;
  823. actasmtoken:=AS_SEPARATOR;
  824. exit;
  825. end;
  826. ';':
  827. begin
  828. c:=current_scanner.asmgetchar;
  829. firsttoken:=TRUE;
  830. actasmtoken:=AS_SEPARATOR;
  831. exit;
  832. end;
  833. '{':
  834. begin
  835. if not(check_operand_extention) then current_scanner.skipcomment(true)
  836. else // exists operand extention e.g. AVX512 {k1..k7} or {z} or {1to8}
  837. begin
  838. case current_scanner.asmgetchar of
  839. '{': begin
  840. current_scanner.inc_comment_level;
  841. current_scanner.skipcomment(true);
  842. end;
  843. '}': ; // local comment closed
  844. 'k',
  845. 'K': begin
  846. actasmtoken := AS_LOPMASK;
  847. exit;
  848. end;
  849. 'z',
  850. 'Z': begin
  851. actasmtoken := AS_LOPZEROMASK;
  852. exit;
  853. end;
  854. '1': begin
  855. actasmtoken := AS_LOPBCST;
  856. exit;
  857. end;
  858. 's',
  859. 'S': begin
  860. actasmtoken := AS_LOPSAE;
  861. exit;
  862. end;
  863. 'r',
  864. 'R': begin
  865. actasmtoken := AS_LOPER;
  866. exit;
  867. end;
  868. else begin
  869. current_scanner.skipcomment(false);
  870. end;
  871. end;
  872. end;
  873. GetToken(check_operand_extention);
  874. end;
  875. else
  876. current_scanner.illegal_char(c);
  877. end;
  878. end;
  879. end;
  880. function tx86intreader.consume(t : tasmtoken; check_operand_extention: boolean):boolean;
  881. begin
  882. Consume:=true;
  883. if t<>actasmtoken then
  884. begin
  885. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  886. Consume:=false;
  887. end;
  888. repeat
  889. gettoken(check_operand_extention);
  890. until actasmtoken<>AS_NONE;
  891. end;
  892. //procedure tx86intreader.ConsumeOperExtention(oper: tx86operand; const aOperExtention: string);
  893. //begin
  894. // //if oper.reg
  895. //end;
  896. procedure tx86intreader.consume_voperand_ext(aop: tx86operand; aConsumeVOpExt: boolean);
  897. var
  898. kreg: tregister;
  899. begin
  900. Consume(actasmtoken, true);
  901. if actasmtoken in [AS_VOPMASK, AS_VOPZEROMASK, AS_OPBCST1TO2, AS_OPBCST1TO4, AS_OPBCST1TO8, AS_OPBCST1TO16,
  902. AS_OPSAE,AS_OPRNSAE,AS_OPRDSAE,AS_OPRUSAE,AS_OPRZSAE] then
  903. begin
  904. case actasmtoken of
  905. AS_VOPMASK: begin
  906. kreg := masm_regnum_search(lower(actasmpattern));
  907. if (kreg >= NR_K1) and
  908. (kreg <= NR_K7) then
  909. begin
  910. aop.vopext := aop.vopext or (tregisterrec(kreg).supreg and $07);
  911. aop.vopext := aop.vopext or OTVE_VECTOR_WRITEMASK;
  912. end;
  913. end;
  914. AS_VOPZEROMASK: aop.vopext := aop.vopext or OTVE_VECTOR_ZERO;
  915. AS_OPBCST1TO2: begin
  916. aop.vopext := aop.vopext or OTVE_VECTOR_BCST or OTVE_VECTOR_BCST2;
  917. aop.vbcst := 2;
  918. end;
  919. AS_OPBCST1TO4: begin
  920. aop.vopext := aop.vopext or OTVE_VECTOR_BCST or OTVE_VECTOR_BCST4;
  921. aop.vbcst := 4;
  922. end;
  923. AS_OPBCST1TO8: begin
  924. aop.vopext := aop.vopext or OTVE_VECTOR_BCST or OTVE_VECTOR_BCST8;
  925. aop.vbcst := 8;
  926. end;
  927. AS_OPBCST1TO16: begin
  928. aop.vopext := aop.vopext or OTVE_VECTOR_BCST or OTVE_VECTOR_BCST16;
  929. aop.vbcst := 16;
  930. end;
  931. AS_OPSAE: aop.vopext := aop.vopext or OTVE_VECTOR_SAE;
  932. AS_OPRNSAE: aop.vopext := aop.vopext or OTVE_VECTOR_RNSAE;
  933. AS_OPRDSAE: aop.vopext := aop.vopext or OTVE_VECTOR_RDSAE;
  934. AS_OPRUSAE: aop.vopext := aop.vopext or OTVE_VECTOR_RUSAE;
  935. AS_OPRZSAE: aop.vopext := aop.vopext or OTVE_VECTOR_RZSAE;
  936. else
  937. Internalerror(2019081009);
  938. end;
  939. if aConsumeVOpExt then
  940. Consume(actasmtoken, true);
  941. end;
  942. end;
  943. procedure tx86intreader.RecoverConsume(allowcomma:boolean);
  944. begin
  945. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  946. begin
  947. if allowcomma and (actasmtoken=AS_COMMA) then
  948. break;
  949. Consume(actasmtoken);
  950. end;
  951. end;
  952. {*****************************************************************************
  953. Parsing Helpers
  954. *****************************************************************************}
  955. { Adds two references (dest:=dest+src) }
  956. procedure tx86intreader.AddReferences(dest,src : tx86operand);
  957. procedure AddRegister(reg:tregister;scalefactor:byte);
  958. begin
  959. if reg=NR_NO then
  960. exit;
  961. if (dest.opr.ref.base=NR_NO) and (scalefactor=1) then
  962. begin
  963. dest.opr.ref.base:=reg;
  964. exit;
  965. end;
  966. if dest.opr.ref.index=NR_NO then
  967. begin
  968. dest.opr.ref.index:=reg;
  969. dest.opr.ref.scalefactor:=scalefactor;
  970. exit;
  971. end;
  972. if dest.opr.ref.index=reg then
  973. begin
  974. Inc(dest.opr.ref.scalefactor,scalefactor);
  975. exit;
  976. end;
  977. Message(asmr_e_multiple_index);
  978. end;
  979. var
  980. tmplocal: TOprRec;
  981. segreg: TRegister;
  982. begin
  983. case dest.opr.typ of
  984. OPR_REFERENCE:
  985. begin
  986. case src.opr.typ of
  987. OPR_REFERENCE:
  988. begin
  989. AddRegister(src.opr.ref.base,1);
  990. AddRegister(src.opr.ref.index,src.opr.ref.scalefactor);
  991. if src.opr.ref.segment<>NR_NO then
  992. SetSegmentOverride(dest,src.opr.ref.segment);
  993. Inc(dest.opr.ref.offset,src.opr.ref.offset);
  994. Inc(dest.opr.constoffset,src.opr.constoffset);
  995. dest.haslabelref:=dest.haslabelref or src.haslabelref;
  996. dest.hasproc:=dest.hasproc or src.hasproc;
  997. dest.hasvar:=dest.hasvar or src.hasvar;
  998. if assigned(src.opr.ref.symbol) then
  999. begin
  1000. if assigned(dest.opr.ref.symbol) then
  1001. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1002. dest.opr.ref.symbol:=src.opr.ref.symbol;
  1003. end;
  1004. if assigned(src.opr.ref.relsymbol) then
  1005. begin
  1006. if assigned(dest.opr.ref.relsymbol) then
  1007. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1008. dest.opr.ref.relsymbol:=src.opr.ref.relsymbol;
  1009. end;
  1010. if dest.opr.ref.refaddr=addr_no then
  1011. dest.opr.ref.refaddr:=src.opr.ref.refaddr;
  1012. end;
  1013. OPR_LOCAL:
  1014. begin
  1015. tmplocal:=src.opr;
  1016. if dest.opr.ref.base<>NR_NO then
  1017. begin
  1018. if tmplocal.localindexreg=NR_NO then
  1019. begin
  1020. tmplocal.localindexreg:=dest.opr.ref.base;
  1021. tmplocal.localscale:=0;
  1022. end
  1023. else if tmplocal.localindexreg=dest.opr.ref.base then
  1024. tmplocal.localscale:=Min(tmplocal.localscale,1)+1
  1025. else
  1026. Message(asmr_e_multiple_index);
  1027. end;
  1028. if dest.opr.ref.index<>NR_NO then
  1029. begin
  1030. if tmplocal.localindexreg=NR_NO then
  1031. begin
  1032. tmplocal.localindexreg:=dest.opr.ref.index;
  1033. tmplocal.localscale:=dest.opr.ref.scalefactor;
  1034. end
  1035. else if tmplocal.localindexreg=dest.opr.ref.index then
  1036. tmplocal.localscale:=Min(tmplocal.localscale,1)+Min(dest.opr.ref.scalefactor,1)
  1037. else
  1038. Message(asmr_e_multiple_index);
  1039. end;
  1040. Inc(tmplocal.localconstoffset,dest.opr.constoffset);
  1041. Inc(tmplocal.localsymofs,dest.opr.ref.offset);
  1042. segreg:=dest.opr.ref.segment;
  1043. dest.opr:=tmplocal;
  1044. if segreg<>NR_NO then
  1045. SetSegmentOverride(dest,segreg);
  1046. end;
  1047. else
  1048. internalerror(2018030701);
  1049. end;
  1050. end;
  1051. OPR_LOCAL:
  1052. begin
  1053. case src.opr.typ of
  1054. OPR_REFERENCE:
  1055. begin
  1056. if src.opr.ref.base<>NR_NO then
  1057. begin
  1058. if dest.opr.localindexreg=NR_NO then
  1059. begin
  1060. dest.opr.localindexreg:=src.opr.ref.base;
  1061. dest.opr.localscale:=0;
  1062. end
  1063. else if dest.opr.localindexreg=src.opr.ref.base then
  1064. dest.opr.localscale:=Min(dest.opr.localscale,1)+1
  1065. else
  1066. Message(asmr_e_multiple_index);
  1067. end;
  1068. if src.opr.ref.index<>NR_NO then
  1069. begin
  1070. if dest.opr.localindexreg=NR_NO then
  1071. begin
  1072. dest.opr.localindexreg:=src.opr.ref.index;
  1073. dest.opr.localscale:=src.opr.ref.scalefactor;
  1074. end
  1075. else if dest.opr.localindexreg=src.opr.ref.index then
  1076. dest.opr.localscale:=Min(dest.opr.localscale,1)+Min(src.opr.ref.scalefactor,1)
  1077. else
  1078. Message(asmr_e_multiple_index);
  1079. end;
  1080. Inc(dest.opr.localconstoffset,src.opr.constoffset);
  1081. Inc(dest.opr.localsymofs,src.opr.ref.offset);
  1082. if src.opr.ref.segment<>NR_NO then
  1083. SetSegmentOverride(dest,src.opr.ref.segment);
  1084. end;
  1085. OPR_LOCAL:
  1086. Message(asmr_e_no_local_or_para_allowed);
  1087. else
  1088. internalerror(2018030703);
  1089. end;
  1090. end;
  1091. else
  1092. internalerror(2018030702);
  1093. end;
  1094. end;
  1095. procedure tx86intreader.SetSegmentOverride(oper:tx86operand;seg:tregister);
  1096. begin
  1097. if not is_segment_reg(seg) then
  1098. Message(asmr_e_invalid_seg_override);
  1099. {$ifdef x86_64}
  1100. if (seg=NR_CS) or (seg=NR_DS) or (seg=NR_SS) or (seg=NR_ES) then
  1101. Message1(asmr_w_segment_override_ignored_in_64bit_mode,masm_regname(seg));
  1102. {$endif x86_64}
  1103. case oper.opr.typ of
  1104. OPR_REFERENCE:
  1105. begin
  1106. if oper.opr.ref.segment<>NR_NO then
  1107. begin
  1108. if m_tp7 in current_settings.modeswitches then
  1109. Message(asmr_w_multiple_segment_overrides)
  1110. else
  1111. Message(asmr_e_multiple_segment_overrides);
  1112. end;
  1113. oper.opr.ref.segment:=seg;
  1114. end;
  1115. OPR_LOCAL:
  1116. begin
  1117. if oper.opr.localsegment<>NR_NO then
  1118. begin
  1119. if m_tp7 in current_settings.modeswitches then
  1120. Message(asmr_w_multiple_segment_overrides)
  1121. else
  1122. Message(asmr_e_multiple_segment_overrides);
  1123. end;
  1124. oper.opr.localsegment:=seg;
  1125. end;
  1126. else
  1127. internalerror(2018030703);
  1128. end;
  1129. end;
  1130. { This routine builds up a record offset after a AS_DOT
  1131. token is encountered.
  1132. On entry actasmtoken should be equal to AS_DOT }
  1133. Procedure tx86intreader.BuildRecordOffsetSize(const expr: string;out offset:tcgint;out size:tcgint; out mangledname: string; needvmtofs: boolean; out hastypecast: boolean);
  1134. var
  1135. s: string;
  1136. Begin
  1137. offset:=0;
  1138. size:=0;
  1139. mangledname:='';
  1140. hastypecast:=false;
  1141. s:=expr;
  1142. while (actasmtoken=AS_DOT) do
  1143. begin
  1144. Consume(AS_DOT);
  1145. if actasmtoken in [AS_BYTE,AS_ID,AS_WORD,AS_DWORD,AS_QWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD,AS_ZWORD,AS_ZMMWORD,AS_REGISTER] then
  1146. begin
  1147. s:=s+'.'+actasmpattern;
  1148. consume(actasmtoken);
  1149. end
  1150. else
  1151. begin
  1152. Consume(AS_ID);
  1153. RecoverConsume(true);
  1154. break;
  1155. end;
  1156. end;
  1157. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs,hastypecast) then
  1158. Message(asmr_e_building_record_offset);
  1159. end;
  1160. Procedure tx86intreader.BuildConstSymbolExpression(in_flags: tconstsymbolexpressioninputflags;out value:tcgint;out asmsym:string;out asmsymtyp:TAsmsymtype;out size:tcgint;out out_flags:tconstsymbolexpressionoutputflags);
  1161. var
  1162. tempstr,expr,hs,mangledname : string;
  1163. parenlevel : longint;
  1164. l,k : tcgint;
  1165. hasparen,
  1166. errorflag,
  1167. needvmtofs : boolean;
  1168. prevtok : tasmtoken;
  1169. hl : tasmlabel;
  1170. hssymtyp : Tasmsymtype;
  1171. def : tdef;
  1172. sym : tsym;
  1173. srsymtable : TSymtable;
  1174. hastypecast : boolean;
  1175. Begin
  1176. { reset }
  1177. value:=0;
  1178. asmsym:='';
  1179. asmsymtyp:=AT_DATA;
  1180. size:=0;
  1181. out_flags:=[];
  1182. errorflag:=FALSE;
  1183. tempstr:='';
  1184. expr:='';
  1185. if cseif_startingminus in in_flags then
  1186. expr:='-';
  1187. inexpression:=TRUE;
  1188. parenlevel:=0;
  1189. sym:=nil;
  1190. needvmtofs:=FALSE;
  1191. Repeat
  1192. { Support ugly delphi constructs like: [ECX].1+2[EDX] }
  1193. if (cseif_isref in in_flags) and (actasmtoken=AS_LBRACKET) then
  1194. break;
  1195. if (cseif_referencelike in in_flags) and
  1196. (actasmtoken in [AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_DQWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD]) then
  1197. begin
  1198. { Support ugly tp7 and delphi constructs like 'DD DWORD PTR 5' }
  1199. Consume(actasmtoken);
  1200. Consume(AS_PTR);
  1201. end;
  1202. if (cseif_referencelike in in_flags) and
  1203. (actasmtoken in [AS_LBRACKET,AS_RBRACKET]) then
  1204. case actasmtoken of
  1205. AS_LBRACKET:
  1206. begin
  1207. Consume(AS_LBRACKET);
  1208. if (length(expr)>0) and
  1209. not (expr[length(expr)] in ['+','-']) then
  1210. expr:=expr+'+';
  1211. expr:=expr+'[';
  1212. end;
  1213. AS_RBRACKET:
  1214. begin
  1215. Consume(AS_RBRACKET);
  1216. expr:=expr+']';
  1217. end;
  1218. else
  1219. ;
  1220. end;
  1221. Case actasmtoken of
  1222. AS_LPAREN:
  1223. Begin
  1224. Consume(AS_LPAREN);
  1225. expr:=expr + '(';
  1226. inc(parenlevel);
  1227. end;
  1228. AS_RPAREN:
  1229. Begin
  1230. { Keep the AS_PAREN in actasmtoken, it is maybe a typecast }
  1231. if parenlevel=0 then
  1232. break;
  1233. Consume(AS_RPAREN);
  1234. expr:=expr + ')';
  1235. dec(parenlevel);
  1236. end;
  1237. AS_SHL:
  1238. Begin
  1239. Consume(AS_SHL);
  1240. expr:=expr + '<';
  1241. end;
  1242. AS_SHR:
  1243. Begin
  1244. Consume(AS_SHR);
  1245. expr:=expr + '>';
  1246. end;
  1247. AS_SLASH:
  1248. Begin
  1249. Consume(AS_SLASH);
  1250. expr:=expr + '/';
  1251. end;
  1252. AS_MOD:
  1253. Begin
  1254. Consume(AS_MOD);
  1255. expr:=expr + '%';
  1256. end;
  1257. AS_STAR:
  1258. Begin
  1259. Consume(AS_STAR);
  1260. if (cseif_isref in in_flags) and (actasmtoken=AS_REGISTER) then
  1261. break;
  1262. expr:=expr + '*';
  1263. end;
  1264. AS_PLUS:
  1265. Begin
  1266. Consume(AS_PLUS);
  1267. if (cseif_isref in in_flags) and ((actasmtoken=AS_REGISTER) or (actasmtoken=AS_LBRACKET)) then
  1268. break;
  1269. expr:=expr + '+';
  1270. end;
  1271. AS_MINUS:
  1272. Begin
  1273. Consume(AS_MINUS);
  1274. expr:=expr + '-';
  1275. end;
  1276. AS_AND:
  1277. Begin
  1278. Consume(AS_AND);
  1279. expr:=expr + '&';
  1280. end;
  1281. AS_NOT:
  1282. Begin
  1283. Consume(AS_NOT);
  1284. expr:=expr + '~';
  1285. end;
  1286. AS_XOR:
  1287. Begin
  1288. Consume(AS_XOR);
  1289. expr:=expr + '^';
  1290. end;
  1291. AS_OR:
  1292. Begin
  1293. Consume(AS_OR);
  1294. expr:=expr + '|';
  1295. end;
  1296. AS_INTNUM:
  1297. Begin
  1298. expr:=expr + actasmpattern;
  1299. Consume(AS_INTNUM);
  1300. end;
  1301. {$ifdef i8086}
  1302. AS_SEG:
  1303. begin
  1304. include(out_flags,cseof_isseg);
  1305. Consume(actasmtoken);
  1306. if actasmtoken<>AS_ID then
  1307. Message(asmr_e_seg_without_identifier);
  1308. end;
  1309. {$endif i8086}
  1310. AS_VMTOFFSET,
  1311. AS_OFFSET:
  1312. begin
  1313. if (actasmtoken = AS_OFFSET) then
  1314. begin
  1315. include(in_flags,cseif_needofs);
  1316. include(out_flags,cseof_hasofs);
  1317. end
  1318. else
  1319. needvmtofs:=true;
  1320. Consume(actasmtoken);
  1321. if actasmtoken<>AS_ID then
  1322. Message(asmr_e_offset_without_identifier);
  1323. end;
  1324. AS_SIZEOF,
  1325. AS_TYPE:
  1326. begin
  1327. l:=0;
  1328. hasparen:=false;
  1329. Consume(actasmtoken);
  1330. if actasmtoken=AS_LPAREN then
  1331. begin
  1332. hasparen:=true;
  1333. Consume(AS_LPAREN);
  1334. end;
  1335. if actasmtoken<>AS_ID then
  1336. Message(asmr_e_type_without_identifier)
  1337. else
  1338. begin
  1339. tempstr:=actasmpattern;
  1340. Consume(AS_ID);
  1341. if actasmtoken=AS_DOT then
  1342. begin
  1343. BuildRecordOffsetSize(tempstr,k,l,mangledname,false,hastypecast);
  1344. if mangledname<>'' then
  1345. { procsym }
  1346. Message(asmr_e_wrong_sym_type);
  1347. if hastypecast then
  1348. end
  1349. else
  1350. begin
  1351. asmsearchsym(tempstr,sym,srsymtable);
  1352. if assigned(sym) then
  1353. begin
  1354. case sym.typ of
  1355. staticvarsym,
  1356. localvarsym,
  1357. paravarsym :
  1358. l:=tabstractvarsym(sym).getsize;
  1359. typesym :
  1360. l:=ttypesym(sym).typedef.size;
  1361. else
  1362. Message(asmr_e_wrong_sym_type);
  1363. end;
  1364. end
  1365. else
  1366. Message1(sym_e_unknown_id,tempstr);
  1367. end;
  1368. end;
  1369. str(l, tempstr);
  1370. expr:=expr + tempstr;
  1371. if hasparen then
  1372. Consume(AS_RPAREN);
  1373. end;
  1374. AS_PTR :
  1375. begin
  1376. { Support ugly delphi constructs like <constant> PTR [ref] }
  1377. break;
  1378. end;
  1379. AS_STRING:
  1380. begin
  1381. l:=0;
  1382. case Length(actasmpattern) of
  1383. 1 :
  1384. l:=ord(actasmpattern[1]);
  1385. 2 :
  1386. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  1387. 3 :
  1388. l:=ord(actasmpattern[3]) +
  1389. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  1390. 4 :
  1391. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1392. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1393. else
  1394. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1395. end;
  1396. str(l, tempstr);
  1397. expr:=expr + tempstr;
  1398. Consume(AS_STRING);
  1399. end;
  1400. AS_ID:
  1401. begin
  1402. hs:='';
  1403. hssymtyp:=AT_DATA;
  1404. def:=nil;
  1405. tempstr:=actasmpattern;
  1406. prevtok:=prevasmtoken;
  1407. { stop parsing a constant expression if we find an opcode after a
  1408. non-operator like "db $66 mov eax,ebx" }
  1409. if (prevtok in [AS_ID,AS_INTNUM,AS_RPAREN]) and
  1410. is_asmopcode(actasmpattern) then
  1411. break;
  1412. consume(AS_ID);
  1413. if (tempstr='@CODE') or (tempstr='@DATA') then
  1414. begin
  1415. if asmsym='' then
  1416. begin
  1417. asmsym:=tempstr;
  1418. asmsymtyp:=AT_SECTION;
  1419. end
  1420. else
  1421. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1422. end
  1423. else if SearchIConstant(tempstr,l) then
  1424. begin
  1425. str(l, tempstr);
  1426. expr:=expr + tempstr;
  1427. end
  1428. else
  1429. begin
  1430. if is_locallabel(tempstr) then
  1431. begin
  1432. CreateLocalLabel(tempstr,hl,false);
  1433. hs:=hl.name;
  1434. hssymtyp:=AT_FUNCTION;
  1435. end
  1436. else
  1437. if SearchLabel(tempstr,hl,false) then
  1438. begin
  1439. hs:=hl.name;
  1440. hssymtyp:=AT_FUNCTION;
  1441. end
  1442. else
  1443. begin
  1444. asmsearchsym(tempstr,sym,srsymtable);
  1445. if assigned(sym) then
  1446. begin
  1447. case sym.typ of
  1448. staticvarsym :
  1449. begin
  1450. hs:=tstaticvarsym(sym).mangledname;
  1451. def:=tstaticvarsym(sym).vardef;
  1452. end;
  1453. localvarsym,
  1454. paravarsym :
  1455. begin
  1456. Message(asmr_e_no_local_or_para_allowed);
  1457. end;
  1458. procsym :
  1459. begin
  1460. if Tprocsym(sym).ProcdefList.Count>1 then
  1461. Message(asmr_w_calling_overload_func);
  1462. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1463. {$ifdef i8086}
  1464. if is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  1465. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions) then
  1466. include(out_flags,cseof_is_farproc_entry)
  1467. else
  1468. exclude(out_flags,cseof_is_farproc_entry);
  1469. {$endif i8086}
  1470. hssymtyp:=AT_FUNCTION;
  1471. end;
  1472. typesym :
  1473. begin
  1474. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1475. Message(asmr_e_wrong_sym_type);
  1476. size:=ttypesym(sym).typedef.size;
  1477. end;
  1478. fieldvarsym :
  1479. begin
  1480. tempstr:=upper(tdef(sym.owner.defowner).GetTypeName)+'.'+tempstr;
  1481. end;
  1482. else
  1483. Message(asmr_e_wrong_sym_type);
  1484. end;
  1485. end
  1486. else
  1487. Message1(sym_e_unknown_id,tempstr);
  1488. end;
  1489. { symbol found? }
  1490. if hs<>'' then
  1491. begin
  1492. if asmsym='' then
  1493. begin
  1494. asmsym:=hs;
  1495. asmsymtyp:=hssymtyp;
  1496. end
  1497. else
  1498. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1499. if (expr='') or (expr[length(expr)]='+') then
  1500. begin
  1501. { don't remove the + if there could be a record field }
  1502. if actasmtoken<>AS_DOT then
  1503. delete(expr,length(expr),1);
  1504. end
  1505. else
  1506. if (cseif_needofs in in_flags) then
  1507. begin
  1508. if (prevtok<>AS_OFFSET) then
  1509. Message(asmr_e_need_offset);
  1510. end
  1511. else
  1512. Message(asmr_e_only_add_relocatable_symbol);
  1513. end;
  1514. if (actasmtoken=AS_DOT) or
  1515. (assigned(sym) and
  1516. is_normal_fieldvarsym(sym)) then
  1517. begin
  1518. BuildRecordOffsetSize(tempstr,l,size,hs,needvmtofs,hastypecast);
  1519. if hs <> '' then
  1520. hssymtyp:=AT_FUNCTION
  1521. else
  1522. begin
  1523. str(l, tempstr);
  1524. expr:=expr + tempstr;
  1525. end
  1526. end
  1527. else if (actasmtoken<>AS_DOT) and
  1528. assigned(sym) and
  1529. (sym.typ=typesym) and
  1530. (ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1531. begin
  1532. { just a record type (without being followed by dot)
  1533. evaluates to 0. Ugly, but TP7 compatible. }
  1534. expr:=expr+'0';
  1535. end
  1536. else
  1537. begin
  1538. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1539. delete(expr,length(expr),1);
  1540. end;
  1541. if (actasmtoken=AS_LBRACKET) and
  1542. assigned(def) and
  1543. (def.typ=arraydef) then
  1544. begin
  1545. consume(AS_LBRACKET);
  1546. l:=BuildConstExpression;
  1547. if l<tarraydef(def).lowrange then
  1548. begin
  1549. Message(asmr_e_constant_out_of_bounds);
  1550. l:=0;
  1551. end
  1552. else
  1553. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  1554. str(l, tempstr);
  1555. expr:=expr + '+' + tempstr;
  1556. consume(AS_RBRACKET);
  1557. end;
  1558. end;
  1559. { check if there are wrong operator used like / or mod etc. }
  1560. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  1561. Message(asmr_e_only_add_relocatable_symbol);
  1562. end;
  1563. AS_ALIGN,
  1564. AS_DB,
  1565. AS_DW,
  1566. AS_DD,
  1567. AS_DQ,
  1568. AS_END,
  1569. AS_RBRACKET,
  1570. AS_SEPARATOR,
  1571. AS_COMMA,
  1572. AS_COLON:
  1573. break;
  1574. else
  1575. begin
  1576. { write error only once. }
  1577. if not errorflag then
  1578. Message(asmr_e_invalid_constant_expression);
  1579. { consume tokens until we find COMMA or SEPARATOR }
  1580. Consume(actasmtoken);
  1581. errorflag:=TRUE;
  1582. end;
  1583. end;
  1584. Until false;
  1585. { calculate expression }
  1586. if not ErrorFlag then
  1587. value:=CalculateExpression(expr)
  1588. else
  1589. value:=0;
  1590. { no longer in an expression }
  1591. inexpression:=FALSE;
  1592. end;
  1593. Function tx86intreader.BuildConstExpression:aint;
  1594. var
  1595. l,size : tcgint;
  1596. hs : string;
  1597. hssymtyp : TAsmsymtype;
  1598. out_flags : tconstsymbolexpressionoutputflags;
  1599. begin
  1600. BuildConstSymbolExpression([],l,hs,hssymtyp,size,out_flags);
  1601. if hs<>'' then
  1602. Message(asmr_e_relocatable_symbol_not_allowed);
  1603. BuildConstExpression:=l;
  1604. end;
  1605. Function tx86intreader.BuildRefConstExpression(out size:tcgint;startingminus:boolean):aint;
  1606. var
  1607. l : tcgint;
  1608. hs : string;
  1609. hssymtyp : TAsmsymtype;
  1610. in_flags : tconstsymbolexpressioninputflags;
  1611. out_flags : tconstsymbolexpressionoutputflags;
  1612. begin
  1613. in_flags:=[cseif_isref];
  1614. if startingminus then
  1615. include(in_flags,cseif_startingminus);
  1616. BuildConstSymbolExpression(in_flags,l,hs,hssymtyp,size,out_flags);
  1617. if hs<>'' then
  1618. Message(asmr_e_relocatable_symbol_not_allowed);
  1619. BuildRefConstExpression:=l;
  1620. end;
  1621. procedure tx86intreader.BuildReference(oper : tx86operand);
  1622. var
  1623. scale : byte;
  1624. k,l,size : tcgint;
  1625. tempstr,hs : string;
  1626. tempsymtyp : tasmsymtype;
  1627. code : integer;
  1628. hreg : tregister;
  1629. GotStar,GotOffset,HadVar,
  1630. GotPlus,Negative,BracketlessReference : boolean;
  1631. hl : tasmlabel;
  1632. hastypecast: boolean;
  1633. tmpoper: tx86operand;
  1634. cse_in_flags: tconstsymbolexpressioninputflags;
  1635. cse_out_flags: tconstsymbolexpressionoutputflags;
  1636. Begin
  1637. if actasmtoken=AS_LBRACKET then
  1638. begin
  1639. Consume(AS_LBRACKET);
  1640. BracketlessReference:=false;
  1641. end
  1642. else
  1643. BracketlessReference:=true;
  1644. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  1645. oper.InitRef;
  1646. GotStar:=false;
  1647. GotPlus:=true;
  1648. GotOffset:=false;
  1649. Negative:=false;
  1650. Scale:=0;
  1651. repeat
  1652. if GotOffset and (actasmtoken<>AS_ID) then
  1653. Message(asmr_e_invalid_reference_syntax);
  1654. Case actasmtoken of
  1655. AS_ID, { Constant reference expression OR variable reference expression }
  1656. AS_VMTOFFSET:
  1657. Begin
  1658. if not GotPlus then
  1659. Message(asmr_e_invalid_reference_syntax);
  1660. GotStar:=false;
  1661. GotPlus:=false;
  1662. if (actasmtoken = AS_VMTOFFSET) or
  1663. (SearchIConstant(actasmpattern,l) or
  1664. SearchRecordType(actasmpattern)) then
  1665. begin
  1666. l:=BuildRefConstExpression(size,negative);
  1667. if size<>0 then
  1668. oper.SetSize(size,false);
  1669. negative:=false; { "l" was negated if necessary }
  1670. GotPlus:=(prevasmtoken=AS_PLUS);
  1671. GotStar:=(prevasmtoken=AS_STAR);
  1672. case oper.opr.typ of
  1673. OPR_LOCAL :
  1674. begin
  1675. if GotStar then
  1676. Message(asmr_e_invalid_reference_syntax);
  1677. Inc(oper.opr.localsymofs,l);
  1678. end;
  1679. OPR_REFERENCE :
  1680. begin
  1681. if GotStar then
  1682. oper.opr.ref.scalefactor:=l
  1683. else
  1684. Inc(oper.opr.ref.offset,l);
  1685. end;
  1686. else
  1687. internalerror(2019050715);
  1688. end;
  1689. end
  1690. else
  1691. Begin
  1692. if negative and not oper.hasvar then
  1693. Message(asmr_e_only_add_relocatable_symbol)
  1694. else if oper.hasvar and not GotOffset and
  1695. (not negative or assigned(oper.opr.ref.relsymbol)) then
  1696. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1697. HadVar:=oper.hasvar and GotOffset;
  1698. tempstr:=actasmpattern;
  1699. Consume(AS_ID);
  1700. { typecasting? }
  1701. if (actasmtoken=AS_LPAREN) and
  1702. SearchType(tempstr,l) then
  1703. begin
  1704. oper.hastype:=true;
  1705. oper.typesize:=l;
  1706. Consume(AS_LPAREN);
  1707. BuildOperand(oper,true);
  1708. Consume(AS_RPAREN);
  1709. end
  1710. else
  1711. if is_locallabel(tempstr) then
  1712. begin
  1713. CreateLocalLabel(tempstr,hl,false);
  1714. oper.InitRef;
  1715. oper.haslabelref:=true;
  1716. if not negative then
  1717. begin
  1718. oper.opr.ref.symbol:=hl;
  1719. oper.hasvar:=true;
  1720. end
  1721. else
  1722. oper.opr.ref.relsymbol:=hl;
  1723. {$ifdef i8086}
  1724. if oper.opr.ref.segment=NR_NO then
  1725. oper.opr.ref.segment:=NR_CS;
  1726. {$endif i8086}
  1727. end
  1728. else
  1729. if oper.SetupVar(tempstr,GotOffset) then
  1730. begin
  1731. { convert OPR_LOCAL register para into a reference base }
  1732. if (oper.opr.typ=OPR_LOCAL) and
  1733. AsmRegisterPara(oper.opr.localsym) then
  1734. oper.InitRefConvertLocal
  1735. else
  1736. begin
  1737. {$ifdef x86_64}
  1738. if actasmtoken=AS_WRT then
  1739. begin
  1740. if (oper.opr.typ=OPR_REFERENCE) then
  1741. begin
  1742. Consume(AS_WRT);
  1743. Consume(AS___GOTPCREL);
  1744. if (oper.opr.ref.base<>NR_NO) or
  1745. (oper.opr.ref.index<>NR_NO) or
  1746. (oper.opr.ref.offset<>0) then
  1747. Message(asmr_e_wrong_gotpcrel_intel_syntax);
  1748. if tf_no_pic_supported in target_info.flags then
  1749. Message(asmr_e_no_gotpcrel_support);
  1750. oper.opr.ref.refaddr:=addr_pic;
  1751. oper.opr.ref.base:=NR_RIP;
  1752. end
  1753. else
  1754. message(asmr_e_invalid_reference_syntax);
  1755. end;
  1756. {$endif x86_64}
  1757. end;
  1758. end
  1759. else
  1760. Message1(sym_e_unknown_id,tempstr);
  1761. { record.field ? }
  1762. if actasmtoken=AS_DOT then
  1763. begin
  1764. BuildRecordOffsetSize(tempstr,l,k,hs,false,hastypecast);
  1765. if (hs<>'') then
  1766. Message(asmr_e_invalid_symbol_ref);
  1767. case oper.opr.typ of
  1768. OPR_LOCAL :
  1769. inc(oper.opr.localsymofs,l);
  1770. OPR_REFERENCE :
  1771. inc(oper.opr.ref.offset,l);
  1772. else
  1773. internalerror(2019050716);
  1774. end;
  1775. if hastypecast then
  1776. oper.hastype:=true;
  1777. oper.SetSize(k,false);
  1778. end;
  1779. if GotOffset then
  1780. begin
  1781. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1782. begin
  1783. if (oper.opr.typ=OPR_REFERENCE) then
  1784. oper.opr.ref.base:=NR_NO;
  1785. oper.hasvar:=hadvar;
  1786. end
  1787. else
  1788. begin
  1789. if oper.hasvar and hadvar then
  1790. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1791. { should we allow ?? }
  1792. end;
  1793. end;
  1794. end;
  1795. GotOffset:=false;
  1796. end;
  1797. AS_PLUS :
  1798. Begin
  1799. Consume(AS_PLUS);
  1800. Negative:=false;
  1801. GotPlus:=true;
  1802. GotStar:=false;
  1803. Scale:=0;
  1804. end;
  1805. AS_DOT :
  1806. Begin
  1807. { Handle like a + }
  1808. Consume(AS_DOT);
  1809. Negative:=false;
  1810. GotPlus:=true;
  1811. GotStar:=false;
  1812. Scale:=0;
  1813. end;
  1814. AS_MINUS :
  1815. begin
  1816. Consume(AS_MINUS);
  1817. Negative:=true;
  1818. GotPlus:=true;
  1819. GotStar:=false;
  1820. Scale:=0;
  1821. end;
  1822. AS_STAR : { Scaling, with eax*4 order }
  1823. begin
  1824. Consume(AS_STAR);
  1825. hs:='';
  1826. l:=0;
  1827. case actasmtoken of
  1828. AS_ID,
  1829. AS_LPAREN :
  1830. l:=BuildConstExpression;
  1831. AS_INTNUM:
  1832. Begin
  1833. hs:=actasmpattern;
  1834. Consume(AS_INTNUM);
  1835. end;
  1836. AS_REGISTER :
  1837. begin
  1838. case oper.opr.typ of
  1839. OPR_REFERENCE :
  1840. begin
  1841. if oper.opr.ref.scalefactor=0 then
  1842. begin
  1843. if scale<>0 then
  1844. begin
  1845. oper.opr.ref.scalefactor:=scale;
  1846. scale:=0;
  1847. end
  1848. else
  1849. Message(asmr_e_wrong_scale_factor);
  1850. end
  1851. else
  1852. Message(asmr_e_invalid_reference_syntax);
  1853. end;
  1854. OPR_LOCAL :
  1855. begin
  1856. if oper.opr.localscale=0 then
  1857. begin
  1858. if scale<>0 then
  1859. begin
  1860. oper.opr.localscale:=scale;
  1861. scale:=0;
  1862. end
  1863. else
  1864. Message(asmr_e_wrong_scale_factor);
  1865. end
  1866. else
  1867. Message(asmr_e_invalid_reference_syntax);
  1868. end;
  1869. else
  1870. internalerror(2019050719);
  1871. end;
  1872. end;
  1873. else
  1874. Message(asmr_e_invalid_reference_syntax);
  1875. end;
  1876. if actasmtoken<>AS_REGISTER then
  1877. begin
  1878. if hs<>'' then
  1879. val(hs,l,code);
  1880. case oper.opr.typ of
  1881. OPR_REFERENCE :
  1882. oper.opr.ref.scalefactor:=l;
  1883. OPR_LOCAL :
  1884. oper.opr.localscale:=l;
  1885. else
  1886. internalerror(2019050717);
  1887. end;
  1888. if l>9 then
  1889. Message(asmr_e_wrong_scale_factor);
  1890. end;
  1891. GotPlus:=false;
  1892. GotStar:=false;
  1893. end;
  1894. AS_REGISTER :
  1895. begin
  1896. hreg:=actasmregister;
  1897. Consume(AS_REGISTER, MightHaveExtension(actopcode));
  1898. while actasmtoken in OPEXT_STARTASMTOKEN do
  1899. begin
  1900. consume_voperand_ext(oper);
  1901. end;
  1902. if actasmtoken=AS_COLON then
  1903. begin
  1904. Consume(AS_COLON);
  1905. oper.InitRefConvertLocal;
  1906. SetSegmentOverride(oper,hreg);
  1907. end
  1908. else
  1909. begin
  1910. if not((GotPlus and (not Negative)) or
  1911. GotStar) then
  1912. Message(asmr_e_invalid_reference_syntax);
  1913. { this register will be the index:
  1914. 1. just read a *
  1915. 2. next token is a *
  1916. 3. base register is already used }
  1917. case oper.opr.typ of
  1918. OPR_LOCAL :
  1919. begin
  1920. if (oper.opr.localindexreg<>NR_NO) then
  1921. Message(asmr_e_multiple_index);
  1922. {$ifdef x86_64}
  1923. { Locals/parameters cannot be accessed RIP-relative. Need a dedicated error message here? }
  1924. if (hreg=NR_RIP) then
  1925. Message(asmr_e_no_local_or_para_allowed);
  1926. {$endif x86_64}
  1927. oper.opr.localindexreg:=hreg;
  1928. if scale<>0 then
  1929. begin
  1930. oper.opr.localscale:=scale;
  1931. scale:=0;
  1932. end;
  1933. end;
  1934. OPR_REFERENCE :
  1935. begin
  1936. if (GotStar) or
  1937. (actasmtoken=AS_STAR) or
  1938. (oper.opr.ref.base<>NR_NO) then
  1939. begin
  1940. if (oper.opr.ref.index<>NR_NO) then
  1941. Message(asmr_e_multiple_index);
  1942. oper.opr.ref.index:=hreg;
  1943. if scale<>0 then
  1944. begin
  1945. oper.opr.ref.scalefactor:=scale;
  1946. scale:=0;
  1947. end;
  1948. end
  1949. else
  1950. begin
  1951. oper.opr.ref.base:=hreg;
  1952. {$ifdef x86_64}
  1953. { non-GOT based RIP-relative accesses are also position-independent }
  1954. if (oper.opr.ref.base=NR_RIP) and
  1955. (oper.opr.ref.refaddr<>addr_pic) then
  1956. oper.opr.ref.refaddr:=addr_pic_no_got;
  1957. {$endif x86_64}
  1958. end;
  1959. end;
  1960. else
  1961. internalerror(2019050718);
  1962. end;
  1963. GotPlus:=false;
  1964. GotStar:=false;
  1965. end;
  1966. end;
  1967. AS_OFFSET :
  1968. begin
  1969. Consume(AS_OFFSET);
  1970. GotOffset:=true;
  1971. end;
  1972. AS_TYPE,
  1973. AS_NOT,
  1974. AS_STRING,
  1975. AS_INTNUM,
  1976. AS_LPAREN : { Constant reference expression }
  1977. begin
  1978. if not GotPlus and not GotStar then
  1979. Message(asmr_e_invalid_reference_syntax);
  1980. cse_in_flags:=[cseif_needofs,cseif_isref];
  1981. if GotPlus and negative then
  1982. include(cse_in_flags,cseif_startingminus);
  1983. BuildConstSymbolExpression(cse_in_flags,l,tempstr,tempsymtyp,size,cse_out_flags);
  1984. { already handled by BuildConstSymbolExpression(); must be
  1985. handled there to avoid [reg-1+1] being interpreted as
  1986. [reg-(1+1)] }
  1987. negative:=false;
  1988. if tempstr<>'' then
  1989. begin
  1990. if GotStar then
  1991. Message(asmr_e_only_add_relocatable_symbol);
  1992. if not assigned(oper.opr.ref.symbol) then
  1993. begin
  1994. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1995. {$ifdef i8086}
  1996. if cseof_isseg in cse_out_flags then
  1997. begin
  1998. if not (oper.opr.ref.refaddr in [addr_fardataseg,addr_dgroup]) then
  1999. oper.opr.ref.refaddr:=addr_seg;
  2000. end
  2001. else if (tempsymtyp=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  2002. oper.opr.ref.segment:=NR_CS;
  2003. {$endif i8086}
  2004. end
  2005. else
  2006. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  2007. end;
  2008. case oper.opr.typ of
  2009. OPR_REFERENCE :
  2010. begin
  2011. if GotStar then
  2012. oper.opr.ref.scalefactor:=l
  2013. else if (prevasmtoken = AS_STAR) then
  2014. begin
  2015. if scale<>0 then
  2016. scale:=l*scale
  2017. else
  2018. scale:=l;
  2019. end
  2020. else
  2021. begin
  2022. Inc(oper.opr.ref.offset,l);
  2023. Inc(oper.opr.constoffset,l);
  2024. end;
  2025. end;
  2026. OPR_LOCAL :
  2027. begin
  2028. if GotStar then
  2029. oper.opr.localscale:=l
  2030. else if (prevasmtoken = AS_STAR) then
  2031. begin
  2032. if scale<>0 then
  2033. scale:=l*scale
  2034. else
  2035. scale:=l;
  2036. end
  2037. else
  2038. Inc(oper.opr.localsymofs,l);
  2039. end;
  2040. else
  2041. internalerror(2019050714);
  2042. end;
  2043. GotPlus:=(prevasmtoken=AS_PLUS) or
  2044. (prevasmtoken=AS_MINUS);
  2045. if GotPlus then
  2046. negative := prevasmtoken = AS_MINUS;
  2047. GotStar:=(prevasmtoken=AS_STAR);
  2048. end;
  2049. AS_LBRACKET :
  2050. begin
  2051. if (GotPlus and Negative) or GotStar then
  2052. Message(asmr_e_invalid_reference_syntax);
  2053. tmpoper:=Tx86Operand.create;
  2054. BuildReference(tmpoper);
  2055. AddReferences(oper,tmpoper);
  2056. tmpoper.Free;
  2057. GotPlus:=false;
  2058. GotStar:=false;
  2059. end;
  2060. AS_RBRACKET :
  2061. begin
  2062. if GotPlus or GotStar or BracketlessReference then
  2063. Message(asmr_e_invalid_reference_syntax);
  2064. Consume(AS_RBRACKET, MightHaveExtension(actopcode));
  2065. while actasmtoken in OPEXT_STARTASMTOKEN do
  2066. begin
  2067. consume_voperand_ext(oper);
  2068. end;
  2069. if actasmtoken=AS_LBRACKET then
  2070. begin
  2071. tmpoper:=Tx86Operand.create;
  2072. BuildReference(tmpoper);
  2073. AddReferences(oper,tmpoper);
  2074. tmpoper.Free;
  2075. end;
  2076. break;
  2077. end;
  2078. AS_SEPARATOR,
  2079. AS_END,
  2080. AS_COMMA:
  2081. begin
  2082. if not BracketlessReference then
  2083. begin
  2084. Message(asmr_e_invalid_reference_syntax);
  2085. RecoverConsume(true);
  2086. end;
  2087. break;
  2088. end;
  2089. else
  2090. Begin
  2091. Message(asmr_e_invalid_reference_syntax);
  2092. RecoverConsume(true);
  2093. break;
  2094. end;
  2095. end;
  2096. until false;
  2097. end;
  2098. { Disable range check because opr.val must accept values from min(longint) to max(dword) for i386 }
  2099. {$R-}
  2100. Procedure tx86intreader.BuildConstantOperand(oper: tx86operand);
  2101. var
  2102. l,size : tcgint;
  2103. tempstr : string;
  2104. tempsymtyp : tasmsymtype;
  2105. cse_out_flags : tconstsymbolexpressionoutputflags;
  2106. begin
  2107. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  2108. Message(asmr_e_invalid_operand_type);
  2109. BuildConstSymbolExpression([cseif_needofs],l,tempstr,tempsymtyp,size,cse_out_flags);
  2110. {$ifdef i8086}
  2111. if tempstr='@DATA' then
  2112. begin
  2113. if not (cseof_isseg in cse_out_flags) then
  2114. Message(asmr_e_CODE_or_DATA_without_SEG);
  2115. oper.SetupData;
  2116. end
  2117. else if tempstr='@CODE' then
  2118. begin
  2119. if not (cseof_isseg in cse_out_flags) then
  2120. Message(asmr_e_CODE_or_DATA_without_SEG);
  2121. oper.SetupCode;
  2122. end
  2123. else
  2124. {$endif i8086}
  2125. if tempstr<>'' then
  2126. begin
  2127. oper.opr.typ:=OPR_SYMBOL;
  2128. oper.opr.symofs:=l;
  2129. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  2130. oper.opr.symseg:=cseof_isseg in cse_out_flags;
  2131. oper.opr.sym_farproc_entry:=cseof_is_farproc_entry in cse_out_flags;
  2132. end
  2133. else
  2134. if oper.opr.typ=OPR_NONE then
  2135. begin
  2136. oper.opr.typ:=OPR_CONSTANT;
  2137. oper.opr.val:=l;
  2138. end
  2139. else
  2140. inc(oper.opr.val,l);
  2141. end;
  2142. Procedure tx86intreader.BuildOperand(oper: tx86operand;istypecast:boolean);
  2143. procedure AddLabelOperand(hl:tasmlabel);
  2144. begin
  2145. if (oper.opr.typ=OPR_NONE) and
  2146. is_calljmp(actopcode) then
  2147. begin
  2148. oper.opr.typ:=OPR_SYMBOL;
  2149. oper.opr.symbol:=hl;
  2150. end
  2151. else
  2152. begin
  2153. oper.InitRef;
  2154. oper.opr.ref.symbol:=hl;
  2155. oper.haslabelref:=true;
  2156. {$ifdef i8086}
  2157. if oper.opr.ref.segment=NR_NO then
  2158. oper.opr.ref.segment:=NR_CS;
  2159. {$endif i8086}
  2160. end;
  2161. end;
  2162. var
  2163. expr,
  2164. hs : string;
  2165. tempreg : tregister;
  2166. l : tcgint;
  2167. hl : tasmlabel;
  2168. toffset,
  2169. tsize : tcgint;
  2170. hastypecast: boolean;
  2171. begin
  2172. oper.vopext := 0;
  2173. expr:='';
  2174. repeat
  2175. if actasmtoken=AS_DOT then
  2176. begin
  2177. if expr<>'' then
  2178. begin
  2179. BuildRecordOffsetSize(expr,toffset,tsize,hs,false,hastypecast);
  2180. if (oper.opr.typ<>OPR_NONE) and
  2181. (hs<>'') then
  2182. Message(asmr_e_wrong_sym_type);
  2183. oper.SetSize(tsize,true);
  2184. if hastypecast then
  2185. oper.hastype:=true;
  2186. { we have used the size of a field. Reset the typesize of the record }
  2187. oper.typesize:=0;
  2188. case oper.opr.typ of
  2189. OPR_LOCAL :
  2190. begin
  2191. { don't allow direct access to fields of parameters, because that
  2192. will generate buggy code. Allow it only for explicit typecasting
  2193. and when the parameter is in a register (delphi compatible) }
  2194. if (not oper.hastype) then
  2195. checklocalsubscript(oper.opr.localsym);
  2196. oper.opr.localforceref:=true;
  2197. inc(oper.opr.localsymofs,toffset);
  2198. oper.opr.localvarsize := tsize;
  2199. end;
  2200. OPR_CONSTANT :
  2201. inc(oper.opr.val,toffset);
  2202. OPR_REFERENCE :
  2203. begin
  2204. inc(oper.opr.ref.offset,toffset);
  2205. oper.opr.varsize := tsize;
  2206. end;
  2207. OPR_NONE :
  2208. begin
  2209. if (hs <> '') then
  2210. begin
  2211. oper.opr.typ:=OPR_SYMBOL;
  2212. oper.opr.symbol:=current_asmdata.RefAsmSymbol(hs,AT_FUNCTION);
  2213. end
  2214. else
  2215. begin
  2216. oper.opr.typ:=OPR_CONSTANT;
  2217. oper.opr.val:=toffset;
  2218. end;
  2219. end;
  2220. OPR_REGISTER :
  2221. Message(asmr_e_invalid_reference_syntax);
  2222. OPR_SYMBOL:
  2223. Message(asmr_e_invalid_symbol_ref);
  2224. else
  2225. internalerror(200309222);
  2226. end;
  2227. expr:='';
  2228. end
  2229. else
  2230. begin
  2231. { See it as a separator }
  2232. Consume(AS_DOT);
  2233. end;
  2234. end;
  2235. case actasmtoken of
  2236. {$ifndef i8086}
  2237. AS_SEG :
  2238. Begin
  2239. Message(asmr_e_seg_not_supported);
  2240. Consume(actasmtoken);
  2241. end;
  2242. {$else not i8086}
  2243. AS_SEG,
  2244. {$endif not i8086}
  2245. AS_OFFSET,
  2246. AS_SIZEOF,
  2247. AS_VMTOFFSET,
  2248. AS_TYPE,
  2249. AS_NOT,
  2250. AS_STRING,
  2251. AS_PLUS,
  2252. AS_MINUS,
  2253. AS_LPAREN,
  2254. AS_INTNUM :
  2255. begin
  2256. case oper.opr.typ of
  2257. OPR_REFERENCE :
  2258. {$ifndef x86_64}
  2259. { this is for the i386 scenario where you have
  2260. <load got into ebx>
  2261. mov eax, [ebx].offset globalvar
  2262. x86-64 uses RIP-based addresses (both for GOT and non-GOT
  2263. relative accesses)
  2264. }
  2265. if (actasmtoken=AS_OFFSET) and
  2266. (cs_create_pic in current_settings.moduleswitches) then
  2267. begin
  2268. Consume(AS_OFFSET);
  2269. oper.opr.ref.refaddr:=addr_pic;
  2270. BuildOperand(oper,false);
  2271. end
  2272. else
  2273. {$endif x86_64}
  2274. begin
  2275. l := BuildRefConstExpression(tsize);
  2276. if tsize<>0 then
  2277. oper.SetSize(tsize,false);
  2278. inc(oper.opr.ref.offset,l);
  2279. inc(oper.opr.constoffset,l);
  2280. end;
  2281. OPR_LOCAL :
  2282. begin
  2283. l := BuildConstExpression;
  2284. inc(oper.opr.localsymofs,l);
  2285. inc(oper.opr.localconstoffset,l);
  2286. end;
  2287. OPR_NONE,
  2288. OPR_CONSTANT :
  2289. BuildConstantOperand(oper);
  2290. else
  2291. Message(asmr_e_invalid_operand_type);
  2292. end;
  2293. end;
  2294. AS_PTR :
  2295. begin
  2296. if not oper.hastype then
  2297. begin
  2298. if (oper.opr.typ=OPR_CONSTANT) then
  2299. begin
  2300. oper.typesize:=oper.opr.val;
  2301. { reset constant value of operand }
  2302. oper.opr.typ:=OPR_NONE;
  2303. oper.opr.val:=0;
  2304. end
  2305. else
  2306. Message(asmr_e_syn_operand);
  2307. end;
  2308. Consume(AS_PTR);
  2309. { in delphi mode, allow e.g. call dword ptr eax,
  2310. see also webtbs/tw18225.pp }
  2311. if not(m_delphi in current_settings.modeswitches) then
  2312. oper.InitRef;
  2313. { if the operand subscripts a record, the typesize will be
  2314. rest -> save it here and restore it afterwards }
  2315. l:=oper.typesize;
  2316. BuildOperand(oper,false);
  2317. oper.setsize(l,true);
  2318. end;
  2319. AS_ID : { A constant expression, or a Variable ref. }
  2320. Begin
  2321. { Label or Special symbol reference? }
  2322. if actasmpattern[1] = '@' then
  2323. Begin
  2324. if actasmpattern = '@RESULT' then
  2325. Begin
  2326. oper.SetupResult;
  2327. Consume(AS_ID);
  2328. expr:='result';
  2329. end
  2330. else
  2331. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  2332. begin
  2333. {$ifdef i8086}
  2334. Message(asmr_e_CODE_or_DATA_without_SEG);
  2335. {$else i8086}
  2336. Message(asmr_w_CODE_and_DATA_not_supported);
  2337. {$endif i8086}
  2338. Consume(AS_ID);
  2339. end
  2340. else
  2341. { Local Label }
  2342. begin
  2343. CreateLocalLabel(actasmpattern,hl,false);
  2344. Consume(AS_ID);
  2345. AddLabelOperand(hl);
  2346. end;
  2347. end
  2348. else
  2349. { support result for delphi modes }
  2350. if (m_objpas in current_settings.modeswitches) and (actasmpattern='RESULT') then
  2351. begin
  2352. oper.SetUpResult;
  2353. Consume(AS_ID);
  2354. expr:='result';
  2355. end
  2356. { probably a variable or normal expression }
  2357. { or a procedure (such as in CALL ID) }
  2358. else
  2359. Begin
  2360. { is it a constant ? }
  2361. if SearchIConstant(actasmpattern,l) then
  2362. Begin
  2363. case oper.opr.typ of
  2364. OPR_REFERENCE :
  2365. begin
  2366. l := BuildRefConstExpression(tsize);
  2367. if tsize<>0 then
  2368. oper.SetSize(tsize,false);
  2369. inc(oper.opr.ref.offset,l);
  2370. inc(oper.opr.constoffset,l);
  2371. end;
  2372. OPR_LOCAL :
  2373. begin
  2374. l := BuildRefConstExpression(tsize);
  2375. if tsize<>0 then
  2376. oper.SetSize(tsize,false);
  2377. inc(oper.opr.localsymofs,l);
  2378. inc(oper.opr.localconstoffset,l);
  2379. end;
  2380. OPR_NONE,
  2381. OPR_CONSTANT :
  2382. BuildConstantOperand(oper);
  2383. else
  2384. Message(asmr_e_invalid_operand_type);
  2385. end;
  2386. end
  2387. else
  2388. { Check for pascal label }
  2389. if SearchLabel(actasmpattern,hl,false) then
  2390. begin
  2391. Consume(AS_ID);
  2392. AddLabelOperand(hl);
  2393. end
  2394. else
  2395. { is it a normal variable ? }
  2396. Begin
  2397. expr:=actasmpattern;
  2398. Consume(AS_ID, MightHaveExtension(actopcode));
  2399. while actasmtoken in OPEXT_STARTASMTOKEN do
  2400. begin
  2401. consume_voperand_ext(oper);
  2402. end;
  2403. { typecasting? }
  2404. if SearchType(expr,l) then
  2405. begin
  2406. oper.hastype:=true;
  2407. oper.typesize:=l;
  2408. case actasmtoken of
  2409. AS_LPAREN :
  2410. begin
  2411. { Support Type([Reference]) }
  2412. Consume(AS_LPAREN);
  2413. BuildOperand(oper,true);
  2414. { Delphi also supports Type(Register) and
  2415. interprets it the same as Type([Register]). }
  2416. if (oper.opr.typ = OPR_REGISTER) then
  2417. { This also sets base to the register. }
  2418. oper.InitRef;
  2419. Consume(AS_RPAREN);
  2420. end;
  2421. AS_LBRACKET :
  2422. begin
  2423. { Support Var.Type[Index] }
  2424. { Convert @label.Byte[1] to reference }
  2425. if oper.opr.typ=OPR_SYMBOL then
  2426. oper.initref;
  2427. end;
  2428. else
  2429. ;
  2430. end;
  2431. end
  2432. else
  2433. begin
  2434. if not oper.SetupVar(expr,false) then
  2435. Begin
  2436. { not a variable, check special variables.. }
  2437. if expr = 'SELF' then
  2438. begin
  2439. oper.SetupSelf;
  2440. expr:='self';
  2441. end
  2442. else
  2443. begin
  2444. Message1(sym_e_unknown_id,expr);
  2445. expr:='';
  2446. end;
  2447. end;
  2448. { indexed access to variable? }
  2449. if actasmtoken=AS_LBRACKET then
  2450. begin
  2451. { ... then the operand size is not known anymore }
  2452. oper.size:=OS_NO;
  2453. BuildReference(oper);
  2454. end;
  2455. end;
  2456. end;
  2457. end;
  2458. end;
  2459. AS_REGISTER : { Register, a variable reference or a constant reference }
  2460. begin
  2461. { save the type of register used. }
  2462. tempreg:=actasmregister;
  2463. Consume(AS_REGISTER, MightHaveExtension(actopcode));
  2464. if (getregtype(tempreg) in [R_MMREGISTER, R_ADDRESSREGISTER]) then
  2465. begin
  2466. while actasmtoken in OPEXT_STARTASMTOKEN do
  2467. begin
  2468. consume_voperand_ext(oper);
  2469. end;
  2470. end;
  2471. if actasmtoken = AS_COLON then
  2472. Begin
  2473. Consume(AS_COLON);
  2474. oper.InitRef;
  2475. SetSegmentOverride(oper,tempreg);
  2476. BuildReference(oper);
  2477. end
  2478. else
  2479. { Simple register }
  2480. begin
  2481. if (oper.opr.typ <> OPR_NONE) then
  2482. Message(asmr_e_syn_operand);
  2483. oper.opr.typ:=OPR_REGISTER;
  2484. oper.opr.reg:=tempreg;
  2485. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  2486. end;
  2487. end;
  2488. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  2489. Begin
  2490. BuildReference(oper);
  2491. end;
  2492. AS_DWORD,
  2493. AS_BYTE,
  2494. AS_WORD,
  2495. AS_TBYTE,
  2496. AS_DQWORD,
  2497. AS_QWORD,
  2498. AS_OWORD,
  2499. AS_XMMWORD,
  2500. AS_YWORD,
  2501. AS_YMMWORD,
  2502. AS_ZWORD,
  2503. AS_ZMMWORD
  2504. :
  2505. begin
  2506. { Type specifier }
  2507. oper.hastype:=true;
  2508. oper.typesize:=0;
  2509. case actasmtoken of
  2510. AS_DWORD : oper.typesize:=4;
  2511. AS_WORD : oper.typesize:=2;
  2512. AS_BYTE : oper.typesize:=1;
  2513. AS_QWORD : oper.typesize:=8;
  2514. AS_DQWORD : oper.typesize:=16;
  2515. AS_TBYTE : oper.typesize:=10;
  2516. AS_OWORD,
  2517. AS_XMMWORD: oper.typesize:=16;
  2518. AS_YWORD,
  2519. AS_YMMWORD: oper.typesize:=32;
  2520. AS_ZWORD,
  2521. AS_ZMMWORD: oper.typesize:=64;
  2522. else
  2523. internalerror(2010061101);
  2524. end;
  2525. Consume(actasmtoken);
  2526. if (actasmtoken=AS_LPAREN) then
  2527. begin
  2528. { Support "xxx ptr [Reference]" }
  2529. { in case the expression subscripts a record, the typesize
  2530. is reset, so save the explicit size we set above }
  2531. l:=oper.typesize;
  2532. Consume(AS_LPAREN);
  2533. BuildOperand(oper,true);
  2534. Consume(AS_RPAREN);
  2535. oper.setsize(l,true);
  2536. end;
  2537. end;
  2538. AS_SEPARATOR,
  2539. AS_END,
  2540. AS_COMMA,
  2541. AS_COLON:
  2542. begin
  2543. break;
  2544. end;
  2545. AS_RPAREN:
  2546. begin
  2547. if not istypecast then
  2548. begin
  2549. Message(asmr_e_syn_operand);
  2550. Consume(AS_RPAREN);
  2551. end
  2552. else
  2553. break;
  2554. end;
  2555. else
  2556. begin
  2557. Message(asmr_e_syn_operand);
  2558. RecoverConsume(true);
  2559. break;
  2560. end;
  2561. end;
  2562. until false;
  2563. { End of operand, update size if a typecast is forced }
  2564. if (oper.typesize<>0) and
  2565. (oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL]) then
  2566. oper.SetSize(oper.typesize,true);
  2567. {$ifdef i8086}
  2568. { references to a procedure/function entry, without an explicit segment
  2569. override, are added an CS: override by default (this is Turbo Pascal 7
  2570. compatible) }
  2571. if (oper.opr.typ=OPR_REFERENCE) and assigned(oper.opr.ref.symbol) and
  2572. (oper.opr.ref.symbol.typ=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  2573. oper.opr.ref.segment:=NR_CS;
  2574. {$endif i8086}
  2575. end;
  2576. Procedure tx86intreader.BuildOpCode(instr : tx86instruction);
  2577. var
  2578. PrefixOp,OverrideOp: tasmop;
  2579. operandnum : longint;
  2580. t: TRegister;
  2581. is_far_const:boolean;
  2582. i:byte;
  2583. tmp: toperand;
  2584. di_param, si_param: ShortInt;
  2585. prefix_or_override_pending_concat: boolean = false;
  2586. {$ifdef i8086}
  2587. hsymbol: TAsmSymbol;
  2588. hoffset: ASizeInt;
  2589. href_farproc_entry: Boolean;
  2590. {$endif i8086}
  2591. begin
  2592. PrefixOp:=A_None;
  2593. OverrideOp:=A_None;
  2594. is_far_const:=false;
  2595. { prefix seg opcode / prefix opcode }
  2596. repeat
  2597. if is_prefix(actopcode) then
  2598. with instr do
  2599. begin
  2600. if prefix_or_override_pending_concat then
  2601. ConcatInstruction(curlist);
  2602. PrefixOp:=ActOpcode;
  2603. opcode:=ActOpcode;
  2604. condition:=ActCondition;
  2605. opsize:=ActOpsize;
  2606. prefix_or_override_pending_concat:=true;
  2607. consume(AS_OPCODE);
  2608. end
  2609. else
  2610. if is_override(actopcode) then
  2611. with instr do
  2612. begin
  2613. if prefix_or_override_pending_concat then
  2614. ConcatInstruction(curlist);
  2615. OverrideOp:=ActOpcode;
  2616. opcode:=ActOpcode;
  2617. condition:=ActCondition;
  2618. opsize:=ActOpsize;
  2619. prefix_or_override_pending_concat:=true;
  2620. consume(AS_OPCODE);
  2621. end
  2622. else
  2623. break;
  2624. { allow for newline after prefix or override }
  2625. while actasmtoken=AS_SEPARATOR do
  2626. consume(AS_SEPARATOR);
  2627. until (actasmtoken<>AS_OPCODE);
  2628. { opcode }
  2629. if (actasmtoken <> AS_OPCODE) then
  2630. begin
  2631. { allow a prefix or override to be used standalone, like an opcode
  2632. with zero operands; this is TP7 compatible and allows compiling
  2633. ugly code like 'seges; db $67,$66; lodsw' }
  2634. if prefix_or_override_pending_concat then
  2635. exit
  2636. else
  2637. begin
  2638. Message(asmr_e_invalid_or_missing_opcode);
  2639. RecoverConsume(false);
  2640. exit;
  2641. end;
  2642. end;
  2643. if prefix_or_override_pending_concat then
  2644. begin
  2645. instr.ConcatInstruction(curlist);
  2646. prefix_or_override_pending_concat:=false;
  2647. end;
  2648. { Fill the instr object with the current state }
  2649. with instr do
  2650. begin
  2651. Opcode:=ActOpcode;
  2652. condition:=ActCondition;
  2653. opsize:=ActOpsize;
  2654. { Valid combination of prefix/override and instruction ? }
  2655. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  2656. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  2657. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  2658. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  2659. end;
  2660. { pushf/popf/pusha/popa have to default to 16 bit in Intel mode
  2661. (Intel manual and Delphi-compatbile) -- setting the opsize for
  2662. these instructions doesn't change anything in the internal assember,
  2663. so change the opcode }
  2664. if (instr.opcode=A_POPF) then
  2665. instr.opcode:=A_POPFW
  2666. else if (instr.opcode=A_PUSHF) then
  2667. instr.opcode:=A_PUSHFW
  2668. {$ifndef x86_64}
  2669. else if (instr.opcode=A_PUSHA) then
  2670. instr.opcode:=A_PUSHAW
  2671. else if (instr.opcode=A_POPA) then
  2672. instr.opcode:=A_POPAW
  2673. {$endif x86_64}
  2674. {$ifdef i8086}
  2675. { ret is converted to retn or retf, depending on the call model of the
  2676. current procedure (BP7 compatible) }
  2677. else if (instr.opcode=A_RET) then
  2678. begin
  2679. if is_proc_far(current_procinfo.procdef) and
  2680. not (po_interrupt in current_procinfo.procdef.procoptions) then
  2681. instr.opcode:=A_RETF
  2682. else
  2683. instr.opcode:=A_RETN;
  2684. end
  2685. {$endif i8086}
  2686. ;
  2687. { We are reading operands, so opcode will be an AS_ID }
  2688. { process operands backwards to get them in AT&T order }
  2689. operandnum:=max_operands;
  2690. is_far_const:=false;
  2691. Consume(AS_OPCODE);
  2692. { Zero operand opcode ? }
  2693. if actasmtoken in [AS_SEPARATOR,AS_END] then
  2694. exit;
  2695. { Read Operands }
  2696. repeat
  2697. case actasmtoken of
  2698. { End of asm operands for this opcode }
  2699. AS_END,
  2700. AS_SEPARATOR :
  2701. break;
  2702. { Operand delimiter }
  2703. AS_COMMA :
  2704. begin
  2705. { should have something before the comma }
  2706. if instr.operands[operandnum].opr.typ=OPR_NONE then
  2707. Message(asmr_e_syntax_error);
  2708. if operandnum <= 1 then
  2709. Message(asmr_e_too_many_operands)
  2710. else
  2711. Dec(operandnum);
  2712. Consume(AS_COMMA,instr.MightHaveExtension);
  2713. end;
  2714. {Far constant, i.e. jmp $0000:$11111111.}
  2715. AS_COLON:
  2716. begin
  2717. is_far_const:=true;
  2718. if operandnum<max_operands then
  2719. message(asmr_e_too_many_operands)
  2720. else
  2721. dec(operandnum);
  2722. consume(AS_COLON);
  2723. end;
  2724. { Type specifier }
  2725. AS_NEAR,
  2726. AS_FAR :
  2727. begin
  2728. if actasmtoken = AS_NEAR then
  2729. begin
  2730. {$ifndef i8086}
  2731. Message(asmr_w_near_ignored);
  2732. {$endif not i8086}
  2733. instr.opsize:=S_NEAR;
  2734. end
  2735. else
  2736. begin
  2737. {$ifndef i8086}
  2738. Message(asmr_w_far_ignored);
  2739. {$endif not i8086}
  2740. instr.opsize:=S_FAR;
  2741. end;
  2742. Consume(actasmtoken);
  2743. if actasmtoken=AS_PTR then
  2744. begin
  2745. Consume(AS_PTR);
  2746. instr.Operands[operandnum].InitRef;
  2747. end;
  2748. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2749. end;
  2750. AS_LOPSAE,
  2751. AS_LOPER:
  2752. if operandnum < max_operands then
  2753. begin
  2754. consume_voperand_ext(instr.Operands[operandnum + 1] as tx86operand, false);
  2755. if actasmtoken in [AS_OPSAE,AS_OPRNSAE,AS_OPRDSAE,AS_OPRUSAE,AS_OPRZSAE] then
  2756. begin
  2757. consume(actasmtoken);
  2758. // ignore operand
  2759. if actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA] then inc(operandnum)
  2760. else Message(asmr_e_syntax_error);
  2761. end
  2762. else Message(asmr_e_syntax_error);
  2763. end
  2764. else Message(asmr_e_syntax_error);
  2765. else
  2766. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2767. end; { end case }
  2768. until false;
  2769. { shift operands to start from 1, exchange to make sure they are destroyed correctly }
  2770. for i:=operandnum to max_operands do
  2771. begin
  2772. tmp:=instr.operands[i+1-operandnum];
  2773. instr.operands[i+1-operandnum]:=instr.operands[i];
  2774. instr.operands[i]:=tmp;
  2775. end;
  2776. operandnum:=(max_operands+1)-operandnum;
  2777. instr.ops:=operandnum;
  2778. { Check operands }
  2779. for i:=1 to operandnum do
  2780. begin
  2781. if is_far_const and
  2782. (instr.operands[i].opr.typ<>OPR_CONSTANT) then
  2783. message(asmr_e_expr_illegal)
  2784. else
  2785. if instr.operands[i].opr.typ=OPR_NONE then
  2786. Message(asmr_e_syntax_error);
  2787. end;
  2788. { Check for invalid ES: overrides }
  2789. if is_x86_parameterized_string_op(instr.opcode) then
  2790. begin
  2791. si_param:=get_x86_string_op_si_param(instr.opcode);
  2792. if si_param<>-1 then
  2793. si_param:=x86_parameterized_string_op_param_count(instr.opcode)-si_param;
  2794. di_param:=get_x86_string_op_di_param(instr.opcode);
  2795. if di_param<>-1 then
  2796. begin
  2797. di_param:=x86_parameterized_string_op_param_count(instr.opcode)-di_param;
  2798. if di_param<=operandnum then
  2799. with instr.operands[di_param] do
  2800. if (opr.typ=OPR_REFERENCE) and
  2801. (opr.ref.segment<>NR_NO) and
  2802. (opr.ref.segment<>NR_ES) then
  2803. Message(asmr_e_cannot_override_es_segment);
  2804. end;
  2805. { if two memory parameters, check whether their address sizes are equal }
  2806. if (si_param<>-1) and (di_param<>-1) and
  2807. (si_param<=operandnum) and (di_param<=operandnum) and
  2808. (instr.operands[si_param].opr.typ=OPR_REFERENCE) and
  2809. (instr.operands[di_param].opr.typ=OPR_REFERENCE) then
  2810. begin
  2811. if get_ref_address_size(instr.operands[si_param].opr.ref)<>
  2812. get_ref_address_size(instr.operands[di_param].opr.ref) then
  2813. Message(asmr_e_address_sizes_do_not_match);
  2814. end;
  2815. end;
  2816. { e.g. for "push dword 1", "push word 6" }
  2817. if (instr.ops=1) and
  2818. (instr.operands[1].typesize<>0) then
  2819. instr.operands[1].setsize(instr.operands[1].typesize,false);
  2820. {$ifdef i8086}
  2821. for i:=1 to operandnum do
  2822. with instr.operands[i].opr do
  2823. begin
  2824. { convert 'call/jmp [proc/label]' to 'call/jmp proc/label'. Ugly,
  2825. but Turbo Pascal 7 compatible. }
  2826. if (instr.opcode in [A_CALL,A_JMP]) and
  2827. (instr.operands[i].haslabelref or instr.operands[i].hasproc) and
  2828. (not instr.operands[i].hastype)
  2829. and (typ=OPR_REFERENCE) and
  2830. assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL,AT_ADDR]) and
  2831. (ref.base=NR_NO) and (ref.index=NR_NO) then
  2832. begin
  2833. hsymbol:=ref.symbol;
  2834. hoffset:=ref.offset;
  2835. href_farproc_entry:=ref_farproc_entry;
  2836. typ:=OPR_SYMBOL;
  2837. symbol:=hsymbol;
  2838. symofs:=hoffset;
  2839. symseg:=False;
  2840. sym_farproc_entry:=href_farproc_entry;
  2841. end;
  2842. { convert 'call/jmp symbol' to 'call/jmp far symbol' for symbols that are an entry point of a far procedure }
  2843. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2844. (typ=OPR_SYMBOL) and sym_farproc_entry then
  2845. instr.opsize:=S_FAR;
  2846. { convert 'call/jmp dword [something]' to 'call/jmp far [something]' (BP7 compatibility) }
  2847. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2848. (typ in [OPR_LOCAL,OPR_REFERENCE]) and (instr.operands[i].size=OS_32) then
  2849. instr.opsize:=S_FAR;
  2850. end;
  2851. {$endif i8086}
  2852. if (MemRefInfo(instr.opcode).ExistsSSEAVX) and
  2853. (MemRefInfo(instr.opcode).MemRefSize in MemRefSizeInfoVMems) then
  2854. begin
  2855. for i:=1 to operandnum do
  2856. begin
  2857. if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2858. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2859. (instr.operands[i].opr.ref.index = NR_NO) then
  2860. begin
  2861. instr.operands[i].opr.ref.index := instr.operands[i].opr.ref.base;
  2862. instr.operands[i].opr.ref.base := NR_NO;
  2863. end
  2864. else if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2865. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2866. (getregtype(instr.operands[i].opr.ref.index) = R_INTREGISTER) and
  2867. (getsubreg(instr.operands[i].opr.ref.index) = R_SUBADDR) then
  2868. begin
  2869. // exchange base- and index-register
  2870. // e.g. VGATHERDPD XMM0, [XMM1 + RAX], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1], XMM2
  2871. // e.g. VGATHERDPD XMM0, [XMM1 + RAX * 2], XMM2 =>> not supported
  2872. // e.g. VGATHERDPD XMM0, [XMM1 + RAX + 16], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1 + 16]
  2873. if instr.operands[i].opr.ref.scalefactor > 1 then Message(asmr_e_invalid_reference_syntax)
  2874. else
  2875. begin
  2876. t := instr.operands[i].opr.ref.base;
  2877. instr.operands[i].opr.ref.base := instr.operands[i].opr.ref.index;
  2878. instr.operands[i].opr.ref.index := t;
  2879. end;
  2880. end;
  2881. end;
  2882. end;
  2883. end;
  2884. Procedure tx86intreader.BuildConstant(constsize: byte);
  2885. var
  2886. asmsymtyp : tasmsymtype;
  2887. asmsym,
  2888. expr: string;
  2889. value,size : tcgint;
  2890. cse_out_flags : tconstsymbolexpressionoutputflags;
  2891. Begin
  2892. Repeat
  2893. Case actasmtoken of
  2894. AS_STRING:
  2895. Begin
  2896. { DD and DW cases }
  2897. if constsize <> 1 then
  2898. Begin
  2899. if Not PadZero(actasmpattern,constsize) then
  2900. Message(scan_f_string_exceeds_line);
  2901. end;
  2902. expr:=actasmpattern;
  2903. Consume(AS_STRING);
  2904. Case actasmtoken of
  2905. AS_COMMA:
  2906. Consume(AS_COMMA);
  2907. AS_END,
  2908. AS_SEPARATOR: ;
  2909. else
  2910. Message(asmr_e_invalid_string_expression);
  2911. end;
  2912. ConcatString(curlist,expr);
  2913. end;
  2914. AS_BYTE,
  2915. AS_WORD,
  2916. AS_DWORD,
  2917. AS_TBYTE,
  2918. AS_DQWORD,
  2919. AS_QWORD,
  2920. AS_OWORD,
  2921. AS_XMMWORD,
  2922. AS_YWORD,
  2923. AS_YMMWORD,
  2924. AS_PLUS,
  2925. AS_MINUS,
  2926. AS_LPAREN,
  2927. AS_NOT,
  2928. AS_INTNUM,
  2929. AS_OFFSET,
  2930. AS_LBRACKET,
  2931. {$ifdef i8086}
  2932. AS_SEG,
  2933. {$endif i8086}
  2934. AS_ID :
  2935. Begin
  2936. BuildConstSymbolExpression([cseif_referencelike],value,asmsym,asmsymtyp,size,cse_out_flags);
  2937. if asmsym<>'' then
  2938. begin
  2939. if not (cseof_isseg in cse_out_flags) and
  2940. {$ifdef i8086}
  2941. ((constsize<>2) and (constsize<>4))
  2942. {$else i8086}
  2943. (constsize<>sizeof(pint))
  2944. {$endif i8086}
  2945. then
  2946. begin
  2947. Message1(asmr_w_const32bit_for_address,asmsym);
  2948. constsize:=sizeof(pint);
  2949. end;
  2950. {$ifdef i8086}
  2951. if asmsym='@DATA' then
  2952. begin
  2953. if not (cseof_isseg in cse_out_flags) then
  2954. Message(asmr_e_CODE_or_DATA_without_SEG);
  2955. if constsize<2 then
  2956. Message1(asmr_e_const16bit_for_segment,asmsym);
  2957. if current_settings.x86memorymodel=mm_huge then
  2958. curlist.concat(Tai_const.Create_fardataseg)
  2959. else
  2960. curlist.concat(Tai_const.Create_dgroup);
  2961. if constsize>2 then
  2962. ConcatConstant(curlist,0,constsize-2);
  2963. end
  2964. else if asmsym='@CODE' then
  2965. begin
  2966. if not (cseof_isseg in cse_out_flags) then
  2967. Message(asmr_e_CODE_or_DATA_without_SEG);
  2968. if constsize<2 then
  2969. Message1(asmr_e_const16bit_for_segment,asmsym);
  2970. curlist.concat(Tai_const.Create_seg_name(current_procinfo.procdef.mangledname));
  2971. if constsize>2 then
  2972. ConcatConstant(curlist,0,constsize-2);
  2973. end
  2974. else if cseof_isseg in cse_out_flags then
  2975. begin
  2976. if constsize<2 then
  2977. Message1(asmr_e_const16bit_for_segment,asmsym);
  2978. curlist.concat(Tai_const.Create_seg_name(asmsym));
  2979. if constsize>2 then
  2980. ConcatConstant(curlist,0,constsize-2);
  2981. end
  2982. else
  2983. {$endif i8086}
  2984. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value,constsize,cseof_hasofs in cse_out_flags);
  2985. end
  2986. else
  2987. ConcatConstant(curlist,value,constsize);
  2988. end;
  2989. AS_COMMA:
  2990. begin
  2991. Consume(AS_COMMA);
  2992. end;
  2993. AS_ALIGN,
  2994. AS_DB,
  2995. AS_DW,
  2996. AS_DD,
  2997. AS_DQ,
  2998. AS_OPCODE,
  2999. AS_END,
  3000. AS_SEPARATOR:
  3001. break;
  3002. else
  3003. begin
  3004. Message(asmr_e_syn_constant);
  3005. RecoverConsume(false);
  3006. end
  3007. end;
  3008. Until false;
  3009. end;
  3010. function tx86intreader.Assemble: tlinkedlist;
  3011. Var
  3012. hl : tasmlabel;
  3013. instr : Tx86Instruction;
  3014. tmpsym: tsym;
  3015. tmpsrsymtable: TSymtable;
  3016. Begin
  3017. Message1(asmr_d_start_reading,'intel');
  3018. inexpression:=FALSE;
  3019. firsttoken:=TRUE;
  3020. { sets up all opcode and register tables in uppercase
  3021. done in the construtor now
  3022. if not _asmsorted then
  3023. Begin
  3024. SetupTables;
  3025. _asmsorted:=TRUE;
  3026. end;
  3027. }
  3028. curlist:=TAsmList.Create;
  3029. { we might need to know which parameters are passed in registers }
  3030. if not parse_generic then
  3031. current_procinfo.generate_parameter_info;
  3032. { start tokenizer }
  3033. gettoken;
  3034. { main loop }
  3035. repeat
  3036. case actasmtoken of
  3037. AS_LLABEL:
  3038. Begin
  3039. if CreateLocalLabel(actasmpattern,hl,true) then
  3040. ConcatLabel(curlist,hl);
  3041. Consume(AS_LLABEL);
  3042. end;
  3043. AS_LABEL:
  3044. Begin
  3045. if SearchLabel(upper(actasmpattern),hl,true) then
  3046. begin
  3047. if hl.is_public then
  3048. ConcatPublic(curlist,actasmpattern_origcase);
  3049. ConcatLabel(curlist,hl);
  3050. end
  3051. else
  3052. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  3053. Consume(AS_LABEL);
  3054. end;
  3055. AS_DW :
  3056. Begin
  3057. inexpression:=true;
  3058. Consume(AS_DW);
  3059. BuildConstant(2);
  3060. inexpression:=false;
  3061. end;
  3062. AS_DB :
  3063. Begin
  3064. inexpression:=true;
  3065. Consume(AS_DB);
  3066. BuildConstant(1);
  3067. inexpression:=false;
  3068. end;
  3069. AS_DD :
  3070. Begin
  3071. inexpression:=true;
  3072. Consume(AS_DD);
  3073. BuildConstant(4);
  3074. inexpression:=false;
  3075. end;
  3076. {$ifdef cpu64bitaddr}
  3077. AS_DQ:
  3078. Begin
  3079. inexpression:=true;
  3080. Consume(AS_DQ);
  3081. BuildConstant(8);
  3082. inexpression:=false;
  3083. end;
  3084. {$endif cpu64bitaddr}
  3085. AS_PUBLIC:
  3086. Begin
  3087. Consume(AS_PUBLIC);
  3088. repeat
  3089. if actasmtoken=AS_ID then
  3090. begin
  3091. if (actasmpattern<>'') and (actasmpattern[1]='@') then
  3092. Message1(asmr_e_local_label_cannot_be_declared_public,actasmpattern)
  3093. else if SearchLabel(upper(actasmpattern),hl,false) then
  3094. begin
  3095. if not hl.is_public then
  3096. begin
  3097. hl.is_public:=true;
  3098. asmsearchsym(upper(actasmpattern),tmpsym,tmpsrsymtable);
  3099. if tlabelsym(tmpsym).defined then
  3100. Message1(asmr_e_public_must_be_used_before_label_definition,actasmpattern);
  3101. end;
  3102. end
  3103. else
  3104. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  3105. end;
  3106. Consume(AS_ID);
  3107. if actasmtoken=AS_COMMA then
  3108. begin
  3109. Consume(AS_COMMA);
  3110. if actasmtoken<>AS_ID then
  3111. Consume(AS_ID);
  3112. end;
  3113. until actasmtoken=AS_SEPARATOR;
  3114. end;
  3115. AS_ALIGN:
  3116. Begin
  3117. Consume(AS_ALIGN);
  3118. ConcatAlign(curlist,BuildConstExpression);
  3119. if actasmtoken<>AS_SEPARATOR then
  3120. Consume(AS_SEPARATOR);
  3121. end;
  3122. AS_OPCODE :
  3123. Begin
  3124. instr:=Tx86Instruction.Create(Tx86Operand);
  3125. BuildOpcode(instr);
  3126. with instr do
  3127. begin
  3128. CheckNonCommutativeOpcodes;
  3129. AddReferenceSizes;
  3130. SetInstructionOpsize;
  3131. CheckOperandSizes;
  3132. ConcatInstruction(curlist);
  3133. end;
  3134. instr.Free;
  3135. end;
  3136. AS_SEPARATOR :
  3137. Begin
  3138. Consume(AS_SEPARATOR);
  3139. end;
  3140. AS_TARGET_DIRECTIVE:
  3141. HandleTargetDirective;
  3142. AS_END :
  3143. break; { end assembly block }
  3144. else
  3145. Begin
  3146. Message(asmr_e_syntax_error);
  3147. RecoverConsume(false);
  3148. end;
  3149. end; { end case }
  3150. until false;
  3151. { check that all referenced local labels are defined }
  3152. checklocallabels;
  3153. { Return the list in an asmnode }
  3154. assemble:=curlist;
  3155. Message1(asmr_d_finish_reading,'intel');
  3156. end;
  3157. end.