rax86int.pas 121 KB

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