rax86int.pas 102 KB

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