rax86int.pas 90 KB

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