rax86int.pas 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332
  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. constructor create;override;
  45. function is_asmopcode(const s: string):boolean;
  46. function is_asmoperator(const s: string):boolean;
  47. function is_asmdirective(const s: string):boolean;
  48. function is_register(const s:string):boolean;
  49. function is_locallabel(const s:string):boolean;
  50. function Assemble: tlinkedlist;override;
  51. procedure GetToken;
  52. function consume(t : tasmtoken):boolean;
  53. procedure RecoverConsume(allowcomma:boolean);
  54. procedure BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint; var mangledname: string; needvmtofs: boolean);
  55. procedure BuildConstSymbolExpression(needofs,isref,startingminus:boolean;var value:aint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  56. function BuildConstExpression:aint;
  57. function BuildRefConstExpression:aint;
  58. procedure BuildReference(oper : tx86operand);
  59. procedure BuildOperand(oper: tx86operand;istypecast:boolean);
  60. procedure BuildConstantOperand(oper: tx86operand);
  61. procedure BuildOpCode(instr : tx86instruction);
  62. procedure BuildConstant(constsize: byte);
  63. end;
  64. implementation
  65. uses
  66. { common }
  67. cutils,
  68. { global }
  69. globals,verbose,
  70. systems,
  71. { aasm }
  72. aasmtai,aasmdata,aasmcpu,
  73. { symtable }
  74. symconst,symbase,symtype,symsym,symdef,symtable,
  75. { parser }
  76. scanner,
  77. { register allocator }
  78. rabase,rautils,itx86int,
  79. { codegen }
  80. cgbase,cgobj,procinfo
  81. ;
  82. type
  83. tasmkeyword = string[9];
  84. const
  85. { These tokens should be modified accordingly to the modifications }
  86. { in the different enumerations. }
  87. firstdirective = AS_ALIGN;
  88. lastdirective = AS_END;
  89. firstoperator = AS_BYTE;
  90. lastoperator = AS___GOTPCREL;
  91. _count_asmdirectives = longint(lastdirective)-longint(firstdirective);
  92. _count_asmoperators = longint(lastoperator)-longint(firstoperator);
  93. _asmdirectives : array[0.._count_asmdirectives] of tasmkeyword =
  94. ('ALIGN','DB','DW','DD','DQ','END');
  95. { problems with shl,shr,not,and,or and xor, they are }
  96. { context sensitive. }
  97. _asmoperators : array[0.._count_asmoperators] of tasmkeyword = (
  98. 'BYTE','WORD','DWORD','QWORD','TBYTE','DQWORD','OWORD','XMMWORD','YWORD','YMMWORD','NEAR','FAR','HIGH',
  99. 'LOW','OFFSET','SIZEOF','VMTOFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  100. 'OR','XOR','WRT','GOTPCREL');
  101. token2str : array[tasmtoken] of string[10] = (
  102. '','Label','LLabel','String','Integer',
  103. ',',',',',',',',',','[',']','(',
  104. ')',':','.','+','-','*',
  105. ';','identifier','register','opcode','/',
  106. '','','','','','END',
  107. '','','','','','','','','',
  108. '','','sizeof','vmtoffset','','type','ptr','mod','shl','shr','not',
  109. 'and','or','xor','wrt','..gotpcrel'
  110. );
  111. var
  112. inexpression : boolean;
  113. constructor tx86intreader.create;
  114. var
  115. i : tasmop;
  116. Begin
  117. inherited create;
  118. iasmops:=TFPHashList.create;
  119. for i:=firstop to lastop do
  120. iasmops.Add(upper(std_op2str[i]),Pointer(PtrInt(i)));
  121. end;
  122. {---------------------------------------------------------------------}
  123. { Routines for the tokenizing }
  124. {---------------------------------------------------------------------}
  125. function tx86intreader.is_asmopcode(const s: string):boolean;
  126. var
  127. cond : string[4];
  128. cnd : tasmcond;
  129. j: longint;
  130. Begin
  131. is_asmopcode:=FALSE;
  132. actopcode:=A_None;
  133. actcondition:=C_None;
  134. actopsize:=S_NO;
  135. { Search opcodes }
  136. actopcode:=tasmop(PtrUInt(iasmops.Find(s)));
  137. if actopcode<>A_NONE then
  138. begin
  139. actasmtoken:=AS_OPCODE;
  140. result:=TRUE;
  141. exit;
  142. end;
  143. { not found yet, check condition opcodes }
  144. j:=0;
  145. while (j<CondAsmOps) do
  146. begin
  147. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  148. begin
  149. cond:=Copy(s,Length(CondAsmOpStr[j])+1,255);
  150. if cond<>'' then
  151. begin
  152. for cnd:=low(TasmCond) to high(TasmCond) do
  153. if Cond=Upper(cond2str[cnd]) then
  154. begin
  155. actopcode:=CondASmOp[j];
  156. actcondition:=cnd;
  157. is_asmopcode:=TRUE;
  158. actasmtoken:=AS_OPCODE;
  159. exit
  160. end;
  161. end;
  162. end;
  163. inc(j);
  164. end;
  165. end;
  166. function tx86intreader.is_asmoperator(const s: string):boolean;
  167. var
  168. i : longint;
  169. Begin
  170. for i:=0 to _count_asmoperators do
  171. if s=_asmoperators[i] then
  172. begin
  173. actasmtoken:=tasmtoken(longint(firstoperator)+i);
  174. is_asmoperator:=true;
  175. exit;
  176. end;
  177. is_asmoperator:=false;
  178. end;
  179. Function tx86intreader.is_asmdirective(const s: string):boolean;
  180. var
  181. i : longint;
  182. Begin
  183. for i:=0 to _count_asmdirectives do
  184. if s=_asmdirectives[i] then
  185. begin
  186. actasmtoken:=tasmtoken(longint(firstdirective)+i);
  187. is_asmdirective:=true;
  188. exit;
  189. end;
  190. is_asmdirective:=false;
  191. end;
  192. function tx86intreader.is_register(const s:string):boolean;
  193. var
  194. entry: TSymEntry;
  195. begin
  196. is_register:=false;
  197. actasmregister:=masm_regnum_search(lower(s));
  198. if (actasmregister=NR_NO) and
  199. (current_procinfo.procdef.proccalloption=pocall_register) and
  200. (po_assembler in current_procinfo.procdef.procoptions) then
  201. begin
  202. entry:=current_procinfo.procdef.parast.Find(s);
  203. if assigned(entry) and
  204. (entry.typ=paravarsym) and
  205. assigned(tparavarsym(entry).paraloc[calleeside].Location) and
  206. (tparavarsym(entry).paraloc[calleeside].Location^.Loc=LOC_REGISTER) then
  207. actasmregister:=tparavarsym(entry).paraloc[calleeside].Location^.register;
  208. end;
  209. if actasmregister<>NR_NO then
  210. begin
  211. is_register:=true;
  212. actasmtoken:=AS_REGISTER;
  213. end;
  214. end;
  215. function tx86intreader.is_locallabel(const s:string):boolean;
  216. begin
  217. is_locallabel:=(length(s)>1) and (s[1]='@');
  218. end;
  219. Procedure tx86intreader.GetToken;
  220. var
  221. len : longint;
  222. forcelabel : boolean;
  223. srsym : tsym;
  224. srsymtable : TSymtable;
  225. begin
  226. { save old token and reset new token }
  227. prevasmtoken:=actasmtoken;
  228. actasmtoken:=AS_NONE;
  229. { reset }
  230. forcelabel:=FALSE;
  231. actasmpattern:='';
  232. { while space and tab , continue scan... }
  233. while (c in [' ',#9]) do
  234. c:=current_scanner.asmgetchar;
  235. { get token pos }
  236. if not (c in [#10,#13,'{',';']) then
  237. current_scanner.gettokenpos;
  238. { Local Label, Label, Directive, Prefix or Opcode }
  239. if firsttoken and not (c in [#10,#13,'{',';']) then
  240. begin
  241. firsttoken:=FALSE;
  242. len:=0;
  243. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  244. begin
  245. { if there is an at_sign, then this must absolutely be a label }
  246. if c = '@' then
  247. forcelabel:=TRUE;
  248. inc(len);
  249. actasmpattern[len]:=c;
  250. c:=current_scanner.asmgetchar;
  251. end;
  252. actasmpattern[0]:=chr(len);
  253. uppervar(actasmpattern);
  254. { allow spaces }
  255. while (c in [' ',#9]) do
  256. c:=current_scanner.asmgetchar;
  257. { label ? }
  258. if c = ':' then
  259. begin
  260. if actasmpattern[1]='@' then
  261. actasmtoken:=AS_LLABEL
  262. else
  263. actasmtoken:=AS_LABEL;
  264. { let us point to the next character }
  265. c:=current_scanner.asmgetchar;
  266. firsttoken:=true;
  267. exit;
  268. end;
  269. { Are we trying to create an identifier with }
  270. { an at-sign...? }
  271. if forcelabel then
  272. Message(asmr_e_none_label_contain_at);
  273. { opcode ? }
  274. If is_asmopcode(actasmpattern) then
  275. Begin
  276. { check if we are in an expression }
  277. { then continue with asm directives }
  278. if not inexpression then
  279. exit;
  280. end;
  281. if is_asmdirective(actasmpattern) then
  282. exit;
  283. message1(asmr_e_unknown_opcode,actasmpattern);
  284. actasmtoken:=AS_NONE;
  285. exit;
  286. end
  287. else { else firsttoken }
  288. begin
  289. case c of
  290. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  291. { - @Result, @Code or @Data special variables. }
  292. begin
  293. actasmpattern:=c;
  294. c:=current_scanner.asmgetchar;
  295. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  296. begin
  297. actasmpattern:=actasmpattern + c;
  298. c:=current_scanner.asmgetchar;
  299. end;
  300. uppervar(actasmpattern);
  301. actasmtoken:=AS_ID;
  302. exit;
  303. end;
  304. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  305. begin
  306. actasmpattern:=c;
  307. c:=current_scanner.asmgetchar;
  308. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  309. begin
  310. actasmpattern:=actasmpattern + c;
  311. c:=current_scanner.asmgetchar;
  312. end;
  313. uppervar(actasmpattern);
  314. { after prefix we allow also a new opcode }
  315. If is_prefix(actopcode) and is_asmopcode(actasmpattern) then
  316. Begin
  317. { if we are not in a constant }
  318. { expression than this is an }
  319. { opcode. }
  320. if not inexpression then
  321. exit;
  322. end;
  323. { support st(X) for fpu registers }
  324. if (actasmpattern = 'ST') and (c='(') then
  325. Begin
  326. actasmpattern:=actasmpattern+c;
  327. c:=current_scanner.asmgetchar;
  328. { allow spaces }
  329. while (c in [' ',#9]) do
  330. c:=current_scanner.asmgetchar;
  331. if c in ['0'..'7'] then
  332. actasmpattern:=actasmpattern + c
  333. else
  334. Message(asmr_e_invalid_fpu_register);
  335. c:=current_scanner.asmgetchar;
  336. { allow spaces }
  337. while (c in [' ',#9]) do
  338. c:=current_scanner.asmgetchar;
  339. if c <> ')' then
  340. Message(asmr_e_invalid_fpu_register)
  341. else
  342. Begin
  343. actasmpattern:=actasmpattern + c;
  344. c:=current_scanner.asmgetchar;
  345. end;
  346. end;
  347. if is_asmdirective(actasmpattern) then
  348. exit;
  349. if is_asmoperator(actasmpattern) then
  350. exit;
  351. if is_register(actasmpattern) then
  352. exit;
  353. { allow spaces }
  354. while (c in [' ',#9]) do
  355. c:=current_scanner.asmgetchar;
  356. { if next is a '.' and this is a unitsym then we also need to
  357. parse the identifier }
  358. if (c='.') then
  359. begin
  360. asmsearchsym(actasmpattern,srsym,srsymtable);
  361. if assigned(srsym) and
  362. (srsym.typ=unitsym) and
  363. (srsym.owner.symtabletype in [staticsymtable,globalsymtable]) and
  364. srsym.owner.iscurrentunit then
  365. begin
  366. { Add . to create System.Identifier }
  367. actasmpattern:=actasmpattern+c;
  368. c:=current_scanner.asmgetchar;
  369. { Delphi allows System.@Halt, just ignore the @ }
  370. if c='@' then
  371. c:=current_scanner.asmgetchar;
  372. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  373. begin
  374. actasmpattern:=actasmpattern + upcase(c);
  375. c:=current_scanner.asmgetchar;
  376. end;
  377. end;
  378. end;
  379. actasmtoken:=AS_ID;
  380. exit;
  381. end;
  382. '''' : { string or character }
  383. begin
  384. actasmpattern:='';
  385. current_scanner.in_asm_string:=true;
  386. repeat
  387. if c = '''' then
  388. begin
  389. c:=current_scanner.asmgetchar;
  390. if c in [#10,#13] then
  391. begin
  392. Message(scan_f_string_exceeds_line);
  393. break;
  394. end;
  395. repeat
  396. if c='''' then
  397. begin
  398. c:=current_scanner.asmgetchar;
  399. if c='''' then
  400. begin
  401. actasmpattern:=actasmpattern+'''';
  402. c:=current_scanner.asmgetchar;
  403. if c in [#10,#13] then
  404. begin
  405. Message(scan_f_string_exceeds_line);
  406. break;
  407. end;
  408. end
  409. else
  410. break;
  411. end
  412. else
  413. begin
  414. actasmpattern:=actasmpattern+c;
  415. c:=current_scanner.asmgetchar;
  416. if c in [#10,#13] then
  417. begin
  418. Message(scan_f_string_exceeds_line);
  419. break
  420. end;
  421. end;
  422. until false; { end repeat }
  423. end
  424. else
  425. break; { end if }
  426. until false;
  427. current_scanner.in_asm_string:=false;
  428. actasmtoken:=AS_STRING;
  429. exit;
  430. end;
  431. '"' : { string or character }
  432. begin
  433. current_scanner.in_asm_string:=true;
  434. actasmpattern:='';
  435. repeat
  436. if c = '"' then
  437. begin
  438. c:=current_scanner.asmgetchar;
  439. if c in [#10,#13] then
  440. begin
  441. Message(scan_f_string_exceeds_line);
  442. break;
  443. end;
  444. repeat
  445. if c='"' then
  446. begin
  447. c:=current_scanner.asmgetchar;
  448. if c='"' then
  449. begin
  450. actasmpattern:=actasmpattern+'"';
  451. c:=current_scanner.asmgetchar;
  452. if c in [#10,#13] then
  453. begin
  454. Message(scan_f_string_exceeds_line);
  455. break;
  456. end;
  457. end
  458. else
  459. break;
  460. end
  461. else
  462. begin
  463. actasmpattern:=actasmpattern+c;
  464. c:=current_scanner.asmgetchar;
  465. if c in [#10,#13] then
  466. begin
  467. Message(scan_f_string_exceeds_line);
  468. break
  469. end;
  470. end;
  471. until false; { end repeat }
  472. end
  473. else
  474. break; { end if }
  475. until false;
  476. current_scanner.in_asm_string:=false;
  477. actasmtoken:=AS_STRING;
  478. exit;
  479. end;
  480. '$' :
  481. begin
  482. c:=current_scanner.asmgetchar;
  483. while c in ['0'..'9','A'..'F','a'..'f'] do
  484. begin
  485. actasmpattern:=actasmpattern + c;
  486. c:=current_scanner.asmgetchar;
  487. end;
  488. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  489. actasmtoken:=AS_INTNUM;
  490. exit;
  491. end;
  492. '&' : { identifier }
  493. begin
  494. actasmpattern:='';
  495. c:=current_scanner.asmgetchar;
  496. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  497. begin
  498. actasmpattern:=actasmpattern + c;
  499. c:=current_scanner.asmgetchar;
  500. end;
  501. uppervar(actasmpattern);
  502. actasmtoken:=AS_ID;
  503. exit;
  504. end;
  505. ',' :
  506. begin
  507. actasmtoken:=AS_COMMA;
  508. c:=current_scanner.asmgetchar;
  509. exit;
  510. end;
  511. '[' :
  512. begin
  513. actasmtoken:=AS_LBRACKET;
  514. c:=current_scanner.asmgetchar;
  515. exit;
  516. end;
  517. ']' :
  518. begin
  519. actasmtoken:=AS_RBRACKET;
  520. c:=current_scanner.asmgetchar;
  521. exit;
  522. end;
  523. '(' :
  524. begin
  525. actasmtoken:=AS_LPAREN;
  526. c:=current_scanner.asmgetchar;
  527. exit;
  528. end;
  529. ')' :
  530. begin
  531. actasmtoken:=AS_RPAREN;
  532. c:=current_scanner.asmgetchar;
  533. exit;
  534. end;
  535. ':' :
  536. begin
  537. actasmtoken:=AS_COLON;
  538. c:=current_scanner.asmgetchar;
  539. exit;
  540. end;
  541. '.' :
  542. begin
  543. c:=current_scanner.asmgetchar;
  544. {$ifdef x86_64}
  545. if c='.' then
  546. begin
  547. actasmpattern:='..';
  548. c:=current_scanner.asmgetchar;
  549. repeat
  550. actasmpattern:=actasmpattern+c;
  551. c:=current_scanner.asmgetchar;
  552. until not(c in ['A'..'Z','a'..'z','0'..'9','_']);
  553. if upper(actasmpattern)<>'..GOTPCREL' then
  554. begin
  555. actasmtoken:=AS_ID;
  556. consume(AS___GOTPCREL);
  557. end;
  558. actasmtoken:=AS___GOTPCREL
  559. end
  560. else
  561. {$endif x86_64}
  562. actasmtoken:=AS_DOT;
  563. exit;
  564. end;
  565. '+' :
  566. begin
  567. actasmtoken:=AS_PLUS;
  568. c:=current_scanner.asmgetchar;
  569. exit;
  570. end;
  571. '-' :
  572. begin
  573. actasmtoken:=AS_MINUS;
  574. c:=current_scanner.asmgetchar;
  575. exit;
  576. end;
  577. '*' :
  578. begin
  579. actasmtoken:=AS_STAR;
  580. c:=current_scanner.asmgetchar;
  581. exit;
  582. end;
  583. '/' :
  584. begin
  585. actasmtoken:=AS_SLASH;
  586. c:=current_scanner.asmgetchar;
  587. exit;
  588. end;
  589. '0'..'9':
  590. begin
  591. actasmpattern:=c;
  592. c:=current_scanner.asmgetchar;
  593. { Get the possible characters }
  594. while c in ['0'..'9','A'..'F','a'..'f'] do
  595. begin
  596. actasmpattern:=actasmpattern + c;
  597. c:=current_scanner.asmgetchar;
  598. end;
  599. { Get ending character }
  600. uppervar(actasmpattern);
  601. c:=upcase(c);
  602. { possibly a binary number. }
  603. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  604. Begin
  605. { Delete the last binary specifier }
  606. delete(actasmpattern,length(actasmpattern),1);
  607. actasmpattern:=tostr(ParseVal(actasmpattern,2));
  608. actasmtoken:=AS_INTNUM;
  609. exit;
  610. end
  611. else
  612. Begin
  613. case c of
  614. 'O' :
  615. Begin
  616. actasmpattern:=tostr(ParseVal(actasmpattern,8));
  617. actasmtoken:=AS_INTNUM;
  618. c:=current_scanner.asmgetchar;
  619. exit;
  620. end;
  621. 'H' :
  622. Begin
  623. actasmpattern:=tostr(ParseVal(actasmpattern,16));
  624. actasmtoken:=AS_INTNUM;
  625. c:=current_scanner.asmgetchar;
  626. exit;
  627. end;
  628. else { must be an integer number }
  629. begin
  630. actasmpattern:=tostr(ParseVal(actasmpattern,10));
  631. actasmtoken:=AS_INTNUM;
  632. exit;
  633. end;
  634. end;
  635. end;
  636. end;
  637. ';','{',#13,#10 :
  638. begin
  639. c:=current_scanner.asmgetchar;
  640. firsttoken:=TRUE;
  641. actasmtoken:=AS_SEPARATOR;
  642. exit;
  643. end;
  644. else
  645. current_scanner.illegal_char(c);
  646. end;
  647. end;
  648. end;
  649. function tx86intreader.consume(t : tasmtoken):boolean;
  650. begin
  651. Consume:=true;
  652. if t<>actasmtoken then
  653. begin
  654. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  655. Consume:=false;
  656. end;
  657. repeat
  658. gettoken;
  659. until actasmtoken<>AS_NONE;
  660. end;
  661. procedure tx86intreader.RecoverConsume(allowcomma:boolean);
  662. begin
  663. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  664. begin
  665. if allowcomma and (actasmtoken=AS_COMMA) then
  666. break;
  667. Consume(actasmtoken);
  668. end;
  669. end;
  670. {*****************************************************************************
  671. Parsing Helpers
  672. *****************************************************************************}
  673. { This routine builds up a record offset after a AS_DOT
  674. token is encountered.
  675. On entry actasmtoken should be equal to AS_DOT }
  676. Procedure tx86intreader.BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint; var mangledname: string; needvmtofs: boolean);
  677. var
  678. s: string;
  679. Begin
  680. offset:=0;
  681. size:=0;
  682. s:=expr;
  683. while (actasmtoken=AS_DOT) do
  684. begin
  685. Consume(AS_DOT);
  686. if actasmtoken in [AS_BYTE,AS_ID,AS_WORD,AS_DWORD,AS_QWORD,AS_OWORD,AS_XMMWORD,AS_YWORD,AS_YMMWORD,AS_REGISTER] then
  687. begin
  688. s:=s+'.'+actasmpattern;
  689. consume(actasmtoken);
  690. end
  691. else
  692. begin
  693. Consume(AS_ID);
  694. RecoverConsume(true);
  695. break;
  696. end;
  697. end;
  698. if not GetRecordOffsetSize(s,offset,size,mangledname,needvmtofs) then
  699. Message(asmr_e_building_record_offset);
  700. end;
  701. Procedure tx86intreader.BuildConstSymbolExpression(needofs,isref,startingminus:boolean;var value:aint;var asmsym:string;var asmsymtyp:TAsmsymtype);
  702. var
  703. tempstr,expr,hs,mangledname : string;
  704. parenlevel : longint;
  705. l,k : aint;
  706. hasparen,
  707. errorflag,
  708. needvmtofs : boolean;
  709. prevtok : tasmtoken;
  710. hl : tasmlabel;
  711. hssymtyp : Tasmsymtype;
  712. def : tdef;
  713. sym : tsym;
  714. srsymtable : TSymtable;
  715. Begin
  716. { reset }
  717. value:=0;
  718. asmsym:='';
  719. asmsymtyp:=AT_DATA;
  720. errorflag:=FALSE;
  721. tempstr:='';
  722. expr:='';
  723. if startingminus then
  724. expr:='-';
  725. inexpression:=TRUE;
  726. parenlevel:=0;
  727. sym:=nil;
  728. needvmtofs:=FALSE;
  729. Repeat
  730. { Support ugly delphi constructs like: [ECX].1+2[EDX] }
  731. if isref and (actasmtoken=AS_LBRACKET) then
  732. break;
  733. Case actasmtoken of
  734. AS_LPAREN:
  735. Begin
  736. Consume(AS_LPAREN);
  737. expr:=expr + '(';
  738. inc(parenlevel);
  739. end;
  740. AS_RPAREN:
  741. Begin
  742. { Keep the AS_PAREN in actasmtoken, it is maybe a typecast }
  743. if parenlevel=0 then
  744. break;
  745. Consume(AS_RPAREN);
  746. expr:=expr + ')';
  747. dec(parenlevel);
  748. end;
  749. AS_SHL:
  750. Begin
  751. Consume(AS_SHL);
  752. expr:=expr + '<';
  753. end;
  754. AS_SHR:
  755. Begin
  756. Consume(AS_SHR);
  757. expr:=expr + '>';
  758. end;
  759. AS_SLASH:
  760. Begin
  761. Consume(AS_SLASH);
  762. expr:=expr + '/';
  763. end;
  764. AS_MOD:
  765. Begin
  766. Consume(AS_MOD);
  767. expr:=expr + '%';
  768. end;
  769. AS_STAR:
  770. Begin
  771. Consume(AS_STAR);
  772. if isref and (actasmtoken=AS_REGISTER) then
  773. break;
  774. expr:=expr + '*';
  775. end;
  776. AS_PLUS:
  777. Begin
  778. Consume(AS_PLUS);
  779. if isref and (actasmtoken=AS_REGISTER) then
  780. break;
  781. expr:=expr + '+';
  782. end;
  783. AS_MINUS:
  784. Begin
  785. Consume(AS_MINUS);
  786. expr:=expr + '-';
  787. end;
  788. AS_AND:
  789. Begin
  790. Consume(AS_AND);
  791. expr:=expr + '&';
  792. end;
  793. AS_NOT:
  794. Begin
  795. Consume(AS_NOT);
  796. expr:=expr + '~';
  797. end;
  798. AS_XOR:
  799. Begin
  800. Consume(AS_XOR);
  801. expr:=expr + '^';
  802. end;
  803. AS_OR:
  804. Begin
  805. Consume(AS_OR);
  806. expr:=expr + '|';
  807. end;
  808. AS_INTNUM:
  809. Begin
  810. expr:=expr + actasmpattern;
  811. Consume(AS_INTNUM);
  812. end;
  813. AS_VMTOFFSET,
  814. AS_OFFSET:
  815. begin
  816. if (actasmtoken = AS_OFFSET) then
  817. needofs:=true
  818. else
  819. needvmtofs:=true;
  820. Consume(actasmtoken);
  821. if actasmtoken<>AS_ID then
  822. Message(asmr_e_offset_without_identifier);
  823. end;
  824. AS_SIZEOF,
  825. AS_TYPE:
  826. begin
  827. l:=0;
  828. hasparen:=false;
  829. Consume(actasmtoken);
  830. if actasmtoken=AS_LPAREN then
  831. begin
  832. hasparen:=true;
  833. Consume(AS_LPAREN);
  834. end;
  835. if actasmtoken<>AS_ID then
  836. Message(asmr_e_type_without_identifier)
  837. else
  838. begin
  839. tempstr:=actasmpattern;
  840. Consume(AS_ID);
  841. if actasmtoken=AS_DOT then
  842. begin
  843. BuildRecordOffsetSize(tempstr,k,l,mangledname,false);
  844. if mangledname<>'' then
  845. { procsym }
  846. Message(asmr_e_wrong_sym_type);
  847. end
  848. else
  849. begin
  850. asmsearchsym(tempstr,sym,srsymtable);
  851. if assigned(sym) then
  852. begin
  853. case sym.typ of
  854. staticvarsym,
  855. localvarsym,
  856. paravarsym :
  857. l:=tabstractvarsym(sym).getsize;
  858. typesym :
  859. l:=ttypesym(sym).typedef.size;
  860. else
  861. Message(asmr_e_wrong_sym_type);
  862. end;
  863. end
  864. else
  865. Message1(sym_e_unknown_id,tempstr);
  866. end;
  867. end;
  868. str(l, tempstr);
  869. expr:=expr + tempstr;
  870. if hasparen then
  871. Consume(AS_RPAREN);
  872. end;
  873. AS_PTR :
  874. begin
  875. { Support ugly delphi constructs like <constant> PTR [ref] }
  876. break;
  877. end;
  878. AS_STRING:
  879. begin
  880. l:=0;
  881. case Length(actasmpattern) of
  882. 1 :
  883. l:=ord(actasmpattern[1]);
  884. 2 :
  885. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  886. 3 :
  887. l:=ord(actasmpattern[3]) +
  888. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  889. 4 :
  890. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  891. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  892. else
  893. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  894. end;
  895. str(l, tempstr);
  896. expr:=expr + tempstr;
  897. Consume(AS_STRING);
  898. end;
  899. AS_ID:
  900. begin
  901. hs:='';
  902. hssymtyp:=AT_DATA;
  903. def:=nil;
  904. tempstr:=actasmpattern;
  905. prevtok:=prevasmtoken;
  906. { stop parsing a constant expression if we find an opcode after a
  907. non-operator like "db $66 mov eax,ebx" }
  908. if (prevtok in [AS_ID,AS_INTNUM,AS_RPAREN]) and
  909. is_asmopcode(actasmpattern) then
  910. break;
  911. consume(AS_ID);
  912. if SearchIConstant(tempstr,l) then
  913. begin
  914. str(l, tempstr);
  915. expr:=expr + tempstr;
  916. end
  917. else
  918. begin
  919. if is_locallabel(tempstr) then
  920. begin
  921. CreateLocalLabel(tempstr,hl,false);
  922. hs:=hl.name;
  923. hssymtyp:=AT_FUNCTION;
  924. end
  925. else
  926. if SearchLabel(tempstr,hl,false) then
  927. begin
  928. hs:=hl.name;
  929. hssymtyp:=AT_FUNCTION;
  930. end
  931. else
  932. begin
  933. asmsearchsym(tempstr,sym,srsymtable);
  934. if assigned(sym) then
  935. begin
  936. case sym.typ of
  937. staticvarsym :
  938. begin
  939. hs:=tstaticvarsym(sym).mangledname;
  940. def:=tstaticvarsym(sym).vardef;
  941. end;
  942. localvarsym,
  943. paravarsym :
  944. begin
  945. Message(asmr_e_no_local_or_para_allowed);
  946. end;
  947. procsym :
  948. begin
  949. if Tprocsym(sym).ProcdefList.Count>1 then
  950. Message(asmr_w_calling_overload_func);
  951. hs:=tprocdef(tprocsym(sym).ProcdefList[0]).mangledname;
  952. hssymtyp:=AT_FUNCTION;
  953. end;
  954. typesym :
  955. begin
  956. if not(ttypesym(sym).typedef.typ in [recorddef,objectdef]) then
  957. Message(asmr_e_wrong_sym_type);
  958. end;
  959. fieldvarsym :
  960. begin
  961. tempstr:=upper(tdef(sym.owner.defowner).GetTypeName)+'.'+tempstr;
  962. end;
  963. else
  964. Message(asmr_e_wrong_sym_type);
  965. end;
  966. end
  967. else
  968. Message1(sym_e_unknown_id,tempstr);
  969. end;
  970. { symbol found? }
  971. if hs<>'' then
  972. begin
  973. if asmsym='' then
  974. begin
  975. asmsym:=hs;
  976. asmsymtyp:=hssymtyp;
  977. end
  978. else
  979. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  980. if (expr='') or (expr[length(expr)]='+') then
  981. begin
  982. { don't remove the + if there could be a record field }
  983. if actasmtoken<>AS_DOT then
  984. delete(expr,length(expr),1);
  985. end
  986. else
  987. if needofs then
  988. begin
  989. if (prevtok<>AS_OFFSET) then
  990. Message(asmr_e_need_offset);
  991. end
  992. else
  993. Message(asmr_e_only_add_relocatable_symbol);
  994. end;
  995. if (actasmtoken=AS_DOT) or
  996. (assigned(sym) and
  997. (sym.typ = fieldvarsym)) then
  998. begin
  999. BuildRecordOffsetSize(tempstr,l,k,hs,needvmtofs);
  1000. if hs <> '' then
  1001. hssymtyp:=AT_FUNCTION
  1002. else
  1003. begin
  1004. str(l, tempstr);
  1005. expr:=expr + tempstr;
  1006. end
  1007. end
  1008. else
  1009. begin
  1010. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  1011. delete(expr,length(expr),1);
  1012. end;
  1013. if (actasmtoken=AS_LBRACKET) and
  1014. assigned(def) and
  1015. (def.typ=arraydef) then
  1016. begin
  1017. consume(AS_LBRACKET);
  1018. l:=BuildConstExpression;
  1019. if l<tarraydef(def).lowrange then
  1020. begin
  1021. Message(asmr_e_constant_out_of_bounds);
  1022. l:=0;
  1023. end
  1024. else
  1025. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  1026. str(l, tempstr);
  1027. expr:=expr + '+' + tempstr;
  1028. consume(AS_RBRACKET);
  1029. end;
  1030. end;
  1031. { check if there are wrong operator used like / or mod etc. }
  1032. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  1033. Message(asmr_e_only_add_relocatable_symbol);
  1034. end;
  1035. AS_ALIGN,
  1036. AS_DB,
  1037. AS_DW,
  1038. AS_DD,
  1039. AS_DQ,
  1040. AS_END,
  1041. AS_RBRACKET,
  1042. AS_SEPARATOR,
  1043. AS_COMMA,
  1044. AS_COLON:
  1045. break;
  1046. else
  1047. begin
  1048. { write error only once. }
  1049. if not errorflag then
  1050. Message(asmr_e_invalid_constant_expression);
  1051. { consume tokens until we find COMMA or SEPARATOR }
  1052. Consume(actasmtoken);
  1053. errorflag:=TRUE;
  1054. end;
  1055. end;
  1056. Until false;
  1057. { calculate expression }
  1058. if not ErrorFlag then
  1059. value:=CalculateExpression(expr)
  1060. else
  1061. value:=0;
  1062. { no longer in an expression }
  1063. inexpression:=FALSE;
  1064. end;
  1065. Function tx86intreader.BuildConstExpression:aint;
  1066. var
  1067. l : aint;
  1068. hs : string;
  1069. hssymtyp : TAsmsymtype;
  1070. begin
  1071. BuildConstSymbolExpression(false,false,false,l,hs,hssymtyp);
  1072. if hs<>'' then
  1073. Message(asmr_e_relocatable_symbol_not_allowed);
  1074. BuildConstExpression:=l;
  1075. end;
  1076. Function tx86intreader.BuildRefConstExpression:aint;
  1077. var
  1078. l : aint;
  1079. hs : string;
  1080. hssymtyp : TAsmsymtype;
  1081. begin
  1082. BuildConstSymbolExpression(false,true,false,l,hs,hssymtyp);
  1083. if hs<>'' then
  1084. Message(asmr_e_relocatable_symbol_not_allowed);
  1085. BuildRefConstExpression:=l;
  1086. end;
  1087. procedure tx86intreader.BuildReference(oper : tx86operand);
  1088. var
  1089. scale : byte;
  1090. k,l : aint;
  1091. tempstr,hs : string;
  1092. tempsymtyp : tasmsymtype;
  1093. code : integer;
  1094. hreg : tregister;
  1095. GotStar,GotOffset,HadVar,
  1096. GotPlus,Negative : boolean;
  1097. hl : tasmlabel;
  1098. Begin
  1099. Consume(AS_LBRACKET);
  1100. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  1101. oper.InitRef;
  1102. GotStar:=false;
  1103. GotPlus:=true;
  1104. GotOffset:=false;
  1105. Negative:=false;
  1106. Scale:=0;
  1107. repeat
  1108. if GotOffset and (actasmtoken<>AS_ID) then
  1109. Message(asmr_e_invalid_reference_syntax);
  1110. Case actasmtoken of
  1111. AS_ID, { Constant reference expression OR variable reference expression }
  1112. AS_VMTOFFSET:
  1113. Begin
  1114. if not GotPlus then
  1115. Message(asmr_e_invalid_reference_syntax);
  1116. GotStar:=false;
  1117. GotPlus:=false;
  1118. if (actasmtoken = AS_VMTOFFSET) or
  1119. (SearchIConstant(actasmpattern,l) or
  1120. SearchRecordType(actasmpattern)) then
  1121. begin
  1122. l:=BuildRefConstExpression;
  1123. GotPlus:=(prevasmtoken=AS_PLUS);
  1124. GotStar:=(prevasmtoken=AS_STAR);
  1125. case oper.opr.typ of
  1126. OPR_LOCAL :
  1127. begin
  1128. if GotStar then
  1129. Message(asmr_e_invalid_reference_syntax);
  1130. if negative then
  1131. Dec(oper.opr.localsymofs,l)
  1132. else
  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. begin
  1141. if negative then
  1142. Dec(oper.opr.ref.offset,l)
  1143. else
  1144. Inc(oper.opr.ref.offset,l);
  1145. end;
  1146. end;
  1147. end;
  1148. end
  1149. else
  1150. Begin
  1151. if negative and not oper.hasvar then
  1152. Message(asmr_e_only_add_relocatable_symbol)
  1153. else if oper.hasvar and not GotOffset and
  1154. (not negative or assigned(oper.opr.ref.relsymbol)) then
  1155. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1156. HadVar:=oper.hasvar and GotOffset;
  1157. tempstr:=actasmpattern;
  1158. Consume(AS_ID);
  1159. { typecasting? }
  1160. if (actasmtoken=AS_LPAREN) and
  1161. SearchType(tempstr,l) then
  1162. begin
  1163. oper.hastype:=true;
  1164. oper.typesize:=l;
  1165. Consume(AS_LPAREN);
  1166. BuildOperand(oper,true);
  1167. Consume(AS_RPAREN);
  1168. end
  1169. else
  1170. if is_locallabel(tempstr) then
  1171. begin
  1172. CreateLocalLabel(tempstr,hl,false);
  1173. oper.InitRef;
  1174. if not negative then
  1175. begin
  1176. oper.opr.ref.symbol:=hl;
  1177. oper.hasvar:=true;
  1178. end
  1179. else
  1180. oper.opr.ref.relsymbol:=hl;
  1181. end
  1182. else
  1183. if oper.SetupVar(tempstr,GotOffset) then
  1184. begin
  1185. { force OPR_LOCAL to be a reference }
  1186. if oper.opr.typ=OPR_LOCAL then
  1187. oper.opr.localforceref:=true
  1188. else
  1189. begin
  1190. {$ifdef x86_64}
  1191. if actasmtoken=AS_WRT then
  1192. begin
  1193. if (oper.opr.typ=OPR_REFERENCE) then
  1194. begin
  1195. Consume(AS_WRT);
  1196. Consume(AS___GOTPCREL);
  1197. if (oper.opr.ref.base<>NR_NO) or
  1198. (oper.opr.ref.index<>NR_NO) or
  1199. (oper.opr.ref.offset<>0) then
  1200. Message(asmr_e_wrong_gotpcrel_intel_syntax);
  1201. if tf_no_pic_supported in target_info.flags then
  1202. Message(asmr_e_no_gotpcrel_support);
  1203. oper.opr.ref.refaddr:=addr_pic;
  1204. oper.opr.ref.base:=NR_RIP;
  1205. end
  1206. else
  1207. message(asmr_e_invalid_reference_syntax);
  1208. end;
  1209. {$endif x86_64}
  1210. end;
  1211. end
  1212. else
  1213. Message1(sym_e_unknown_id,tempstr);
  1214. { record.field ? }
  1215. if actasmtoken=AS_DOT then
  1216. begin
  1217. BuildRecordOffsetSize(tempstr,l,k,hs,false);
  1218. if (hs<>'') then
  1219. Message(asmr_e_invalid_symbol_ref);
  1220. case oper.opr.typ of
  1221. OPR_LOCAL :
  1222. inc(oper.opr.localsymofs,l);
  1223. OPR_REFERENCE :
  1224. inc(oper.opr.ref.offset,l);
  1225. end;
  1226. oper.SetSize(k,false);
  1227. end;
  1228. if GotOffset then
  1229. begin
  1230. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1231. begin
  1232. if (oper.opr.typ=OPR_REFERENCE) then
  1233. oper.opr.ref.base:=NR_NO;
  1234. oper.hasvar:=hadvar;
  1235. end
  1236. else
  1237. begin
  1238. if oper.hasvar and hadvar then
  1239. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1240. { should we allow ?? }
  1241. end;
  1242. end;
  1243. end;
  1244. GotOffset:=false;
  1245. end;
  1246. AS_PLUS :
  1247. Begin
  1248. Consume(AS_PLUS);
  1249. Negative:=false;
  1250. GotPlus:=true;
  1251. GotStar:=false;
  1252. Scale:=0;
  1253. end;
  1254. AS_DOT :
  1255. Begin
  1256. { Handle like a + }
  1257. Consume(AS_DOT);
  1258. Negative:=false;
  1259. GotPlus:=true;
  1260. GotStar:=false;
  1261. Scale:=0;
  1262. end;
  1263. AS_MINUS :
  1264. begin
  1265. Consume(AS_MINUS);
  1266. Negative:=true;
  1267. GotPlus:=true;
  1268. GotStar:=false;
  1269. Scale:=0;
  1270. end;
  1271. AS_STAR : { Scaling, with eax*4 order }
  1272. begin
  1273. Consume(AS_STAR);
  1274. hs:='';
  1275. l:=0;
  1276. case actasmtoken of
  1277. AS_LPAREN :
  1278. l:=BuildConstExpression;
  1279. AS_INTNUM:
  1280. Begin
  1281. hs:=actasmpattern;
  1282. Consume(AS_INTNUM);
  1283. end;
  1284. AS_REGISTER :
  1285. begin
  1286. case oper.opr.typ of
  1287. OPR_REFERENCE :
  1288. begin
  1289. if oper.opr.ref.scalefactor=0 then
  1290. begin
  1291. if scale<>0 then
  1292. begin
  1293. oper.opr.ref.scalefactor:=scale;
  1294. scale:=0;
  1295. end
  1296. else
  1297. Message(asmr_e_wrong_scale_factor);
  1298. end
  1299. else
  1300. Message(asmr_e_invalid_reference_syntax);
  1301. end;
  1302. OPR_LOCAL :
  1303. begin
  1304. if oper.opr.localscale=0 then
  1305. begin
  1306. if scale<>0 then
  1307. begin
  1308. oper.opr.localscale:=scale;
  1309. scale:=0;
  1310. end
  1311. else
  1312. Message(asmr_e_wrong_scale_factor);
  1313. end
  1314. else
  1315. Message(asmr_e_invalid_reference_syntax);
  1316. end;
  1317. end;
  1318. end;
  1319. else
  1320. Message(asmr_e_invalid_reference_syntax);
  1321. end;
  1322. if actasmtoken<>AS_REGISTER then
  1323. begin
  1324. if hs<>'' then
  1325. val(hs,l,code);
  1326. case oper.opr.typ of
  1327. OPR_REFERENCE :
  1328. oper.opr.ref.scalefactor:=l;
  1329. OPR_LOCAL :
  1330. oper.opr.localscale:=l;
  1331. end;
  1332. if l>9 then
  1333. Message(asmr_e_wrong_scale_factor);
  1334. end;
  1335. GotPlus:=false;
  1336. GotStar:=false;
  1337. end;
  1338. AS_REGISTER :
  1339. begin
  1340. if not((GotPlus and (not Negative)) or
  1341. GotStar) then
  1342. Message(asmr_e_invalid_reference_syntax);
  1343. hreg:=actasmregister;
  1344. Consume(AS_REGISTER);
  1345. { this register will be the index:
  1346. 1. just read a *
  1347. 2. next token is a *
  1348. 3. base register is already used }
  1349. case oper.opr.typ of
  1350. OPR_LOCAL :
  1351. begin
  1352. if (oper.opr.localindexreg<>NR_NO) then
  1353. Message(asmr_e_multiple_index);
  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. begin
  1880. PrefixOp:=A_None;
  1881. OverrideOp:=A_None;
  1882. is_far_const:=false;
  1883. { prefix seg opcode / prefix opcode }
  1884. repeat
  1885. if is_prefix(actopcode) then
  1886. with instr do
  1887. begin
  1888. OpOrder:=op_intel;
  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. OpOrder:=op_intel;
  1901. OverrideOp:=ActOpcode;
  1902. opcode:=ActOpcode;
  1903. condition:=ActCondition;
  1904. opsize:=ActOpsize;
  1905. ConcatInstruction(curlist);
  1906. consume(AS_OPCODE);
  1907. end
  1908. else
  1909. break;
  1910. { allow for newline after prefix or override }
  1911. while actasmtoken=AS_SEPARATOR do
  1912. consume(AS_SEPARATOR);
  1913. until (actasmtoken<>AS_OPCODE);
  1914. { opcode }
  1915. if (actasmtoken <> AS_OPCODE) then
  1916. begin
  1917. Message(asmr_e_invalid_or_missing_opcode);
  1918. RecoverConsume(false);
  1919. exit;
  1920. end;
  1921. { Fill the instr object with the current state }
  1922. with instr do
  1923. begin
  1924. OpOrder:=op_intel;
  1925. Opcode:=ActOpcode;
  1926. condition:=ActCondition;
  1927. opsize:=ActOpsize;
  1928. { Valid combination of prefix/override and instruction ? }
  1929. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1930. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1931. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1932. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1933. end;
  1934. { pushf/popf/pusha/popa have to default to 16 bit in Intel mode
  1935. (Intel manual and Delphi-compatbile) -- setting the opsize for
  1936. these instructions doesn't change anything in the internal assember,
  1937. so change the opcode }
  1938. if (instr.opcode=A_POPF) then
  1939. instr.opcode:=A_POPFW
  1940. else if (instr.opcode=A_PUSHF) then
  1941. instr.opcode:=A_PUSHFW
  1942. else if (instr.opcode=A_PUSHA) then
  1943. instr.opcode:=A_PUSHAW
  1944. else if (instr.opcode=A_POPA) then
  1945. instr.opcode:=A_POPAW;
  1946. { We are reading operands, so opcode will be an AS_ID }
  1947. operandnum:=1;
  1948. is_far_const:=false;
  1949. Consume(AS_OPCODE);
  1950. { Zero operand opcode ? }
  1951. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1952. begin
  1953. operandnum:=0;
  1954. exit;
  1955. end;
  1956. { Read Operands }
  1957. repeat
  1958. case actasmtoken of
  1959. { End of asm operands for this opcode }
  1960. AS_END,
  1961. AS_SEPARATOR :
  1962. break;
  1963. { Operand delimiter }
  1964. AS_COMMA :
  1965. begin
  1966. if operandnum > Max_Operands then
  1967. Message(asmr_e_too_many_operands)
  1968. else
  1969. Inc(operandnum);
  1970. Consume(AS_COMMA);
  1971. end;
  1972. {Far constant, i.e. jmp $0000:$11111111.}
  1973. AS_COLON:
  1974. begin
  1975. is_far_const:=true;
  1976. if operandnum>1 then
  1977. message(asmr_e_too_many_operands)
  1978. else
  1979. inc(operandnum);
  1980. consume(AS_COLON);
  1981. end;
  1982. { Type specifier }
  1983. AS_NEAR,
  1984. AS_FAR :
  1985. begin
  1986. if actasmtoken = AS_NEAR then
  1987. begin
  1988. Message(asmr_w_near_ignored);
  1989. instr.opsize:=S_NEAR;
  1990. end
  1991. else
  1992. begin
  1993. Message(asmr_w_far_ignored);
  1994. instr.opsize:=S_FAR;
  1995. end;
  1996. Consume(actasmtoken);
  1997. if actasmtoken=AS_PTR then
  1998. begin
  1999. Consume(AS_PTR);
  2000. instr.Operands[operandnum].InitRef;
  2001. end;
  2002. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2003. end;
  2004. else
  2005. BuildOperand(instr.Operands[operandnum] as tx86operand,false);
  2006. end; { end case }
  2007. until false;
  2008. instr.ops:=operandnum;
  2009. { Check operands }
  2010. for i:=1 to operandnum do
  2011. begin
  2012. if is_far_const and
  2013. (instr.operands[i].opr.typ<>OPR_CONSTANT) then
  2014. message(asmr_e_expr_illegal)
  2015. else
  2016. if instr.operands[i].opr.typ=OPR_NONE then
  2017. Message(asmr_e_syntax_error);
  2018. end;
  2019. { e.g. for "push dword 1", "push word 6" }
  2020. if (instr.ops=1) and
  2021. (instr.operands[1].typesize<>0) then
  2022. instr.operands[1].setsize(instr.operands[1].typesize,false);
  2023. end;
  2024. Procedure tx86intreader.BuildConstant(constsize: byte);
  2025. var
  2026. asmsymtyp : tasmsymtype;
  2027. asmsym,
  2028. expr: string;
  2029. value : aint;
  2030. Begin
  2031. Repeat
  2032. Case actasmtoken of
  2033. AS_STRING:
  2034. Begin
  2035. { DD and DW cases }
  2036. if constsize <> 1 then
  2037. Begin
  2038. if Not PadZero(actasmpattern,constsize) then
  2039. Message(scan_f_string_exceeds_line);
  2040. end;
  2041. expr:=actasmpattern;
  2042. Consume(AS_STRING);
  2043. Case actasmtoken of
  2044. AS_COMMA:
  2045. Consume(AS_COMMA);
  2046. AS_END,
  2047. AS_SEPARATOR: ;
  2048. else
  2049. Message(asmr_e_invalid_string_expression);
  2050. end;
  2051. ConcatString(curlist,expr);
  2052. end;
  2053. AS_PLUS,
  2054. AS_MINUS,
  2055. AS_LPAREN,
  2056. AS_NOT,
  2057. AS_INTNUM,
  2058. AS_ID :
  2059. Begin
  2060. BuildConstSymbolExpression(false,false,false,value,asmsym,asmsymtyp);
  2061. if asmsym<>'' then
  2062. begin
  2063. if constsize<>sizeof(pint) then
  2064. Message1(asmr_w_const32bit_for_address,asmsym);
  2065. ConcatConstSymbol(curlist,asmsym,asmsymtyp,value)
  2066. end
  2067. else
  2068. ConcatConstant(curlist,value,constsize);
  2069. end;
  2070. AS_COMMA:
  2071. begin
  2072. Consume(AS_COMMA);
  2073. end;
  2074. AS_ALIGN,
  2075. AS_DB,
  2076. AS_DW,
  2077. AS_DD,
  2078. AS_DQ,
  2079. AS_OPCODE,
  2080. AS_END,
  2081. AS_SEPARATOR:
  2082. break;
  2083. else
  2084. begin
  2085. Message(asmr_e_syn_constant);
  2086. RecoverConsume(false);
  2087. end
  2088. end;
  2089. Until false;
  2090. end;
  2091. function tx86intreader.Assemble: tlinkedlist;
  2092. Var
  2093. hl : tasmlabel;
  2094. instr : Tx86Instruction;
  2095. Begin
  2096. Message1(asmr_d_start_reading,'intel');
  2097. inexpression:=FALSE;
  2098. firsttoken:=TRUE;
  2099. { sets up all opcode and register tables in uppercase
  2100. done in the construtor now
  2101. if not _asmsorted then
  2102. Begin
  2103. SetupTables;
  2104. _asmsorted:=TRUE;
  2105. end;
  2106. }
  2107. curlist:=TAsmList.Create;
  2108. { setup label linked list }
  2109. LocalLabelList:=TLocalLabelList.Create;
  2110. { we might need to know which parameters are passed in registers }
  2111. current_procinfo.generate_parameter_info;
  2112. { start tokenizer }
  2113. c:=current_scanner.asmgetcharstart;
  2114. gettoken;
  2115. { main loop }
  2116. repeat
  2117. case actasmtoken of
  2118. AS_LLABEL:
  2119. Begin
  2120. if CreateLocalLabel(actasmpattern,hl,true) then
  2121. ConcatLabel(curlist,hl);
  2122. Consume(AS_LLABEL);
  2123. end;
  2124. AS_LABEL:
  2125. Begin
  2126. if SearchLabel(upper(actasmpattern),hl,true) then
  2127. ConcatLabel(curlist,hl)
  2128. else
  2129. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  2130. Consume(AS_LABEL);
  2131. end;
  2132. AS_DW :
  2133. Begin
  2134. inexpression:=true;
  2135. Consume(AS_DW);
  2136. BuildConstant(2);
  2137. inexpression:=false;
  2138. end;
  2139. AS_DB :
  2140. Begin
  2141. inexpression:=true;
  2142. Consume(AS_DB);
  2143. BuildConstant(1);
  2144. inexpression:=false;
  2145. end;
  2146. AS_DD :
  2147. Begin
  2148. inexpression:=true;
  2149. Consume(AS_DD);
  2150. BuildConstant(4);
  2151. inexpression:=false;
  2152. end;
  2153. {$ifdef cpu64bitaddr}
  2154. AS_DQ:
  2155. Begin
  2156. inexpression:=true;
  2157. Consume(AS_DQ);
  2158. BuildConstant(8);
  2159. inexpression:=false;
  2160. end;
  2161. {$endif cpu64bitaddr}
  2162. AS_ALIGN:
  2163. Begin
  2164. Consume(AS_ALIGN);
  2165. ConcatAlign(curlist,BuildConstExpression);
  2166. if actasmtoken<>AS_SEPARATOR then
  2167. Consume(AS_SEPARATOR);
  2168. end;
  2169. AS_OPCODE :
  2170. Begin
  2171. instr:=Tx86Instruction.Create(Tx86Operand);
  2172. BuildOpcode(instr);
  2173. with instr do
  2174. begin
  2175. { We need AT&T style operands }
  2176. Swapoperands;
  2177. { Must be done with args in ATT order }
  2178. CheckNonCommutativeOpcodes;
  2179. AddReferenceSizes;
  2180. SetInstructionOpsize;
  2181. CheckOperandSizes;
  2182. ConcatInstruction(curlist);
  2183. end;
  2184. instr.Free;
  2185. end;
  2186. AS_SEPARATOR :
  2187. Begin
  2188. Consume(AS_SEPARATOR);
  2189. end;
  2190. AS_END :
  2191. break; { end assembly block }
  2192. else
  2193. Begin
  2194. Message(asmr_e_syntax_error);
  2195. RecoverConsume(false);
  2196. end;
  2197. end; { end case }
  2198. until false;
  2199. { Check LocalLabelList }
  2200. LocalLabelList.CheckEmitted;
  2201. LocalLabelList.Free;
  2202. { Return the list in an asmnode }
  2203. assemble:=curlist;
  2204. Message1(asmr_d_finish_reading,'intel');
  2205. end;
  2206. end.