rax86int.pas 86 KB

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