rax86int.pas 122 KB

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