rax86int.pas 122 KB

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