rax86int.pas 122 KB

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