rax86int.pas 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019
  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_LBRACKET,AS_RBRACKET]) then
  989. case actasmtoken of
  990. AS_LBRACKET:
  991. begin
  992. Consume(AS_LBRACKET);
  993. if (length(expr)>0) and
  994. not (expr[length(expr)] in ['+','-']) then
  995. expr:=expr+'+';
  996. expr:=expr+'[';
  997. end;
  998. AS_RBRACKET:
  999. begin
  1000. Consume(AS_RBRACKET);
  1001. expr:=expr+']';
  1002. end;
  1003. end;
  1004. Case actasmtoken of
  1005. AS_LPAREN:
  1006. Begin
  1007. Consume(AS_LPAREN);
  1008. expr:=expr + '(';
  1009. inc(parenlevel);
  1010. end;
  1011. AS_RPAREN:
  1012. Begin
  1013. { Keep the AS_PAREN in actasmtoken, it is maybe a typecast }
  1014. if parenlevel=0 then
  1015. break;
  1016. Consume(AS_RPAREN);
  1017. expr:=expr + ')';
  1018. dec(parenlevel);
  1019. end;
  1020. AS_SHL:
  1021. Begin
  1022. Consume(AS_SHL);
  1023. expr:=expr + '<';
  1024. end;
  1025. AS_SHR:
  1026. Begin
  1027. Consume(AS_SHR);
  1028. expr:=expr + '>';
  1029. end;
  1030. AS_SLASH:
  1031. Begin
  1032. Consume(AS_SLASH);
  1033. expr:=expr + '/';
  1034. end;
  1035. AS_MOD:
  1036. Begin
  1037. Consume(AS_MOD);
  1038. expr:=expr + '%';
  1039. end;
  1040. AS_STAR:
  1041. Begin
  1042. Consume(AS_STAR);
  1043. if (cseif_isref in in_flags) and (actasmtoken=AS_REGISTER) then
  1044. break;
  1045. expr:=expr + '*';
  1046. end;
  1047. AS_PLUS:
  1048. Begin
  1049. Consume(AS_PLUS);
  1050. if (cseif_isref in in_flags) and ((actasmtoken=AS_REGISTER) or (actasmtoken=AS_LBRACKET)) then
  1051. break;
  1052. expr:=expr + '+';
  1053. end;
  1054. AS_MINUS:
  1055. Begin
  1056. Consume(AS_MINUS);
  1057. expr:=expr + '-';
  1058. end;
  1059. AS_AND:
  1060. Begin
  1061. Consume(AS_AND);
  1062. expr:=expr + '&';
  1063. end;
  1064. AS_NOT:
  1065. Begin
  1066. Consume(AS_NOT);
  1067. expr:=expr + '~';
  1068. end;
  1069. AS_XOR:
  1070. Begin
  1071. Consume(AS_XOR);
  1072. expr:=expr + '^';
  1073. end;
  1074. AS_OR:
  1075. Begin
  1076. Consume(AS_OR);
  1077. expr:=expr + '|';
  1078. end;
  1079. AS_INTNUM:
  1080. Begin
  1081. expr:=expr + actasmpattern;
  1082. Consume(AS_INTNUM);
  1083. end;
  1084. {$ifdef i8086}
  1085. AS_SEG:
  1086. begin
  1087. include(out_flags,cseof_isseg);
  1088. Consume(actasmtoken);
  1089. if actasmtoken<>AS_ID then
  1090. Message(asmr_e_seg_without_identifier);
  1091. end;
  1092. {$endif i8086}
  1093. AS_VMTOFFSET,
  1094. AS_OFFSET:
  1095. begin
  1096. if (actasmtoken = AS_OFFSET) then
  1097. begin
  1098. include(in_flags,cseif_needofs);
  1099. include(out_flags,cseof_hasofs);
  1100. end
  1101. else
  1102. needvmtofs:=true;
  1103. Consume(actasmtoken);
  1104. if actasmtoken<>AS_ID then
  1105. Message(asmr_e_offset_without_identifier);
  1106. end;
  1107. AS_SIZEOF,
  1108. AS_TYPE:
  1109. begin
  1110. l:=0;
  1111. hasparen:=false;
  1112. Consume(actasmtoken);
  1113. if actasmtoken=AS_LPAREN then
  1114. begin
  1115. hasparen:=true;
  1116. Consume(AS_LPAREN);
  1117. end;
  1118. if actasmtoken<>AS_ID then
  1119. Message(asmr_e_type_without_identifier)
  1120. else
  1121. begin
  1122. tempstr:=actasmpattern;
  1123. Consume(AS_ID);
  1124. if actasmtoken=AS_DOT then
  1125. begin
  1126. BuildRecordOffsetSize(tempstr,k,l,mangledname,false,hastypecast);
  1127. if mangledname<>'' then
  1128. { procsym }
  1129. Message(asmr_e_wrong_sym_type);
  1130. if hastypecast then
  1131. end
  1132. else
  1133. begin
  1134. asmsearchsym(tempstr,sym,srsymtable);
  1135. if assigned(sym) then
  1136. begin
  1137. case sym.typ of
  1138. staticvarsym,
  1139. localvarsym,
  1140. paravarsym :
  1141. l:=tabstractvarsym(sym).getsize;
  1142. typesym :
  1143. l:=ttypesym(sym).typedef.size;
  1144. else
  1145. Message(asmr_e_wrong_sym_type);
  1146. end;
  1147. end
  1148. else
  1149. Message1(sym_e_unknown_id,tempstr);
  1150. end;
  1151. end;
  1152. str(l, tempstr);
  1153. expr:=expr + tempstr;
  1154. if hasparen then
  1155. Consume(AS_RPAREN);
  1156. end;
  1157. AS_PTR :
  1158. begin
  1159. { Support ugly delphi constructs like <constant> PTR [ref] }
  1160. break;
  1161. end;
  1162. AS_STRING:
  1163. begin
  1164. l:=0;
  1165. case Length(actasmpattern) of
  1166. 1 :
  1167. l:=ord(actasmpattern[1]);
  1168. 2 :
  1169. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  1170. 3 :
  1171. l:=ord(actasmpattern[3]) +
  1172. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  1173. 4 :
  1174. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1175. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1176. else
  1177. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1178. end;
  1179. str(l, tempstr);
  1180. expr:=expr + tempstr;
  1181. Consume(AS_STRING);
  1182. end;
  1183. AS_ID:
  1184. begin
  1185. hs:='';
  1186. hssymtyp:=AT_DATA;
  1187. def:=nil;
  1188. tempstr:=actasmpattern;
  1189. prevtok:=prevasmtoken;
  1190. { stop parsing a constant expression if we find an opcode after a
  1191. non-operator like "db $66 mov eax,ebx" }
  1192. if (prevtok in [AS_ID,AS_INTNUM,AS_RPAREN]) and
  1193. is_asmopcode(actasmpattern) then
  1194. break;
  1195. consume(AS_ID);
  1196. if (tempstr='@CODE') or (tempstr='@DATA') then
  1197. begin
  1198. if asmsym='' then
  1199. begin
  1200. asmsym:=tempstr;
  1201. asmsymtyp:=AT_SECTION;
  1202. end
  1203. else
  1204. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1205. end
  1206. else if SearchIConstant(tempstr,l) then
  1207. begin
  1208. str(l, tempstr);
  1209. expr:=expr + tempstr;
  1210. end
  1211. else
  1212. begin
  1213. if is_locallabel(tempstr) then
  1214. begin
  1215. CreateLocalLabel(tempstr,hl,false);
  1216. hs:=hl.name;
  1217. hssymtyp:=AT_FUNCTION;
  1218. end
  1219. else
  1220. if SearchLabel(tempstr,hl,false) then
  1221. begin
  1222. hs:=hl.name;
  1223. hssymtyp:=AT_FUNCTION;
  1224. end
  1225. else
  1226. begin
  1227. asmsearchsym(tempstr,sym,srsymtable);
  1228. if assigned(sym) then
  1229. begin
  1230. case sym.typ of
  1231. staticvarsym :
  1232. begin
  1233. hs:=tstaticvarsym(sym).mangledname;
  1234. def:=tstaticvarsym(sym).vardef;
  1235. end;
  1236. localvarsym,
  1237. paravarsym :
  1238. begin
  1239. Message(asmr_e_no_local_or_para_allowed);
  1240. end;
  1241. procsym :
  1242. begin
  1243. if Tprocsym(sym).ProcdefList.Count>1 then
  1244. Message(asmr_w_calling_overload_func);
  1245. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  1246. {$ifdef i8086}
  1247. if is_proc_far(tprocdef(tprocsym(sym).ProcdefList[0]))
  1248. and not (po_interrupt in tprocdef(tprocsym(sym).ProcdefList[0]).procoptions) then
  1249. include(out_flags,cseof_is_farproc_entry)
  1250. else
  1251. exclude(out_flags,cseof_is_farproc_entry);
  1252. {$endif i8086}
  1253. hssymtyp:=AT_FUNCTION;
  1254. end;
  1255. typesym :
  1256. begin
  1257. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1258. Message(asmr_e_wrong_sym_type);
  1259. size:=ttypesym(sym).typedef.size;
  1260. end;
  1261. fieldvarsym :
  1262. begin
  1263. tempstr:=upper(tdef(sym.owner.defowner).GetTypeName)+'.'+tempstr;
  1264. end;
  1265. else
  1266. Message(asmr_e_wrong_sym_type);
  1267. end;
  1268. end
  1269. else
  1270. Message1(sym_e_unknown_id,tempstr);
  1271. end;
  1272. { symbol found? }
  1273. if hs<>'' then
  1274. begin
  1275. if asmsym='' then
  1276. begin
  1277. asmsym:=hs;
  1278. asmsymtyp:=hssymtyp;
  1279. end
  1280. else
  1281. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1282. if (expr='') or (expr[length(expr)]='+') then
  1283. begin
  1284. { don't remove the + if there could be a record field }
  1285. if actasmtoken<>AS_DOT then
  1286. delete(expr,length(expr),1);
  1287. end
  1288. else
  1289. if (cseif_needofs in in_flags) then
  1290. begin
  1291. if (prevtok<>AS_OFFSET) then
  1292. Message(asmr_e_need_offset);
  1293. end
  1294. else
  1295. Message(asmr_e_only_add_relocatable_symbol);
  1296. end;
  1297. if (actasmtoken=AS_DOT) or
  1298. (assigned(sym) and
  1299. (sym.typ = fieldvarsym) and
  1300. not(sp_static in sym.symoptions)) then
  1301. begin
  1302. BuildRecordOffsetSize(tempstr,l,size,hs,needvmtofs,hastypecast);
  1303. if hs <> '' then
  1304. hssymtyp:=AT_FUNCTION
  1305. else
  1306. begin
  1307. str(l, tempstr);
  1308. expr:=expr + tempstr;
  1309. end
  1310. end
  1311. else if (actasmtoken<>AS_DOT) and
  1312. assigned(sym) and
  1313. (sym.typ=typesym) and
  1314. (ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  1315. begin
  1316. { just a record type (without being followed by dot)
  1317. evaluates to 0. Ugly, but TP7 compatible. }
  1318. expr:=expr+'0';
  1319. end
  1320. else
  1321. begin
  1322. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1323. delete(expr,length(expr),1);
  1324. end;
  1325. if (actasmtoken=AS_LBRACKET) and
  1326. assigned(def) and
  1327. (def.typ=arraydef) then
  1328. begin
  1329. consume(AS_LBRACKET);
  1330. l:=BuildConstExpression;
  1331. if l<tarraydef(def).lowrange then
  1332. begin
  1333. Message(asmr_e_constant_out_of_bounds);
  1334. l:=0;
  1335. end
  1336. else
  1337. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  1338. str(l, tempstr);
  1339. expr:=expr + '+' + tempstr;
  1340. consume(AS_RBRACKET);
  1341. end;
  1342. end;
  1343. { check if there are wrong operator used like / or mod etc. }
  1344. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  1345. Message(asmr_e_only_add_relocatable_symbol);
  1346. end;
  1347. AS_ALIGN,
  1348. AS_DB,
  1349. AS_DW,
  1350. AS_DD,
  1351. AS_DQ,
  1352. AS_END,
  1353. AS_RBRACKET,
  1354. AS_SEPARATOR,
  1355. AS_COMMA,
  1356. AS_COLON:
  1357. break;
  1358. else
  1359. begin
  1360. { write error only once. }
  1361. if not errorflag then
  1362. Message(asmr_e_invalid_constant_expression);
  1363. { consume tokens until we find COMMA or SEPARATOR }
  1364. Consume(actasmtoken);
  1365. errorflag:=TRUE;
  1366. end;
  1367. end;
  1368. Until false;
  1369. { calculate expression }
  1370. if not ErrorFlag then
  1371. value:=CalculateExpression(expr)
  1372. else
  1373. value:=0;
  1374. { no longer in an expression }
  1375. inexpression:=FALSE;
  1376. end;
  1377. Function tx86intreader.BuildConstExpression:aint;
  1378. var
  1379. l,size : tcgint;
  1380. hs : string;
  1381. hssymtyp : TAsmsymtype;
  1382. out_flags : tconstsymbolexpressionoutputflags;
  1383. begin
  1384. BuildConstSymbolExpression([],l,hs,hssymtyp,size,out_flags);
  1385. if hs<>'' then
  1386. Message(asmr_e_relocatable_symbol_not_allowed);
  1387. BuildConstExpression:=l;
  1388. end;
  1389. Function tx86intreader.BuildRefConstExpression(out size:tcgint;startingminus:boolean):aint;
  1390. var
  1391. l : tcgint;
  1392. hs : string;
  1393. hssymtyp : TAsmsymtype;
  1394. in_flags : tconstsymbolexpressioninputflags;
  1395. out_flags : tconstsymbolexpressionoutputflags;
  1396. begin
  1397. in_flags:=[cseif_isref];
  1398. if startingminus then
  1399. include(in_flags,cseif_startingminus);
  1400. BuildConstSymbolExpression(in_flags,l,hs,hssymtyp,size,out_flags);
  1401. if hs<>'' then
  1402. Message(asmr_e_relocatable_symbol_not_allowed);
  1403. BuildRefConstExpression:=l;
  1404. end;
  1405. procedure tx86intreader.BuildReference(oper : tx86operand);
  1406. var
  1407. scale : byte;
  1408. k,l,size : tcgint;
  1409. tempstr,hs : string;
  1410. tempsymtyp : tasmsymtype;
  1411. code : integer;
  1412. hreg : tregister;
  1413. GotStar,GotOffset,HadVar,
  1414. GotPlus,Negative,BracketlessReference : boolean;
  1415. hl : tasmlabel;
  1416. hastypecast: boolean;
  1417. tmpoper: tx86operand;
  1418. cse_in_flags: tconstsymbolexpressioninputflags;
  1419. cse_out_flags: tconstsymbolexpressionoutputflags;
  1420. Begin
  1421. if actasmtoken=AS_LBRACKET then
  1422. begin
  1423. Consume(AS_LBRACKET);
  1424. BracketlessReference:=false;
  1425. end
  1426. else
  1427. BracketlessReference:=true;
  1428. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  1429. oper.InitRef;
  1430. GotStar:=false;
  1431. GotPlus:=true;
  1432. GotOffset:=false;
  1433. Negative:=false;
  1434. Scale:=0;
  1435. repeat
  1436. if GotOffset and (actasmtoken<>AS_ID) then
  1437. Message(asmr_e_invalid_reference_syntax);
  1438. Case actasmtoken of
  1439. AS_ID, { Constant reference expression OR variable reference expression }
  1440. AS_VMTOFFSET:
  1441. Begin
  1442. if not GotPlus then
  1443. Message(asmr_e_invalid_reference_syntax);
  1444. GotStar:=false;
  1445. GotPlus:=false;
  1446. if (actasmtoken = AS_VMTOFFSET) or
  1447. (SearchIConstant(actasmpattern,l) or
  1448. SearchRecordType(actasmpattern)) then
  1449. begin
  1450. l:=BuildRefConstExpression(size,negative);
  1451. if size<>0 then
  1452. oper.SetSize(size,false);
  1453. negative:=false; { "l" was negated if necessary }
  1454. GotPlus:=(prevasmtoken=AS_PLUS);
  1455. GotStar:=(prevasmtoken=AS_STAR);
  1456. case oper.opr.typ of
  1457. OPR_LOCAL :
  1458. begin
  1459. if GotStar then
  1460. Message(asmr_e_invalid_reference_syntax);
  1461. Inc(oper.opr.localsymofs,l);
  1462. end;
  1463. OPR_REFERENCE :
  1464. begin
  1465. if GotStar then
  1466. oper.opr.ref.scalefactor:=l
  1467. else
  1468. Inc(oper.opr.ref.offset,l);
  1469. end;
  1470. end;
  1471. end
  1472. else
  1473. Begin
  1474. if negative and not oper.hasvar then
  1475. Message(asmr_e_only_add_relocatable_symbol)
  1476. else if oper.hasvar and not GotOffset and
  1477. (not negative or assigned(oper.opr.ref.relsymbol)) then
  1478. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1479. HadVar:=oper.hasvar and GotOffset;
  1480. tempstr:=actasmpattern;
  1481. Consume(AS_ID);
  1482. { typecasting? }
  1483. if (actasmtoken=AS_LPAREN) and
  1484. SearchType(tempstr,l) then
  1485. begin
  1486. oper.hastype:=true;
  1487. oper.typesize:=l;
  1488. Consume(AS_LPAREN);
  1489. BuildOperand(oper,true);
  1490. Consume(AS_RPAREN);
  1491. end
  1492. else
  1493. if is_locallabel(tempstr) then
  1494. begin
  1495. CreateLocalLabel(tempstr,hl,false);
  1496. oper.InitRef;
  1497. oper.haslabelref:=true;
  1498. if not negative then
  1499. begin
  1500. oper.opr.ref.symbol:=hl;
  1501. oper.hasvar:=true;
  1502. end
  1503. else
  1504. oper.opr.ref.relsymbol:=hl;
  1505. {$ifdef i8086}
  1506. if oper.opr.ref.segment=NR_NO then
  1507. oper.opr.ref.segment:=NR_CS;
  1508. {$endif i8086}
  1509. end
  1510. else
  1511. if oper.SetupVar(tempstr,GotOffset) then
  1512. begin
  1513. { convert OPR_LOCAL register para into a reference base }
  1514. if (oper.opr.typ=OPR_LOCAL) and
  1515. AsmRegisterPara(oper.opr.localsym) then
  1516. oper.InitRefConvertLocal
  1517. else
  1518. begin
  1519. {$ifdef x86_64}
  1520. if actasmtoken=AS_WRT then
  1521. begin
  1522. if (oper.opr.typ=OPR_REFERENCE) then
  1523. begin
  1524. Consume(AS_WRT);
  1525. Consume(AS___GOTPCREL);
  1526. if (oper.opr.ref.base<>NR_NO) or
  1527. (oper.opr.ref.index<>NR_NO) or
  1528. (oper.opr.ref.offset<>0) then
  1529. Message(asmr_e_wrong_gotpcrel_intel_syntax);
  1530. if tf_no_pic_supported in target_info.flags then
  1531. Message(asmr_e_no_gotpcrel_support);
  1532. oper.opr.ref.refaddr:=addr_pic;
  1533. oper.opr.ref.base:=NR_RIP;
  1534. end
  1535. else
  1536. message(asmr_e_invalid_reference_syntax);
  1537. end;
  1538. {$endif x86_64}
  1539. end;
  1540. end
  1541. else
  1542. Message1(sym_e_unknown_id,tempstr);
  1543. { record.field ? }
  1544. if actasmtoken=AS_DOT then
  1545. begin
  1546. BuildRecordOffsetSize(tempstr,l,k,hs,false,hastypecast);
  1547. if (hs<>'') then
  1548. Message(asmr_e_invalid_symbol_ref);
  1549. case oper.opr.typ of
  1550. OPR_LOCAL :
  1551. inc(oper.opr.localsymofs,l);
  1552. OPR_REFERENCE :
  1553. inc(oper.opr.ref.offset,l);
  1554. end;
  1555. if hastypecast then
  1556. oper.hastype:=true;
  1557. oper.SetSize(k,false);
  1558. end;
  1559. if GotOffset then
  1560. begin
  1561. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1562. begin
  1563. if (oper.opr.typ=OPR_REFERENCE) then
  1564. oper.opr.ref.base:=NR_NO;
  1565. oper.hasvar:=hadvar;
  1566. end
  1567. else
  1568. begin
  1569. if oper.hasvar and hadvar then
  1570. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1571. { should we allow ?? }
  1572. end;
  1573. end;
  1574. end;
  1575. GotOffset:=false;
  1576. end;
  1577. AS_PLUS :
  1578. Begin
  1579. Consume(AS_PLUS);
  1580. Negative:=false;
  1581. GotPlus:=true;
  1582. GotStar:=false;
  1583. Scale:=0;
  1584. end;
  1585. AS_DOT :
  1586. Begin
  1587. { Handle like a + }
  1588. Consume(AS_DOT);
  1589. Negative:=false;
  1590. GotPlus:=true;
  1591. GotStar:=false;
  1592. Scale:=0;
  1593. end;
  1594. AS_MINUS :
  1595. begin
  1596. Consume(AS_MINUS);
  1597. Negative:=true;
  1598. GotPlus:=true;
  1599. GotStar:=false;
  1600. Scale:=0;
  1601. end;
  1602. AS_STAR : { Scaling, with eax*4 order }
  1603. begin
  1604. Consume(AS_STAR);
  1605. hs:='';
  1606. l:=0;
  1607. case actasmtoken of
  1608. AS_ID,
  1609. AS_LPAREN :
  1610. l:=BuildConstExpression;
  1611. AS_INTNUM:
  1612. Begin
  1613. hs:=actasmpattern;
  1614. Consume(AS_INTNUM);
  1615. end;
  1616. AS_REGISTER :
  1617. begin
  1618. case oper.opr.typ of
  1619. OPR_REFERENCE :
  1620. begin
  1621. if oper.opr.ref.scalefactor=0 then
  1622. begin
  1623. if scale<>0 then
  1624. begin
  1625. oper.opr.ref.scalefactor:=scale;
  1626. scale:=0;
  1627. end
  1628. else
  1629. Message(asmr_e_wrong_scale_factor);
  1630. end
  1631. else
  1632. Message(asmr_e_invalid_reference_syntax);
  1633. end;
  1634. OPR_LOCAL :
  1635. begin
  1636. if oper.opr.localscale=0 then
  1637. begin
  1638. if scale<>0 then
  1639. begin
  1640. oper.opr.localscale:=scale;
  1641. scale:=0;
  1642. end
  1643. else
  1644. Message(asmr_e_wrong_scale_factor);
  1645. end
  1646. else
  1647. Message(asmr_e_invalid_reference_syntax);
  1648. end;
  1649. end;
  1650. end;
  1651. else
  1652. Message(asmr_e_invalid_reference_syntax);
  1653. end;
  1654. if actasmtoken<>AS_REGISTER then
  1655. begin
  1656. if hs<>'' then
  1657. val(hs,l,code);
  1658. case oper.opr.typ of
  1659. OPR_REFERENCE :
  1660. oper.opr.ref.scalefactor:=l;
  1661. OPR_LOCAL :
  1662. oper.opr.localscale:=l;
  1663. end;
  1664. if l>9 then
  1665. Message(asmr_e_wrong_scale_factor);
  1666. end;
  1667. GotPlus:=false;
  1668. GotStar:=false;
  1669. end;
  1670. AS_REGISTER :
  1671. begin
  1672. hreg:=actasmregister;
  1673. Consume(AS_REGISTER);
  1674. if actasmtoken=AS_COLON then
  1675. begin
  1676. Consume(AS_COLON);
  1677. oper.InitRefConvertLocal;
  1678. SetSegmentOverride(oper,hreg);
  1679. end
  1680. else
  1681. begin
  1682. if not((GotPlus and (not Negative)) or
  1683. GotStar) then
  1684. Message(asmr_e_invalid_reference_syntax);
  1685. { this register will be the index:
  1686. 1. just read a *
  1687. 2. next token is a *
  1688. 3. base register is already used }
  1689. case oper.opr.typ of
  1690. OPR_LOCAL :
  1691. begin
  1692. if (oper.opr.localindexreg<>NR_NO) then
  1693. Message(asmr_e_multiple_index);
  1694. {$ifdef x86_64}
  1695. { Locals/parameters cannot be accessed RIP-relative. Need a dedicated error message here? }
  1696. if (hreg=NR_RIP) then
  1697. Message(asmr_e_no_local_or_para_allowed);
  1698. {$endif x86_64}
  1699. oper.opr.localindexreg:=hreg;
  1700. if scale<>0 then
  1701. begin
  1702. oper.opr.localscale:=scale;
  1703. scale:=0;
  1704. end;
  1705. end;
  1706. OPR_REFERENCE :
  1707. begin
  1708. if (GotStar) or
  1709. (actasmtoken=AS_STAR) or
  1710. (oper.opr.ref.base<>NR_NO) then
  1711. begin
  1712. if (oper.opr.ref.index<>NR_NO) then
  1713. Message(asmr_e_multiple_index);
  1714. oper.opr.ref.index:=hreg;
  1715. if scale<>0 then
  1716. begin
  1717. oper.opr.ref.scalefactor:=scale;
  1718. scale:=0;
  1719. end;
  1720. end
  1721. else
  1722. begin
  1723. oper.opr.ref.base:=hreg;
  1724. {$ifdef x86_64}
  1725. { non-GOT based RIP-relative accesses are also position-independent }
  1726. if (oper.opr.ref.base=NR_RIP) and
  1727. (oper.opr.ref.refaddr<>addr_pic) then
  1728. oper.opr.ref.refaddr:=addr_pic_no_got;
  1729. {$endif x86_64}
  1730. end;
  1731. end;
  1732. end;
  1733. GotPlus:=false;
  1734. GotStar:=false;
  1735. end;
  1736. end;
  1737. AS_OFFSET :
  1738. begin
  1739. Consume(AS_OFFSET);
  1740. GotOffset:=true;
  1741. end;
  1742. AS_TYPE,
  1743. AS_NOT,
  1744. AS_STRING,
  1745. AS_INTNUM,
  1746. AS_LPAREN : { Constant reference expression }
  1747. begin
  1748. if not GotPlus and not GotStar then
  1749. Message(asmr_e_invalid_reference_syntax);
  1750. cse_in_flags:=[cseif_needofs,cseif_isref];
  1751. if GotPlus and negative then
  1752. include(cse_in_flags,cseif_startingminus);
  1753. BuildConstSymbolExpression(cse_in_flags,l,tempstr,tempsymtyp,size,cse_out_flags);
  1754. { already handled by BuildConstSymbolExpression(); must be
  1755. handled there to avoid [reg-1+1] being interpreted as
  1756. [reg-(1+1)] }
  1757. negative:=false;
  1758. if tempstr<>'' then
  1759. begin
  1760. if GotStar then
  1761. Message(asmr_e_only_add_relocatable_symbol);
  1762. if not assigned(oper.opr.ref.symbol) then
  1763. begin
  1764. oper.opr.ref.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1765. {$ifdef i8086}
  1766. if cseof_isseg in cse_out_flags then
  1767. begin
  1768. if not (oper.opr.ref.refaddr in [addr_fardataseg,addr_dgroup]) then
  1769. oper.opr.ref.refaddr:=addr_seg;
  1770. end
  1771. else if (tempsymtyp=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  1772. oper.opr.ref.segment:=NR_CS;
  1773. {$endif i8086}
  1774. end
  1775. else
  1776. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1777. end;
  1778. case oper.opr.typ of
  1779. OPR_REFERENCE :
  1780. begin
  1781. if GotStar then
  1782. oper.opr.ref.scalefactor:=l
  1783. else if (prevasmtoken = AS_STAR) then
  1784. begin
  1785. if scale<>0 then
  1786. scale:=l*scale
  1787. else
  1788. scale:=l;
  1789. end
  1790. else
  1791. begin
  1792. Inc(oper.opr.ref.offset,l);
  1793. Inc(oper.opr.constoffset,l);
  1794. end;
  1795. end;
  1796. OPR_LOCAL :
  1797. begin
  1798. if GotStar then
  1799. oper.opr.localscale:=l
  1800. else if (prevasmtoken = AS_STAR) then
  1801. begin
  1802. if scale<>0 then
  1803. scale:=l*scale
  1804. else
  1805. scale:=l;
  1806. end
  1807. else
  1808. Inc(oper.opr.localsymofs,l);
  1809. end;
  1810. end;
  1811. GotPlus:=(prevasmtoken=AS_PLUS) or
  1812. (prevasmtoken=AS_MINUS);
  1813. if GotPlus then
  1814. negative := prevasmtoken = AS_MINUS;
  1815. GotStar:=(prevasmtoken=AS_STAR);
  1816. end;
  1817. AS_LBRACKET :
  1818. begin
  1819. if (GotPlus and Negative) or GotStar then
  1820. Message(asmr_e_invalid_reference_syntax);
  1821. tmpoper:=Tx86Operand.create;
  1822. BuildReference(tmpoper);
  1823. AddReferences(oper,tmpoper);
  1824. tmpoper.Free;
  1825. GotPlus:=false;
  1826. GotStar:=false;
  1827. end;
  1828. AS_RBRACKET :
  1829. begin
  1830. if GotPlus or GotStar or BracketlessReference then
  1831. Message(asmr_e_invalid_reference_syntax);
  1832. Consume(AS_RBRACKET);
  1833. if actasmtoken=AS_LBRACKET then
  1834. begin
  1835. tmpoper:=Tx86Operand.create;
  1836. BuildReference(tmpoper);
  1837. AddReferences(oper,tmpoper);
  1838. tmpoper.Free;
  1839. end;
  1840. break;
  1841. end;
  1842. AS_SEPARATOR,
  1843. AS_END,
  1844. AS_COMMA:
  1845. begin
  1846. if not BracketlessReference then
  1847. begin
  1848. Message(asmr_e_invalid_reference_syntax);
  1849. RecoverConsume(true);
  1850. end;
  1851. break;
  1852. end;
  1853. else
  1854. Begin
  1855. Message(asmr_e_invalid_reference_syntax);
  1856. RecoverConsume(true);
  1857. break;
  1858. end;
  1859. end;
  1860. until false;
  1861. end;
  1862. Procedure tx86intreader.BuildConstantOperand(oper: tx86operand);
  1863. var
  1864. l,size : tcgint;
  1865. tempstr : string;
  1866. tempsymtyp : tasmsymtype;
  1867. cse_out_flags : tconstsymbolexpressionoutputflags;
  1868. begin
  1869. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1870. Message(asmr_e_invalid_operand_type);
  1871. BuildConstSymbolExpression([cseif_needofs],l,tempstr,tempsymtyp,size,cse_out_flags);
  1872. {$ifdef i8086}
  1873. if tempstr='@DATA' then
  1874. begin
  1875. if not (cseof_isseg in cse_out_flags) then
  1876. Message(asmr_e_CODE_or_DATA_without_SEG);
  1877. oper.SetupData;
  1878. end
  1879. else if tempstr='@CODE' then
  1880. begin
  1881. if not (cseof_isseg in cse_out_flags) then
  1882. Message(asmr_e_CODE_or_DATA_without_SEG);
  1883. oper.SetupCode;
  1884. end
  1885. else
  1886. {$endif i8086}
  1887. if tempstr<>'' then
  1888. begin
  1889. oper.opr.typ:=OPR_SYMBOL;
  1890. oper.opr.symofs:=l;
  1891. oper.opr.symbol:=current_asmdata.RefAsmSymbol(tempstr,tempsymtyp);
  1892. oper.opr.symseg:=cseof_isseg in cse_out_flags;
  1893. oper.opr.sym_farproc_entry:=cseof_is_farproc_entry in cse_out_flags;
  1894. end
  1895. else
  1896. if oper.opr.typ=OPR_NONE then
  1897. begin
  1898. oper.opr.typ:=OPR_CONSTANT;
  1899. oper.opr.val:=l;
  1900. end
  1901. else
  1902. inc(oper.opr.val,l);
  1903. end;
  1904. Procedure tx86intreader.BuildOperand(oper: tx86operand;istypecast:boolean);
  1905. procedure AddLabelOperand(hl:tasmlabel);
  1906. begin
  1907. if (oper.opr.typ=OPR_NONE) and
  1908. is_calljmp(actopcode) then
  1909. begin
  1910. oper.opr.typ:=OPR_SYMBOL;
  1911. oper.opr.symbol:=hl;
  1912. end
  1913. else
  1914. begin
  1915. oper.InitRef;
  1916. oper.opr.ref.symbol:=hl;
  1917. oper.haslabelref:=true;
  1918. {$ifdef i8086}
  1919. if oper.opr.ref.segment=NR_NO then
  1920. oper.opr.ref.segment:=NR_CS;
  1921. {$endif i8086}
  1922. end;
  1923. end;
  1924. var
  1925. expr,
  1926. hs : string;
  1927. tempreg : tregister;
  1928. l : tcgint;
  1929. hl : tasmlabel;
  1930. toffset,
  1931. tsize : tcgint;
  1932. hastypecast: boolean;
  1933. begin
  1934. expr:='';
  1935. repeat
  1936. if actasmtoken=AS_DOT then
  1937. begin
  1938. if expr<>'' then
  1939. begin
  1940. BuildRecordOffsetSize(expr,toffset,tsize,hs,false,hastypecast);
  1941. if (oper.opr.typ<>OPR_NONE) and
  1942. (hs<>'') then
  1943. Message(asmr_e_wrong_sym_type);
  1944. oper.SetSize(tsize,true);
  1945. if hastypecast then
  1946. oper.hastype:=true;
  1947. { we have used the size of a field. Reset the typesize of the record }
  1948. oper.typesize:=0;
  1949. case oper.opr.typ of
  1950. OPR_LOCAL :
  1951. begin
  1952. { don't allow direct access to fields of parameters, because that
  1953. will generate buggy code. Allow it only for explicit typecasting
  1954. and when the parameter is in a register (delphi compatible) }
  1955. if (not oper.hastype) then
  1956. checklocalsubscript(oper.opr.localsym);
  1957. oper.opr.localforceref:=true;
  1958. inc(oper.opr.localsymofs,toffset);
  1959. oper.opr.localvarsize := tsize;
  1960. end;
  1961. OPR_CONSTANT :
  1962. inc(oper.opr.val,toffset);
  1963. OPR_REFERENCE :
  1964. begin
  1965. inc(oper.opr.ref.offset,toffset);
  1966. oper.opr.varsize := tsize;
  1967. end;
  1968. OPR_NONE :
  1969. begin
  1970. if (hs <> '') then
  1971. begin
  1972. oper.opr.typ:=OPR_SYMBOL;
  1973. oper.opr.symbol:=current_asmdata.RefAsmSymbol(hs,AT_FUNCTION);
  1974. end
  1975. else
  1976. begin
  1977. oper.opr.typ:=OPR_CONSTANT;
  1978. oper.opr.val:=toffset;
  1979. end;
  1980. end;
  1981. OPR_REGISTER :
  1982. Message(asmr_e_invalid_reference_syntax);
  1983. OPR_SYMBOL:
  1984. Message(asmr_e_invalid_symbol_ref);
  1985. else
  1986. internalerror(200309222);
  1987. end;
  1988. expr:='';
  1989. end
  1990. else
  1991. begin
  1992. { See it as a separator }
  1993. Consume(AS_DOT);
  1994. end;
  1995. end;
  1996. case actasmtoken of
  1997. {$ifndef i8086}
  1998. AS_SEG :
  1999. Begin
  2000. Message(asmr_e_seg_not_supported);
  2001. Consume(actasmtoken);
  2002. end;
  2003. {$else not i8086}
  2004. AS_SEG,
  2005. {$endif not i8086}
  2006. AS_OFFSET,
  2007. AS_SIZEOF,
  2008. AS_VMTOFFSET,
  2009. AS_TYPE,
  2010. AS_NOT,
  2011. AS_STRING,
  2012. AS_PLUS,
  2013. AS_MINUS,
  2014. AS_LPAREN,
  2015. AS_INTNUM :
  2016. begin
  2017. case oper.opr.typ of
  2018. OPR_REFERENCE :
  2019. {$ifndef x86_64}
  2020. { this is for the i386 scenario where you have
  2021. <load got into ebx>
  2022. mov eax, [ebx].offset globalvar
  2023. x86-64 uses RIP-based addresses (both for GOT and non-GOT
  2024. relative accesses)
  2025. }
  2026. if (actasmtoken=AS_OFFSET) and
  2027. (cs_create_pic in current_settings.moduleswitches) then
  2028. begin
  2029. Consume(AS_OFFSET);
  2030. oper.opr.ref.refaddr:=addr_pic;
  2031. BuildOperand(oper,false);
  2032. end
  2033. else
  2034. {$endif x86_64}
  2035. begin
  2036. l := BuildRefConstExpression(tsize);
  2037. if tsize<>0 then
  2038. oper.SetSize(tsize,false);
  2039. inc(oper.opr.ref.offset,l);
  2040. inc(oper.opr.constoffset,l);
  2041. end;
  2042. OPR_LOCAL :
  2043. begin
  2044. l := BuildConstExpression;
  2045. inc(oper.opr.localsymofs,l);
  2046. inc(oper.opr.localconstoffset,l);
  2047. end;
  2048. OPR_NONE,
  2049. OPR_CONSTANT :
  2050. BuildConstantOperand(oper);
  2051. else
  2052. Message(asmr_e_invalid_operand_type);
  2053. end;
  2054. end;
  2055. AS_PTR :
  2056. begin
  2057. if not oper.hastype then
  2058. begin
  2059. if (oper.opr.typ=OPR_CONSTANT) then
  2060. begin
  2061. oper.typesize:=oper.opr.val;
  2062. { reset constant value of operand }
  2063. oper.opr.typ:=OPR_NONE;
  2064. oper.opr.val:=0;
  2065. end
  2066. else
  2067. Message(asmr_e_syn_operand);
  2068. end;
  2069. Consume(AS_PTR);
  2070. { in delphi mode, allow e.g. call dword ptr eax,
  2071. see also webtbs/tw18225.pp }
  2072. if not(m_delphi in current_settings.modeswitches) then
  2073. oper.InitRef;
  2074. { if the operand subscripts a record, the typesize will be
  2075. rest -> save it here and restore it afterwards }
  2076. l:=oper.typesize;
  2077. BuildOperand(oper,false);
  2078. oper.setsize(l,true);
  2079. end;
  2080. AS_ID : { A constant expression, or a Variable ref. }
  2081. Begin
  2082. { Label or Special symbol reference? }
  2083. if actasmpattern[1] = '@' then
  2084. Begin
  2085. if actasmpattern = '@RESULT' then
  2086. Begin
  2087. oper.SetupResult;
  2088. Consume(AS_ID);
  2089. expr:='result';
  2090. end
  2091. else
  2092. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  2093. begin
  2094. {$ifdef i8086}
  2095. Message(asmr_e_CODE_or_DATA_without_SEG);
  2096. {$else i8086}
  2097. Message(asmr_w_CODE_and_DATA_not_supported);
  2098. {$endif i8086}
  2099. Consume(AS_ID);
  2100. end
  2101. else
  2102. { Local Label }
  2103. begin
  2104. CreateLocalLabel(actasmpattern,hl,false);
  2105. Consume(AS_ID);
  2106. AddLabelOperand(hl);
  2107. end;
  2108. end
  2109. else
  2110. { support result for delphi modes }
  2111. if (m_objpas in current_settings.modeswitches) and (actasmpattern='RESULT') then
  2112. begin
  2113. oper.SetUpResult;
  2114. Consume(AS_ID);
  2115. expr:='result';
  2116. end
  2117. { probably a variable or normal expression }
  2118. { or a procedure (such as in CALL ID) }
  2119. else
  2120. Begin
  2121. { is it a constant ? }
  2122. if SearchIConstant(actasmpattern,l) then
  2123. Begin
  2124. case oper.opr.typ of
  2125. OPR_REFERENCE :
  2126. begin
  2127. l := BuildRefConstExpression(tsize);
  2128. if tsize<>0 then
  2129. oper.SetSize(tsize,false);
  2130. inc(oper.opr.ref.offset,l);
  2131. inc(oper.opr.constoffset,l);
  2132. end;
  2133. OPR_LOCAL :
  2134. begin
  2135. l := BuildRefConstExpression(tsize);
  2136. if tsize<>0 then
  2137. oper.SetSize(tsize,false);
  2138. inc(oper.opr.localsymofs,l);
  2139. inc(oper.opr.localconstoffset,l);
  2140. end;
  2141. OPR_NONE,
  2142. OPR_CONSTANT :
  2143. BuildConstantOperand(oper);
  2144. else
  2145. Message(asmr_e_invalid_operand_type);
  2146. end;
  2147. end
  2148. else
  2149. { Check for pascal label }
  2150. if SearchLabel(actasmpattern,hl,false) then
  2151. begin
  2152. Consume(AS_ID);
  2153. AddLabelOperand(hl);
  2154. end
  2155. else
  2156. { is it a normal variable ? }
  2157. Begin
  2158. expr:=actasmpattern;
  2159. Consume(AS_ID);
  2160. { typecasting? }
  2161. if SearchType(expr,l) then
  2162. begin
  2163. oper.hastype:=true;
  2164. oper.typesize:=l;
  2165. case actasmtoken of
  2166. AS_LPAREN :
  2167. begin
  2168. { Support Type([Reference]) }
  2169. Consume(AS_LPAREN);
  2170. BuildOperand(oper,true);
  2171. { Delphi also supports Type(Register) and
  2172. interprets it the same as Type([Register]). }
  2173. if (oper.opr.typ = OPR_REGISTER) then
  2174. { This also sets base to the register. }
  2175. oper.InitRef;
  2176. Consume(AS_RPAREN);
  2177. end;
  2178. AS_LBRACKET :
  2179. begin
  2180. { Support Var.Type[Index] }
  2181. { Convert @label.Byte[1] to reference }
  2182. if oper.opr.typ=OPR_SYMBOL then
  2183. oper.initref;
  2184. end;
  2185. end;
  2186. end
  2187. else
  2188. begin
  2189. if not oper.SetupVar(expr,false) then
  2190. Begin
  2191. { not a variable, check special variables.. }
  2192. if expr = 'SELF' then
  2193. begin
  2194. oper.SetupSelf;
  2195. expr:='self';
  2196. end
  2197. else
  2198. begin
  2199. Message1(sym_e_unknown_id,expr);
  2200. expr:='';
  2201. end;
  2202. end;
  2203. { indexed access to variable? }
  2204. if actasmtoken=AS_LBRACKET then
  2205. begin
  2206. { ... then the operand size is not known anymore }
  2207. oper.size:=OS_NO;
  2208. BuildReference(oper);
  2209. end;
  2210. end;
  2211. end;
  2212. end;
  2213. end;
  2214. AS_REGISTER : { Register, a variable reference or a constant reference }
  2215. begin
  2216. { save the type of register used. }
  2217. tempreg:=actasmregister;
  2218. Consume(AS_REGISTER);
  2219. if actasmtoken = AS_COLON then
  2220. Begin
  2221. Consume(AS_COLON);
  2222. oper.InitRef;
  2223. SetSegmentOverride(oper,tempreg);
  2224. BuildReference(oper);
  2225. end
  2226. else
  2227. { Simple register }
  2228. begin
  2229. if (oper.opr.typ <> OPR_NONE) then
  2230. Message(asmr_e_syn_operand);
  2231. oper.opr.typ:=OPR_REGISTER;
  2232. oper.opr.reg:=tempreg;
  2233. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  2234. end;
  2235. end;
  2236. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  2237. Begin
  2238. BuildReference(oper);
  2239. end;
  2240. AS_DWORD,
  2241. AS_BYTE,
  2242. AS_WORD,
  2243. AS_TBYTE,
  2244. AS_DQWORD,
  2245. AS_QWORD,
  2246. AS_OWORD,
  2247. AS_XMMWORD,
  2248. AS_YWORD,
  2249. AS_YMMWORD:
  2250. begin
  2251. { Type specifier }
  2252. oper.hastype:=true;
  2253. oper.typesize:=0;
  2254. case actasmtoken of
  2255. AS_DWORD : oper.typesize:=4;
  2256. AS_WORD : oper.typesize:=2;
  2257. AS_BYTE : oper.typesize:=1;
  2258. AS_QWORD : oper.typesize:=8;
  2259. AS_DQWORD : oper.typesize:=16;
  2260. AS_TBYTE : oper.typesize:=10;
  2261. AS_OWORD,
  2262. AS_XMMWORD: oper.typesize:=16;
  2263. AS_YWORD,
  2264. AS_YMMWORD: oper.typesize:=32;
  2265. else
  2266. internalerror(2010061101);
  2267. end;
  2268. Consume(actasmtoken);
  2269. if (actasmtoken=AS_LPAREN) then
  2270. begin
  2271. { Support "xxx ptr [Reference]" }
  2272. { in case the expression subscripts a record, the typesize
  2273. is reset, so save the explicit size we set above }
  2274. l:=oper.typesize;
  2275. Consume(AS_LPAREN);
  2276. BuildOperand(oper,true);
  2277. Consume(AS_RPAREN);
  2278. oper.setsize(l,true);
  2279. end;
  2280. end;
  2281. AS_SEPARATOR,
  2282. AS_END,
  2283. AS_COMMA,
  2284. AS_COLON:
  2285. begin
  2286. break;
  2287. end;
  2288. AS_RPAREN:
  2289. begin
  2290. if not istypecast then
  2291. begin
  2292. Message(asmr_e_syn_operand);
  2293. Consume(AS_RPAREN);
  2294. end
  2295. else
  2296. break;
  2297. end;
  2298. else
  2299. begin
  2300. Message(asmr_e_syn_operand);
  2301. RecoverConsume(true);
  2302. break;
  2303. end;
  2304. end;
  2305. until false;
  2306. { End of operand, update size if a typecast is forced }
  2307. if (oper.typesize<>0) and
  2308. (oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL]) then
  2309. oper.SetSize(oper.typesize,true);
  2310. {$ifdef i8086}
  2311. { references to a procedure/function entry, without an explicit segment
  2312. override, are added an CS: override by default (this is Turbo Pascal 7
  2313. compatible) }
  2314. if (oper.opr.typ=OPR_REFERENCE) and assigned(oper.opr.ref.symbol) and
  2315. (oper.opr.ref.symbol.typ=AT_FUNCTION) and (oper.opr.ref.segment=NR_NO) then
  2316. oper.opr.ref.segment:=NR_CS;
  2317. {$endif i8086}
  2318. end;
  2319. Procedure tx86intreader.BuildOpCode(instr : tx86instruction);
  2320. var
  2321. PrefixOp,OverrideOp: tasmop;
  2322. operandnum : longint;
  2323. t: TRegister;
  2324. is_far_const:boolean;
  2325. i:byte;
  2326. tmp: toperand;
  2327. di_param, si_param: ShortInt;
  2328. prefix_or_override_pending_concat: boolean = false;
  2329. {$ifdef i8086}
  2330. hsymbol: TAsmSymbol;
  2331. hoffset: ASizeInt;
  2332. href_farproc_entry: Boolean;
  2333. {$endif i8086}
  2334. begin
  2335. PrefixOp:=A_None;
  2336. OverrideOp:=A_None;
  2337. is_far_const:=false;
  2338. { prefix seg opcode / prefix opcode }
  2339. repeat
  2340. if is_prefix(actopcode) then
  2341. with instr do
  2342. begin
  2343. if prefix_or_override_pending_concat then
  2344. ConcatInstruction(curlist);
  2345. PrefixOp:=ActOpcode;
  2346. opcode:=ActOpcode;
  2347. condition:=ActCondition;
  2348. opsize:=ActOpsize;
  2349. prefix_or_override_pending_concat:=true;
  2350. consume(AS_OPCODE);
  2351. end
  2352. else
  2353. if is_override(actopcode) then
  2354. with instr do
  2355. begin
  2356. if prefix_or_override_pending_concat then
  2357. ConcatInstruction(curlist);
  2358. OverrideOp:=ActOpcode;
  2359. opcode:=ActOpcode;
  2360. condition:=ActCondition;
  2361. opsize:=ActOpsize;
  2362. prefix_or_override_pending_concat:=true;
  2363. consume(AS_OPCODE);
  2364. end
  2365. else
  2366. break;
  2367. { allow for newline after prefix or override }
  2368. while actasmtoken=AS_SEPARATOR do
  2369. consume(AS_SEPARATOR);
  2370. until (actasmtoken<>AS_OPCODE);
  2371. { opcode }
  2372. if (actasmtoken <> AS_OPCODE) then
  2373. begin
  2374. { allow a prefix or override to be used standalone, like an opcode
  2375. with zero operands; this is TP7 compatible and allows compiling
  2376. ugly code like 'seges; db $67,$66; lodsw' }
  2377. if prefix_or_override_pending_concat then
  2378. exit
  2379. else
  2380. begin
  2381. Message(asmr_e_invalid_or_missing_opcode);
  2382. RecoverConsume(false);
  2383. exit;
  2384. end;
  2385. end;
  2386. if prefix_or_override_pending_concat then
  2387. begin
  2388. instr.ConcatInstruction(curlist);
  2389. prefix_or_override_pending_concat:=false;
  2390. end;
  2391. { Fill the instr object with the current state }
  2392. with instr do
  2393. begin
  2394. Opcode:=ActOpcode;
  2395. condition:=ActCondition;
  2396. opsize:=ActOpsize;
  2397. { Valid combination of prefix/override and instruction ? }
  2398. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  2399. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  2400. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  2401. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  2402. end;
  2403. { pushf/popf/pusha/popa have to default to 16 bit in Intel mode
  2404. (Intel manual and Delphi-compatbile) -- setting the opsize for
  2405. these instructions doesn't change anything in the internal assember,
  2406. so change the opcode }
  2407. if (instr.opcode=A_POPF) then
  2408. instr.opcode:=A_POPFW
  2409. else if (instr.opcode=A_PUSHF) then
  2410. instr.opcode:=A_PUSHFW
  2411. {$ifndef x86_64}
  2412. else if (instr.opcode=A_PUSHA) then
  2413. instr.opcode:=A_PUSHAW
  2414. else if (instr.opcode=A_POPA) then
  2415. instr.opcode:=A_POPAW
  2416. {$endif x86_64}
  2417. {$ifdef i8086}
  2418. { ret is converted to retn or retf, depending on the call model of the
  2419. current procedure (BP7 compatible) }
  2420. else if (instr.opcode=A_RET) then
  2421. begin
  2422. if is_proc_far(current_procinfo.procdef) and
  2423. not (po_interrupt in current_procinfo.procdef.procoptions) then
  2424. instr.opcode:=A_RETF
  2425. else
  2426. instr.opcode:=A_RETN;
  2427. end
  2428. {$endif i8086}
  2429. ;
  2430. { We are reading operands, so opcode will be an AS_ID }
  2431. { process operands backwards to get them in AT&T order }
  2432. operandnum:=max_operands;
  2433. is_far_const:=false;
  2434. Consume(AS_OPCODE);
  2435. { Zero operand opcode ? }
  2436. if actasmtoken in [AS_SEPARATOR,AS_END] then
  2437. exit;
  2438. { Read Operands }
  2439. repeat
  2440. case actasmtoken of
  2441. { End of asm operands for this opcode }
  2442. AS_END,
  2443. AS_SEPARATOR :
  2444. break;
  2445. { Operand delimiter }
  2446. AS_COMMA :
  2447. begin
  2448. { should have something before the comma }
  2449. if instr.operands[operandnum].opr.typ=OPR_NONE then
  2450. Message(asmr_e_syntax_error);
  2451. if operandnum <= 1 then
  2452. Message(asmr_e_too_many_operands)
  2453. else
  2454. Dec(operandnum);
  2455. Consume(AS_COMMA);
  2456. end;
  2457. {Far constant, i.e. jmp $0000:$11111111.}
  2458. AS_COLON:
  2459. begin
  2460. is_far_const:=true;
  2461. if operandnum<max_operands then
  2462. message(asmr_e_too_many_operands)
  2463. else
  2464. dec(operandnum);
  2465. consume(AS_COLON);
  2466. end;
  2467. { Type specifier }
  2468. AS_NEAR,
  2469. AS_FAR :
  2470. begin
  2471. if actasmtoken = AS_NEAR then
  2472. begin
  2473. {$ifndef i8086}
  2474. Message(asmr_w_near_ignored);
  2475. {$endif not i8086}
  2476. instr.opsize:=S_NEAR;
  2477. end
  2478. else
  2479. begin
  2480. {$ifndef i8086}
  2481. Message(asmr_w_far_ignored);
  2482. {$endif not i8086}
  2483. instr.opsize:=S_FAR;
  2484. end;
  2485. Consume(actasmtoken);
  2486. if actasmtoken=AS_PTR then
  2487. begin
  2488. Consume(AS_PTR);
  2489. instr.Operands[operandnum].InitRef;
  2490. end;
  2491. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2492. end;
  2493. else
  2494. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2495. end; { end case }
  2496. until false;
  2497. { shift operands to start from 1, exchange to make sure they are destroyed correctly }
  2498. for i:=operandnum to max_operands do
  2499. begin
  2500. tmp:=instr.operands[i+1-operandnum];
  2501. instr.operands[i+1-operandnum]:=instr.operands[i];
  2502. instr.operands[i]:=tmp;
  2503. end;
  2504. operandnum:=(max_operands+1)-operandnum;
  2505. instr.ops:=operandnum;
  2506. { Check operands }
  2507. for i:=1 to operandnum do
  2508. begin
  2509. if is_far_const and
  2510. (instr.operands[i].opr.typ<>OPR_CONSTANT) then
  2511. message(asmr_e_expr_illegal)
  2512. else
  2513. if instr.operands[i].opr.typ=OPR_NONE then
  2514. Message(asmr_e_syntax_error);
  2515. end;
  2516. { Check for invalid ES: overrides }
  2517. if is_x86_parameterized_string_op(instr.opcode) then
  2518. begin
  2519. si_param:=get_x86_string_op_si_param(instr.opcode);
  2520. if si_param<>-1 then
  2521. si_param:=x86_parameterized_string_op_param_count(instr.opcode)-si_param;
  2522. di_param:=get_x86_string_op_di_param(instr.opcode);
  2523. if di_param<>-1 then
  2524. begin
  2525. di_param:=x86_parameterized_string_op_param_count(instr.opcode)-di_param;
  2526. if di_param<=operandnum then
  2527. with instr.operands[di_param] do
  2528. if (opr.typ=OPR_REFERENCE) and
  2529. (opr.ref.segment<>NR_NO) and
  2530. (opr.ref.segment<>NR_ES) then
  2531. Message(asmr_e_cannot_override_es_segment);
  2532. end;
  2533. { if two memory parameters, check whether their address sizes are equal }
  2534. if (si_param<>-1) and (di_param<>-1) and
  2535. (si_param<=operandnum) and (di_param<=operandnum) and
  2536. (instr.operands[si_param].opr.typ=OPR_REFERENCE) and
  2537. (instr.operands[di_param].opr.typ=OPR_REFERENCE) then
  2538. begin
  2539. if get_ref_address_size(instr.operands[si_param].opr.ref)<>
  2540. get_ref_address_size(instr.operands[di_param].opr.ref) then
  2541. Message(asmr_e_address_sizes_do_not_match);
  2542. end;
  2543. end;
  2544. { e.g. for "push dword 1", "push word 6" }
  2545. if (instr.ops=1) and
  2546. (instr.operands[1].typesize<>0) then
  2547. instr.operands[1].setsize(instr.operands[1].typesize,false);
  2548. {$ifdef i8086}
  2549. for i:=1 to operandnum do
  2550. with instr.operands[i].opr do
  2551. begin
  2552. { convert 'call/jmp [proc/label]' to 'call/jmp proc/label'. Ugly,
  2553. but Turbo Pascal 7 compatible. }
  2554. if (instr.opcode in [A_CALL,A_JMP]) and
  2555. (instr.operands[i].haslabelref or instr.operands[i].hasproc)
  2556. and (typ=OPR_REFERENCE) and
  2557. assigned(ref.symbol) and (ref.symbol.typ in [AT_FUNCTION,AT_LABEL,AT_ADDR]) and
  2558. (ref.base=NR_NO) and (ref.index=NR_NO) then
  2559. begin
  2560. hsymbol:=ref.symbol;
  2561. hoffset:=ref.offset;
  2562. href_farproc_entry:=ref_farproc_entry;
  2563. typ:=OPR_SYMBOL;
  2564. symbol:=hsymbol;
  2565. symofs:=hoffset;
  2566. symseg:=False;
  2567. sym_farproc_entry:=href_farproc_entry;
  2568. end;
  2569. { convert 'call/jmp symbol' to 'call/jmp far symbol' for symbols that are an entry point of a far procedure }
  2570. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2571. (typ=OPR_SYMBOL) and sym_farproc_entry then
  2572. instr.opsize:=S_FAR;
  2573. { convert 'call/jmp dword [something]' to 'call/jmp far [something]' (BP7 compatibility) }
  2574. if (instr.opcode in [A_CALL,A_JMP]) and (instr.opsize=S_NO) and
  2575. (typ in [OPR_LOCAL,OPR_REFERENCE]) and (instr.operands[i].size=OS_32) then
  2576. instr.opsize:=S_FAR;
  2577. end;
  2578. {$endif i8086}
  2579. if (MemRefInfo(instr.opcode).ExistsSSEAVX) and
  2580. (MemRefInfo(instr.opcode).MemRefSize in MemRefSizeInfoVMems) then
  2581. begin
  2582. for i:=1 to operandnum do
  2583. begin
  2584. if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2585. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2586. (instr.operands[i].opr.ref.index = NR_NO) then
  2587. begin
  2588. instr.operands[i].opr.ref.index := instr.operands[i].opr.ref.base;
  2589. instr.operands[i].opr.ref.base := NR_NO;
  2590. end
  2591. else if (instr.operands[i].opr.typ = OPR_REFERENCE) and
  2592. (getregtype(instr.operands[i].opr.ref.base) = R_MMREGISTER) and
  2593. (getregtype(instr.operands[i].opr.ref.index) = R_INTREGISTER) and
  2594. (getsubreg(instr.operands[i].opr.ref.index) = R_SUBADDR) then
  2595. begin
  2596. // exchange base- and index-register
  2597. // e.g. VGATHERDPD XMM0, [XMM1 + RAX], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1], XMM2
  2598. // e.g. VGATHERDPD XMM0, [XMM1 + RAX * 2], XMM2 =>> not supported
  2599. // e.g. VGATHERDPD XMM0, [XMM1 + RAX + 16], XMM2 =>> VGATHERDPD XMM0, [RAX + XMM1 + 16]
  2600. if instr.operands[i].opr.ref.scalefactor > 1 then Message(asmr_e_invalid_reference_syntax)
  2601. else
  2602. begin
  2603. t := instr.operands[i].opr.ref.base;
  2604. instr.operands[i].opr.ref.base := instr.operands[i].opr.ref.index;
  2605. instr.operands[i].opr.ref.index := t;
  2606. end;
  2607. end;
  2608. end;
  2609. end;
  2610. end;
  2611. Procedure tx86intreader.BuildConstant(constsize: byte);
  2612. var
  2613. asmsymtyp : tasmsymtype;
  2614. asmsym,
  2615. expr: string;
  2616. value,size : tcgint;
  2617. cse_out_flags : tconstsymbolexpressionoutputflags;
  2618. Begin
  2619. Repeat
  2620. Case actasmtoken of
  2621. AS_STRING:
  2622. Begin
  2623. { DD and DW cases }
  2624. if constsize <> 1 then
  2625. Begin
  2626. if Not PadZero(actasmpattern,constsize) then
  2627. Message(scan_f_string_exceeds_line);
  2628. end;
  2629. expr:=actasmpattern;
  2630. Consume(AS_STRING);
  2631. Case actasmtoken of
  2632. AS_COMMA:
  2633. Consume(AS_COMMA);
  2634. AS_END,
  2635. AS_SEPARATOR: ;
  2636. else
  2637. Message(asmr_e_invalid_string_expression);
  2638. end;
  2639. ConcatString(curlist,expr);
  2640. end;
  2641. AS_PLUS,
  2642. AS_MINUS,
  2643. AS_LPAREN,
  2644. AS_NOT,
  2645. AS_INTNUM,
  2646. AS_OFFSET,
  2647. AS_LBRACKET,
  2648. {$ifdef i8086}
  2649. AS_SEG,
  2650. {$endif i8086}
  2651. AS_ID :
  2652. Begin
  2653. BuildConstSymbolExpression([cseif_referencelike],value,asmsym,asmsymtyp,size,cse_out_flags);
  2654. if asmsym<>'' then
  2655. begin
  2656. if not (cseof_isseg in cse_out_flags) and
  2657. {$ifdef i8086}
  2658. ((constsize<>2) and (constsize<>4))
  2659. {$else i8086}
  2660. (constsize<>sizeof(pint))
  2661. {$endif i8086}
  2662. then
  2663. begin
  2664. Message1(asmr_w_const32bit_for_address,asmsym);
  2665. constsize:=sizeof(pint);
  2666. end;
  2667. {$ifdef i8086}
  2668. if asmsym='@DATA' then
  2669. begin
  2670. if not (cseof_isseg in cse_out_flags) then
  2671. Message(asmr_e_CODE_or_DATA_without_SEG);
  2672. if constsize<2 then
  2673. Message1(asmr_e_const16bit_for_segment,asmsym);
  2674. if current_settings.x86memorymodel=mm_huge then
  2675. curlist.concat(Tai_const.Create_fardataseg)
  2676. else
  2677. curlist.concat(Tai_const.Create_dgroup);
  2678. if constsize>2 then
  2679. ConcatConstant(curlist,0,constsize-2);
  2680. end
  2681. else if asmsym='@CODE' then
  2682. begin
  2683. if not (cseof_isseg in cse_out_flags) then
  2684. Message(asmr_e_CODE_or_DATA_without_SEG);
  2685. if constsize<2 then
  2686. Message1(asmr_e_const16bit_for_segment,asmsym);
  2687. curlist.concat(Tai_const.Create_seg_name(current_procinfo.procdef.mangledname));
  2688. if constsize>2 then
  2689. ConcatConstant(curlist,0,constsize-2);
  2690. end
  2691. else if cseof_isseg in cse_out_flags then
  2692. begin
  2693. if constsize<2 then
  2694. Message1(asmr_e_const16bit_for_segment,asmsym);
  2695. curlist.concat(Tai_const.Create_seg_name(asmsym));
  2696. if constsize>2 then
  2697. ConcatConstant(curlist,0,constsize-2);
  2698. end
  2699. else
  2700. {$endif i8086}
  2701. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value,constsize,cseof_hasofs in cse_out_flags);
  2702. end
  2703. else
  2704. ConcatConstant(curlist,value,constsize);
  2705. end;
  2706. AS_COMMA:
  2707. begin
  2708. Consume(AS_COMMA);
  2709. end;
  2710. AS_ALIGN,
  2711. AS_DB,
  2712. AS_DW,
  2713. AS_DD,
  2714. AS_DQ,
  2715. AS_OPCODE,
  2716. AS_END,
  2717. AS_SEPARATOR:
  2718. break;
  2719. else
  2720. begin
  2721. Message(asmr_e_syn_constant);
  2722. RecoverConsume(false);
  2723. end
  2724. end;
  2725. Until false;
  2726. end;
  2727. function tx86intreader.Assemble: tlinkedlist;
  2728. Var
  2729. hl : tasmlabel;
  2730. instr : Tx86Instruction;
  2731. tmpsym: tsym;
  2732. tmpsrsymtable: TSymtable;
  2733. Begin
  2734. Message1(asmr_d_start_reading,'intel');
  2735. inexpression:=FALSE;
  2736. firsttoken:=TRUE;
  2737. { sets up all opcode and register tables in uppercase
  2738. done in the construtor now
  2739. if not _asmsorted then
  2740. Begin
  2741. SetupTables;
  2742. _asmsorted:=TRUE;
  2743. end;
  2744. }
  2745. curlist:=TAsmList.Create;
  2746. { we might need to know which parameters are passed in registers }
  2747. if not parse_generic then
  2748. current_procinfo.generate_parameter_info;
  2749. { start tokenizer }
  2750. gettoken;
  2751. { main loop }
  2752. repeat
  2753. case actasmtoken of
  2754. AS_LLABEL:
  2755. Begin
  2756. if CreateLocalLabel(actasmpattern,hl,true) then
  2757. ConcatLabel(curlist,hl);
  2758. Consume(AS_LLABEL);
  2759. end;
  2760. AS_LABEL:
  2761. Begin
  2762. if SearchLabel(upper(actasmpattern),hl,true) then
  2763. begin
  2764. if hl.is_public then
  2765. ConcatPublic(curlist,actasmpattern_origcase);
  2766. ConcatLabel(curlist,hl);
  2767. end
  2768. else
  2769. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2770. Consume(AS_LABEL);
  2771. end;
  2772. AS_DW :
  2773. Begin
  2774. inexpression:=true;
  2775. Consume(AS_DW);
  2776. BuildConstant(2);
  2777. inexpression:=false;
  2778. end;
  2779. AS_DB :
  2780. Begin
  2781. inexpression:=true;
  2782. Consume(AS_DB);
  2783. BuildConstant(1);
  2784. inexpression:=false;
  2785. end;
  2786. AS_DD :
  2787. Begin
  2788. inexpression:=true;
  2789. Consume(AS_DD);
  2790. BuildConstant(4);
  2791. inexpression:=false;
  2792. end;
  2793. {$ifdef cpu64bitaddr}
  2794. AS_DQ:
  2795. Begin
  2796. inexpression:=true;
  2797. Consume(AS_DQ);
  2798. BuildConstant(8);
  2799. inexpression:=false;
  2800. end;
  2801. {$endif cpu64bitaddr}
  2802. AS_PUBLIC:
  2803. Begin
  2804. Consume(AS_PUBLIC);
  2805. repeat
  2806. if actasmtoken=AS_ID then
  2807. begin
  2808. if (actasmpattern<>'') and (actasmpattern[1]='@') then
  2809. Message1(asmr_e_local_label_cannot_be_declared_public,actasmpattern)
  2810. else if SearchLabel(upper(actasmpattern),hl,false) then
  2811. begin
  2812. if not hl.is_public then
  2813. begin
  2814. hl.is_public:=true;
  2815. asmsearchsym(upper(actasmpattern),tmpsym,tmpsrsymtable);
  2816. if tlabelsym(tmpsym).defined then
  2817. Message1(asmr_e_public_must_be_used_before_label_definition,actasmpattern);
  2818. end;
  2819. end
  2820. else
  2821. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2822. end;
  2823. Consume(AS_ID);
  2824. if actasmtoken=AS_COMMA then
  2825. begin
  2826. Consume(AS_COMMA);
  2827. if actasmtoken<>AS_ID then
  2828. Consume(AS_ID);
  2829. end;
  2830. until actasmtoken=AS_SEPARATOR;
  2831. end;
  2832. AS_ALIGN:
  2833. Begin
  2834. Consume(AS_ALIGN);
  2835. ConcatAlign(curlist,BuildConstExpression);
  2836. if actasmtoken<>AS_SEPARATOR then
  2837. Consume(AS_SEPARATOR);
  2838. end;
  2839. AS_OPCODE :
  2840. Begin
  2841. instr:=Tx86Instruction.Create(Tx86Operand);
  2842. BuildOpcode(instr);
  2843. with instr do
  2844. begin
  2845. CheckNonCommutativeOpcodes;
  2846. AddReferenceSizes;
  2847. SetInstructionOpsize;
  2848. CheckOperandSizes;
  2849. ConcatInstruction(curlist);
  2850. end;
  2851. instr.Free;
  2852. end;
  2853. AS_SEPARATOR :
  2854. Begin
  2855. Consume(AS_SEPARATOR);
  2856. end;
  2857. AS_TARGET_DIRECTIVE:
  2858. HandleTargetDirective;
  2859. AS_END :
  2860. break; { end assembly block }
  2861. else
  2862. Begin
  2863. Message(asmr_e_syntax_error);
  2864. RecoverConsume(false);
  2865. end;
  2866. end; { end case }
  2867. until false;
  2868. { check that all referenced local labels are defined }
  2869. checklocallabels;
  2870. { Return the list in an asmnode }
  2871. assemble:=curlist;
  2872. Message1(asmr_d_finish_reading,'intel');
  2873. end;
  2874. end.