rax86int.pas 121 KB

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