rax86int.pas 120 KB

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