rax86int.pas 101 KB

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