rax86int.pas 109 KB

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