rax86int.pas 81 KB

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