rax86int.pas 109 KB

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