ra386int.pas 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 by Carl Eric Codere and Peter Vreman
  4. Does the parsing process for the intel styled inline assembler.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. ****************************************************************************
  17. }
  18. Unit Ra386int;
  19. {$i fpcdefs.inc}
  20. interface
  21. uses
  22. cclasses,
  23. cpubase,
  24. globtype,
  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_END,
  35. {------------------ Assembler Operators --------------------}
  36. AS_BYTE,AS_WORD,AS_DWORD,AS_QWORD,AS_TBYTE,AS_DQWORD,AS_NEAR,AS_FAR,
  37. AS_HIGH,AS_LOW,AS_OFFSET,AS_SEG,AS_TYPE,AS_PTR,AS_MOD,AS_SHL,AS_SHR,AS_NOT,
  38. AS_AND,AS_OR,AS_XOR);
  39. type
  40. ti386intreader = 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);
  55. procedure BuildConstSymbolExpression(needofs,isref:boolean;var value:aint;var asmsym:string);
  56. function BuildConstExpression:aint;
  57. function BuildRefConstExpression:aint;
  58. procedure BuildReference(oper : tx86operand);
  59. procedure BuildOperand(oper: tx86operand);
  60. procedure BuildConstantOperand(oper: tx86operand);
  61. procedure BuildOpCode(instr : tx86instruction);
  62. procedure BuildConstant(constsize: longint);
  63. end;
  64. implementation
  65. uses
  66. { common }
  67. cutils,
  68. { global }
  69. globals,verbose,
  70. systems,
  71. { aasm }
  72. cpuinfo,aasmbase,aasmtai,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[6];
  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_XOR;
  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','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','NEAR','FAR','HIGH',
  99. 'LOW','OFFSET','SEG','TYPE','PTR','MOD','SHL','SHR','NOT','AND',
  100. 'OR','XOR');
  101. token2str : array[tasmtoken] of string[10] = (
  102. '','Label','LLabel','String','Integer',
  103. ',','[',']','(',
  104. ')',':','.','+','-','*',
  105. ';','identifier','register','opcode','/',
  106. '','','','','END',
  107. '','','','','','','','','',
  108. '','','','type','ptr','mod','shl','shr','not',
  109. 'and','or','xor'
  110. );
  111. var
  112. inexpression : boolean;
  113. constructor ti386intreader.create;
  114. var
  115. i : tasmop;
  116. str2opentry: tstr2opentry;
  117. Begin
  118. inherited create;
  119. { opcodes }
  120. { creates uppercased symbol tables for speed access }
  121. iasmops:=tdictionary.create;
  122. iasmops.delete_doubles:=true;
  123. for i:=firstop to lastop do
  124. begin
  125. str2opentry:=tstr2opentry.createname(upper(std_op2str[i]));
  126. str2opentry.op:=i;
  127. iasmops.insert(str2opentry);
  128. end;
  129. end;
  130. {---------------------------------------------------------------------}
  131. { Routines for the tokenizing }
  132. {---------------------------------------------------------------------}
  133. function ti386intreader.is_asmopcode(const s: string):boolean;
  134. var
  135. str2opentry: tstr2opentry;
  136. cond : string[4];
  137. cnd : tasmcond;
  138. j: longint;
  139. Begin
  140. is_asmopcode:=FALSE;
  141. actopcode:=A_None;
  142. actcondition:=C_None;
  143. actopsize:=S_NO;
  144. str2opentry:=tstr2opentry(iasmops.search(s));
  145. if assigned(str2opentry) then
  146. begin
  147. actopcode:=str2opentry.op;
  148. actasmtoken:=AS_OPCODE;
  149. is_asmopcode:=TRUE;
  150. exit;
  151. end;
  152. { not found yet, check condition opcodes }
  153. j:=0;
  154. while (j<CondAsmOps) do
  155. begin
  156. if Copy(s,1,Length(CondAsmOpStr[j]))=CondAsmOpStr[j] then
  157. begin
  158. cond:=Copy(s,Length(CondAsmOpStr[j])+1,255);
  159. if cond<>'' then
  160. begin
  161. for cnd:=low(TasmCond) to high(TasmCond) do
  162. if Cond=Upper(cond2str[cnd]) then
  163. begin
  164. actopcode:=CondASmOp[j];
  165. actcondition:=cnd;
  166. is_asmopcode:=TRUE;
  167. actasmtoken:=AS_OPCODE;
  168. exit
  169. end;
  170. end;
  171. end;
  172. inc(j);
  173. end;
  174. end;
  175. function ti386intreader.is_asmoperator(const s: string):boolean;
  176. var
  177. i : longint;
  178. Begin
  179. for i:=0 to _count_asmoperators do
  180. if s=_asmoperators[i] then
  181. begin
  182. actasmtoken:=tasmtoken(longint(firstoperator)+i);
  183. is_asmoperator:=true;
  184. exit;
  185. end;
  186. is_asmoperator:=false;
  187. end;
  188. Function ti386intreader.is_asmdirective(const s: string):boolean;
  189. var
  190. i : longint;
  191. Begin
  192. for i:=0 to _count_asmdirectives do
  193. if s=_asmdirectives[i] then
  194. begin
  195. actasmtoken:=tasmtoken(longint(firstdirective)+i);
  196. is_asmdirective:=true;
  197. exit;
  198. end;
  199. is_asmdirective:=false;
  200. end;
  201. function ti386intreader.is_register(const s:string):boolean;
  202. begin
  203. is_register:=false;
  204. actasmregister:=masm_regnum_search(lower(s));
  205. if actasmregister<>NR_NO then
  206. begin
  207. is_register:=true;
  208. actasmtoken:=AS_REGISTER;
  209. end;
  210. end;
  211. function ti386intreader.is_locallabel(const s:string):boolean;
  212. begin
  213. is_locallabel:=(length(s)>1) and (s[1]='@');
  214. end;
  215. Procedure ti386intreader.GetToken;
  216. var
  217. len : longint;
  218. forcelabel : boolean;
  219. srsym : tsym;
  220. srsymtable : tsymtable;
  221. begin
  222. { save old token and reset new token }
  223. prevasmtoken:=actasmtoken;
  224. actasmtoken:=AS_NONE;
  225. { reset }
  226. forcelabel:=FALSE;
  227. actasmpattern:='';
  228. { while space and tab , continue scan... }
  229. while (c in [' ',#9]) do
  230. c:=current_scanner.asmgetchar;
  231. { get token pos }
  232. if not (c in [#10,#13,'{',';']) then
  233. current_scanner.gettokenpos;
  234. { Local Label, Label, Directive, Prefix or Opcode }
  235. if firsttoken and not (c in [#10,#13,'{',';']) then
  236. begin
  237. firsttoken:=FALSE;
  238. len:=0;
  239. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  240. begin
  241. { if there is an at_sign, then this must absolutely be a label }
  242. if c = '@' then
  243. forcelabel:=TRUE;
  244. inc(len);
  245. actasmpattern[len]:=c;
  246. c:=current_scanner.asmgetchar;
  247. end;
  248. actasmpattern[0]:=chr(len);
  249. uppervar(actasmpattern);
  250. { label ? }
  251. if c = ':' then
  252. begin
  253. if actasmpattern[1]='@' then
  254. actasmtoken:=AS_LLABEL
  255. else
  256. actasmtoken:=AS_LABEL;
  257. { let us point to the next character }
  258. c:=current_scanner.asmgetchar;
  259. firsttoken:=true;
  260. exit;
  261. end;
  262. { Are we trying to create an identifier with }
  263. { an at-sign...? }
  264. if forcelabel then
  265. Message(asmr_e_none_label_contain_at);
  266. { opcode ? }
  267. If is_asmopcode(actasmpattern) then
  268. Begin
  269. { check if we are in an expression }
  270. { then continue with asm directives }
  271. if not inexpression then
  272. exit;
  273. end;
  274. if is_asmdirective(actasmpattern) then
  275. exit;
  276. message1(asmr_e_unknown_opcode,actasmpattern);
  277. actasmtoken:=AS_NONE;
  278. exit;
  279. end
  280. else { else firsttoken }
  281. begin
  282. case c of
  283. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  284. { - @Result, @Code or @Data special variables. }
  285. begin
  286. actasmpattern:=c;
  287. c:=current_scanner.asmgetchar;
  288. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  289. begin
  290. actasmpattern:=actasmpattern + c;
  291. c:=current_scanner.asmgetchar;
  292. end;
  293. uppervar(actasmpattern);
  294. actasmtoken:=AS_ID;
  295. exit;
  296. end;
  297. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  298. begin
  299. actasmpattern:=c;
  300. c:=current_scanner.asmgetchar;
  301. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  302. begin
  303. actasmpattern:=actasmpattern + c;
  304. c:=current_scanner.asmgetchar;
  305. end;
  306. uppervar(actasmpattern);
  307. { after prefix we allow also a new opcode }
  308. If is_prefix(actopcode) and is_asmopcode(actasmpattern) then
  309. Begin
  310. { if we are not in a constant }
  311. { expression than this is an }
  312. { opcode. }
  313. if not inexpression then
  314. exit;
  315. end;
  316. { support st(X) for fpu registers }
  317. if (actasmpattern = 'ST') and (c='(') then
  318. Begin
  319. actasmpattern:=actasmpattern+c;
  320. c:=current_scanner.asmgetchar;
  321. if c in ['0'..'7'] then
  322. actasmpattern:=actasmpattern + c
  323. else
  324. Message(asmr_e_invalid_fpu_register);
  325. c:=current_scanner.asmgetchar;
  326. if c <> ')' then
  327. Message(asmr_e_invalid_fpu_register)
  328. else
  329. Begin
  330. actasmpattern:=actasmpattern + c;
  331. c:=current_scanner.asmgetchar;
  332. end;
  333. end;
  334. if is_register(actasmpattern) then
  335. exit;
  336. if is_asmdirective(actasmpattern) then
  337. exit;
  338. if is_asmoperator(actasmpattern) then
  339. exit;
  340. { if next is a '.' and this is a unitsym then we also need to
  341. parse the identifier }
  342. if (c='.') then
  343. begin
  344. searchsym(actasmpattern,srsym,srsymtable);
  345. if assigned(srsym) and
  346. (srsym.typ=unitsym) and
  347. (srsym.owner.unitid=0) then
  348. begin
  349. { Add . to create System.Identifier }
  350. actasmpattern:=actasmpattern+c;
  351. c:=current_scanner.asmgetchar;
  352. { Delphi allows System.@Halt, just ignore the @ }
  353. if c='@' then
  354. c:=current_scanner.asmgetchar;
  355. while c in ['A'..'Z','a'..'z','0'..'9','_','$'] do
  356. begin
  357. actasmpattern:=actasmpattern + upcase(c);
  358. c:=current_scanner.asmgetchar;
  359. end;
  360. end;
  361. end;
  362. actasmtoken:=AS_ID;
  363. exit;
  364. end;
  365. '''' : { string or character }
  366. begin
  367. actasmpattern:='';
  368. current_scanner.in_asm_string:=true;
  369. repeat
  370. if c = '''' then
  371. begin
  372. c:=current_scanner.asmgetchar;
  373. if c in [#10,#13] then
  374. begin
  375. Message(scan_f_string_exceeds_line);
  376. break;
  377. end;
  378. repeat
  379. if c='''' then
  380. begin
  381. c:=current_scanner.asmgetchar;
  382. if c='''' then
  383. begin
  384. actasmpattern:=actasmpattern+'''';
  385. c:=current_scanner.asmgetchar;
  386. if c in [#10,#13] then
  387. begin
  388. Message(scan_f_string_exceeds_line);
  389. break;
  390. end;
  391. end
  392. else
  393. break;
  394. end
  395. else
  396. begin
  397. actasmpattern:=actasmpattern+c;
  398. c:=current_scanner.asmgetchar;
  399. if c in [#10,#13] then
  400. begin
  401. Message(scan_f_string_exceeds_line);
  402. break
  403. end;
  404. end;
  405. until false; { end repeat }
  406. end
  407. else
  408. break; { end if }
  409. until false;
  410. current_scanner.in_asm_string:=false;
  411. actasmtoken:=AS_STRING;
  412. exit;
  413. end;
  414. '"' : { string or character }
  415. begin
  416. current_scanner.in_asm_string:=true;
  417. actasmpattern:='';
  418. repeat
  419. if c = '"' then
  420. begin
  421. c:=current_scanner.asmgetchar;
  422. if c in [#10,#13] then
  423. begin
  424. Message(scan_f_string_exceeds_line);
  425. break;
  426. end;
  427. repeat
  428. if c='"' then
  429. begin
  430. c:=current_scanner.asmgetchar;
  431. if c='"' then
  432. begin
  433. actasmpattern:=actasmpattern+'"';
  434. c:=current_scanner.asmgetchar;
  435. if c in [#10,#13] then
  436. begin
  437. Message(scan_f_string_exceeds_line);
  438. break;
  439. end;
  440. end
  441. else
  442. break;
  443. end
  444. else
  445. begin
  446. actasmpattern:=actasmpattern+c;
  447. c:=current_scanner.asmgetchar;
  448. if c in [#10,#13] then
  449. begin
  450. Message(scan_f_string_exceeds_line);
  451. break
  452. end;
  453. end;
  454. until false; { end repeat }
  455. end
  456. else
  457. break; { end if }
  458. until false;
  459. current_scanner.in_asm_string:=false;
  460. actasmtoken:=AS_STRING;
  461. exit;
  462. end;
  463. '$' :
  464. begin
  465. c:=current_scanner.asmgetchar;
  466. while c in ['0'..'9','A'..'F','a'..'f'] do
  467. begin
  468. actasmpattern:=actasmpattern + c;
  469. c:=current_scanner.asmgetchar;
  470. end;
  471. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  472. actasmtoken:=AS_INTNUM;
  473. exit;
  474. end;
  475. ',' :
  476. begin
  477. actasmtoken:=AS_COMMA;
  478. c:=current_scanner.asmgetchar;
  479. exit;
  480. end;
  481. '[' :
  482. begin
  483. actasmtoken:=AS_LBRACKET;
  484. c:=current_scanner.asmgetchar;
  485. exit;
  486. end;
  487. ']' :
  488. begin
  489. actasmtoken:=AS_RBRACKET;
  490. c:=current_scanner.asmgetchar;
  491. exit;
  492. end;
  493. '(' :
  494. begin
  495. actasmtoken:=AS_LPAREN;
  496. c:=current_scanner.asmgetchar;
  497. exit;
  498. end;
  499. ')' :
  500. begin
  501. actasmtoken:=AS_RPAREN;
  502. c:=current_scanner.asmgetchar;
  503. exit;
  504. end;
  505. ':' :
  506. begin
  507. actasmtoken:=AS_COLON;
  508. c:=current_scanner.asmgetchar;
  509. exit;
  510. end;
  511. '.' :
  512. begin
  513. actasmtoken:=AS_DOT;
  514. c:=current_scanner.asmgetchar;
  515. exit;
  516. end;
  517. '+' :
  518. begin
  519. actasmtoken:=AS_PLUS;
  520. c:=current_scanner.asmgetchar;
  521. exit;
  522. end;
  523. '-' :
  524. begin
  525. actasmtoken:=AS_MINUS;
  526. c:=current_scanner.asmgetchar;
  527. exit;
  528. end;
  529. '*' :
  530. begin
  531. actasmtoken:=AS_STAR;
  532. c:=current_scanner.asmgetchar;
  533. exit;
  534. end;
  535. '/' :
  536. begin
  537. actasmtoken:=AS_SLASH;
  538. c:=current_scanner.asmgetchar;
  539. exit;
  540. end;
  541. '0'..'9':
  542. begin
  543. actasmpattern:=c;
  544. c:=current_scanner.asmgetchar;
  545. { Get the possible characters }
  546. while c in ['0'..'9','A'..'F','a'..'f'] do
  547. begin
  548. actasmpattern:=actasmpattern + c;
  549. c:=current_scanner.asmgetchar;
  550. end;
  551. { Get ending character }
  552. uppervar(actasmpattern);
  553. c:=upcase(c);
  554. { possibly a binary number. }
  555. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  556. Begin
  557. { Delete the last binary specifier }
  558. delete(actasmpattern,length(actasmpattern),1);
  559. actasmpattern:=tostr(ValBinary(actasmpattern));
  560. actasmtoken:=AS_INTNUM;
  561. exit;
  562. end
  563. else
  564. Begin
  565. case c of
  566. 'O' :
  567. Begin
  568. actasmpattern:=tostr(ValOctal(actasmpattern));
  569. actasmtoken:=AS_INTNUM;
  570. c:=current_scanner.asmgetchar;
  571. exit;
  572. end;
  573. 'H' :
  574. Begin
  575. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  576. actasmtoken:=AS_INTNUM;
  577. c:=current_scanner.asmgetchar;
  578. exit;
  579. end;
  580. else { must be an integer number }
  581. begin
  582. actasmpattern:=tostr(ValDecimal(actasmpattern));
  583. actasmtoken:=AS_INTNUM;
  584. exit;
  585. end;
  586. end;
  587. end;
  588. end;
  589. ';','{',#13,#10 :
  590. begin
  591. c:=current_scanner.asmgetchar;
  592. firsttoken:=TRUE;
  593. actasmtoken:=AS_SEPARATOR;
  594. exit;
  595. end;
  596. else
  597. current_scanner.illegal_char(c);
  598. end;
  599. end;
  600. end;
  601. function ti386intreader.consume(t : tasmtoken):boolean;
  602. begin
  603. Consume:=true;
  604. if t<>actasmtoken then
  605. begin
  606. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  607. Consume:=false;
  608. end;
  609. repeat
  610. gettoken;
  611. until actasmtoken<>AS_NONE;
  612. end;
  613. procedure ti386intreader.RecoverConsume(allowcomma:boolean);
  614. begin
  615. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  616. begin
  617. if allowcomma and (actasmtoken=AS_COMMA) then
  618. break;
  619. Consume(actasmtoken);
  620. end;
  621. end;
  622. {*****************************************************************************
  623. Parsing Helpers
  624. *****************************************************************************}
  625. { This routine builds up a record offset after a AS_DOT
  626. token is encountered.
  627. On entry actasmtoken should be equal to AS_DOT }
  628. Procedure ti386intreader.BuildRecordOffsetSize(const expr: string;var offset:aint;var size:aint);
  629. var
  630. s : string;
  631. Begin
  632. offset:=0;
  633. size:=0;
  634. s:=expr;
  635. while (actasmtoken=AS_DOT) do
  636. begin
  637. Consume(AS_DOT);
  638. if actasmtoken=AS_ID then
  639. s:=s+'.'+actasmpattern;
  640. if not Consume(AS_ID) then
  641. begin
  642. RecoverConsume(true);
  643. break;
  644. end;
  645. end;
  646. if not GetRecordOffsetSize(s,offset,size) then
  647. Message(asmr_e_building_record_offset);
  648. end;
  649. Procedure ti386intreader.BuildConstSymbolExpression(needofs,isref:boolean;var value:aint;var asmsym:string);
  650. var
  651. tempstr,expr,hs : string;
  652. parenlevel : longint;
  653. l,k : aint;
  654. hasparen,
  655. errorflag : boolean;
  656. prevtok : tasmtoken;
  657. hl : tasmlabel;
  658. def : tdef;
  659. sym : tsym;
  660. srsymtable : tsymtable;
  661. Begin
  662. { reset }
  663. value:=0;
  664. asmsym:='';
  665. errorflag:=FALSE;
  666. tempstr:='';
  667. expr:='';
  668. inexpression:=TRUE;
  669. parenlevel:=0;
  670. Repeat
  671. Case actasmtoken of
  672. AS_LPAREN:
  673. Begin
  674. Consume(AS_LPAREN);
  675. expr:=expr + '(';
  676. inc(parenlevel);
  677. end;
  678. AS_RPAREN:
  679. Begin
  680. Consume(AS_RPAREN);
  681. expr:=expr + ')';
  682. dec(parenlevel);
  683. end;
  684. AS_SHL:
  685. Begin
  686. Consume(AS_SHL);
  687. expr:=expr + '<';
  688. end;
  689. AS_SHR:
  690. Begin
  691. Consume(AS_SHR);
  692. expr:=expr + '>';
  693. end;
  694. AS_SLASH:
  695. Begin
  696. Consume(AS_SLASH);
  697. expr:=expr + '/';
  698. end;
  699. AS_MOD:
  700. Begin
  701. Consume(AS_MOD);
  702. expr:=expr + '%';
  703. end;
  704. AS_STAR:
  705. Begin
  706. Consume(AS_STAR);
  707. if isref and (actasmtoken=AS_REGISTER) then
  708. break;
  709. expr:=expr + '*';
  710. end;
  711. AS_PLUS:
  712. Begin
  713. Consume(AS_PLUS);
  714. if isref and (actasmtoken=AS_REGISTER) then
  715. break;
  716. expr:=expr + '+';
  717. end;
  718. AS_LBRACKET:
  719. begin
  720. { Support ugly delphi constructs like: [ECX].1+2[EDX] }
  721. if isref then
  722. break;
  723. end;
  724. AS_MINUS:
  725. Begin
  726. Consume(AS_MINUS);
  727. expr:=expr + '-';
  728. end;
  729. AS_AND:
  730. Begin
  731. Consume(AS_AND);
  732. expr:=expr + '&';
  733. end;
  734. AS_NOT:
  735. Begin
  736. Consume(AS_NOT);
  737. expr:=expr + '~';
  738. end;
  739. AS_XOR:
  740. Begin
  741. Consume(AS_XOR);
  742. expr:=expr + '^';
  743. end;
  744. AS_OR:
  745. Begin
  746. Consume(AS_OR);
  747. expr:=expr + '|';
  748. end;
  749. AS_INTNUM:
  750. Begin
  751. expr:=expr + actasmpattern;
  752. Consume(AS_INTNUM);
  753. end;
  754. AS_OFFSET:
  755. begin
  756. Consume(AS_OFFSET);
  757. needofs:=true;
  758. if actasmtoken<>AS_ID then
  759. Message(asmr_e_offset_without_identifier);
  760. end;
  761. AS_TYPE:
  762. begin
  763. l:=0;
  764. hasparen:=false;
  765. Consume(AS_TYPE);
  766. if actasmtoken=AS_LPAREN then
  767. begin
  768. hasparen:=true;
  769. Consume(AS_LPAREN);
  770. end;
  771. if actasmtoken<>AS_ID then
  772. Message(asmr_e_type_without_identifier)
  773. else
  774. begin
  775. tempstr:=actasmpattern;
  776. Consume(AS_ID);
  777. if actasmtoken=AS_DOT then
  778. BuildRecordOffsetSize(tempstr,k,l)
  779. else
  780. begin
  781. searchsym(tempstr,sym,srsymtable);
  782. if assigned(sym) then
  783. begin
  784. case sym.typ of
  785. varsym :
  786. l:=tvarsym(sym).getsize;
  787. typedconstsym :
  788. l:=ttypedconstsym(sym).getsize;
  789. typesym :
  790. l:=ttypesym(sym).restype.def.size;
  791. else
  792. Message(asmr_e_wrong_sym_type);
  793. end;
  794. end
  795. else
  796. Message1(sym_e_unknown_id,tempstr);
  797. end;
  798. end;
  799. str(l, tempstr);
  800. expr:=expr + tempstr;
  801. if hasparen then
  802. Consume(AS_RPAREN);
  803. end;
  804. AS_STRING:
  805. Begin
  806. l:=0;
  807. case Length(actasmpattern) of
  808. 1 :
  809. l:=ord(actasmpattern[1]);
  810. 2 :
  811. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  812. 3 :
  813. l:=ord(actasmpattern[3]) +
  814. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  815. 4 :
  816. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  817. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  818. else
  819. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  820. end;
  821. str(l, tempstr);
  822. expr:=expr + tempstr;
  823. Consume(AS_STRING);
  824. end;
  825. AS_ID:
  826. Begin
  827. hs:='';
  828. def:=nil;
  829. tempstr:=actasmpattern;
  830. prevtok:=prevasmtoken;
  831. consume(AS_ID);
  832. if SearchIConstant(tempstr,l) then
  833. begin
  834. str(l, tempstr);
  835. expr:=expr + tempstr;
  836. end
  837. else
  838. begin
  839. if is_locallabel(tempstr) then
  840. begin
  841. CreateLocalLabel(tempstr,hl,false);
  842. hs:=hl.name
  843. end
  844. else
  845. if SearchLabel(tempstr,hl,false) then
  846. hs:=hl.name
  847. else
  848. begin
  849. searchsym(tempstr,sym,srsymtable);
  850. if assigned(sym) then
  851. begin
  852. case sym.typ of
  853. varsym :
  854. begin
  855. if sym.owner.symtabletype in [localsymtable,parasymtable] then
  856. Message(asmr_e_no_local_or_para_allowed);
  857. hs:=tvarsym(sym).mangledname;
  858. def:=tvarsym(sym).vartype.def;
  859. end;
  860. typedconstsym :
  861. begin
  862. hs:=ttypedconstsym(sym).mangledname;
  863. def:=ttypedconstsym(sym).typedconsttype.def;
  864. end;
  865. procsym :
  866. begin
  867. if Tprocsym(sym).procdef_count>1 then
  868. Message(asmr_w_calling_overload_func);
  869. hs:=tprocsym(sym).first_procdef.mangledname;
  870. end;
  871. typesym :
  872. begin
  873. if not(ttypesym(sym).restype.def.deftype in [recorddef,objectdef]) then
  874. Message(asmr_e_wrong_sym_type);
  875. end;
  876. else
  877. Message(asmr_e_wrong_sym_type);
  878. end;
  879. end
  880. else
  881. Message1(sym_e_unknown_id,tempstr);
  882. end;
  883. { symbol found? }
  884. if hs<>'' then
  885. begin
  886. if asmsym='' then
  887. asmsym:=hs
  888. else
  889. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  890. if (expr='') or (expr[length(expr)]='+') then
  891. begin
  892. { don't remove the + if there could be a record field }
  893. if actasmtoken<>AS_DOT then
  894. delete(expr,length(expr),1);
  895. end
  896. else
  897. if needofs then
  898. begin
  899. if (prevtok<>AS_OFFSET) then
  900. Message(asmr_e_need_offset);
  901. end
  902. else
  903. Message(asmr_e_only_add_relocatable_symbol);
  904. end;
  905. if actasmtoken=AS_DOT then
  906. begin
  907. BuildRecordOffsetSize(tempstr,l,k);
  908. str(l, tempstr);
  909. expr:=expr + tempstr;
  910. end
  911. else
  912. begin
  913. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  914. delete(expr,length(expr),1);
  915. end;
  916. if (actasmtoken=AS_LBRACKET) and
  917. assigned(def) and
  918. (def.deftype=arraydef) then
  919. begin
  920. consume(AS_LBRACKET);
  921. l:=BuildConstExpression;
  922. if l<tarraydef(def).lowrange then
  923. begin
  924. Message(asmr_e_constant_out_of_bounds);
  925. l:=0;
  926. end
  927. else
  928. l:=(l-tarraydef(def).lowrange)*tarraydef(def).elesize;
  929. str(l, tempstr);
  930. expr:=expr + '+' + tempstr;
  931. consume(AS_RBRACKET);
  932. end;
  933. end;
  934. { check if there are wrong operator used like / or mod etc. }
  935. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  936. Message(asmr_e_only_add_relocatable_symbol);
  937. end;
  938. AS_END,
  939. AS_RBRACKET,
  940. AS_SEPARATOR,
  941. AS_COMMA:
  942. Begin
  943. break;
  944. end;
  945. else
  946. Begin
  947. { write error only once. }
  948. if not errorflag then
  949. Message(asmr_e_invalid_constant_expression);
  950. { consume tokens until we find COMMA or SEPARATOR }
  951. Consume(actasmtoken);
  952. errorflag:=TRUE;
  953. end;
  954. end;
  955. Until false;
  956. { calculate expression }
  957. if not ErrorFlag then
  958. value:=CalculateExpression(expr)
  959. else
  960. value:=0;
  961. { no longer in an expression }
  962. inexpression:=FALSE;
  963. end;
  964. Function ti386intreader.BuildConstExpression:aint;
  965. var
  966. l : aint;
  967. hs : string;
  968. begin
  969. BuildConstSymbolExpression(false,false,l,hs);
  970. if hs<>'' then
  971. Message(asmr_e_relocatable_symbol_not_allowed);
  972. BuildConstExpression:=l;
  973. end;
  974. Function ti386intreader.BuildRefConstExpression:aint;
  975. var
  976. l : aint;
  977. hs : string;
  978. begin
  979. BuildConstSymbolExpression(false,true,l,hs);
  980. if hs<>'' then
  981. Message(asmr_e_relocatable_symbol_not_allowed);
  982. BuildRefConstExpression:=l;
  983. end;
  984. procedure ti386intreader.BuildReference(oper : tx86operand);
  985. var
  986. k,l,scale : aint;
  987. tempstr,hs : string;
  988. typesize : longint;
  989. code : integer;
  990. hreg : tregister;
  991. GotStar,GotOffset,HadVar,
  992. GotPlus,Negative : boolean;
  993. Begin
  994. Consume(AS_LBRACKET);
  995. if not(oper.opr.typ in [OPR_LOCAL,OPR_REFERENCE]) then
  996. oper.InitRef;
  997. GotStar:=false;
  998. GotPlus:=true;
  999. GotOffset:=false;
  1000. Negative:=false;
  1001. Scale:=0;
  1002. repeat
  1003. if GotOffset and (actasmtoken<>AS_ID) then
  1004. Message(asmr_e_invalid_reference_syntax);
  1005. Case actasmtoken of
  1006. AS_ID: { Constant reference expression OR variable reference expression }
  1007. Begin
  1008. if not GotPlus then
  1009. Message(asmr_e_invalid_reference_syntax);
  1010. if actasmpattern[1] = '@' then
  1011. Message(asmr_e_local_label_not_allowed_as_ref);
  1012. GotStar:=false;
  1013. GotPlus:=false;
  1014. if SearchIConstant(actasmpattern,l) or
  1015. SearchRecordType(actasmpattern) then
  1016. begin
  1017. l:=BuildRefConstExpression;
  1018. GotPlus:=(prevasmtoken=AS_PLUS);
  1019. GotStar:=(prevasmtoken=AS_STAR);
  1020. case oper.opr.typ of
  1021. OPR_LOCAL :
  1022. begin
  1023. if GotStar then
  1024. Message(asmr_e_invalid_reference_syntax);
  1025. if negative then
  1026. Dec(oper.opr.localsymofs,l)
  1027. else
  1028. Inc(oper.opr.localsymofs,l);
  1029. end;
  1030. OPR_REFERENCE :
  1031. begin
  1032. if GotStar then
  1033. oper.opr.ref.scalefactor:=l
  1034. else
  1035. begin
  1036. if negative then
  1037. Dec(oper.opr.ref.offset,l)
  1038. else
  1039. Inc(oper.opr.ref.offset,l);
  1040. end;
  1041. end;
  1042. end;
  1043. end
  1044. else
  1045. Begin
  1046. if oper.hasvar and not GotOffset then
  1047. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1048. HadVar:=oper.hasvar and GotOffset;
  1049. if negative then
  1050. Message(asmr_e_only_add_relocatable_symbol);
  1051. tempstr:=actasmpattern;
  1052. Consume(AS_ID);
  1053. { typecasting? }
  1054. if (actasmtoken=AS_LPAREN) and
  1055. SearchType(tempstr,typesize) then
  1056. begin
  1057. oper.hastype:=true;
  1058. Consume(AS_LPAREN);
  1059. BuildOperand(oper);
  1060. Consume(AS_RPAREN);
  1061. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  1062. oper.SetSize(typesize,true);
  1063. end
  1064. else
  1065. if not oper.SetupVar(tempstr,GotOffset) then
  1066. Message1(sym_e_unknown_id,tempstr);
  1067. { record.field ? }
  1068. if actasmtoken=AS_DOT then
  1069. begin
  1070. BuildRecordOffsetSize(tempstr,l,k);
  1071. case oper.opr.typ of
  1072. OPR_LOCAL :
  1073. inc(oper.opr.localsymofs,l);
  1074. OPR_REFERENCE :
  1075. inc(oper.opr.ref.offset,l);
  1076. end;
  1077. end;
  1078. if GotOffset then
  1079. begin
  1080. if oper.hasvar and (oper.opr.ref.base=current_procinfo.framepointer) then
  1081. begin
  1082. if (oper.opr.typ=OPR_REFERENCE) then
  1083. oper.opr.ref.base:=NR_NO;
  1084. oper.hasvar:=hadvar;
  1085. end
  1086. else
  1087. begin
  1088. if oper.hasvar and hadvar then
  1089. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1090. { should we allow ?? }
  1091. end;
  1092. end;
  1093. end;
  1094. GotOffset:=false;
  1095. end;
  1096. AS_PLUS :
  1097. Begin
  1098. Consume(AS_PLUS);
  1099. Negative:=false;
  1100. GotPlus:=true;
  1101. GotStar:=false;
  1102. Scale:=0;
  1103. end;
  1104. AS_MINUS :
  1105. begin
  1106. Consume(AS_MINUS);
  1107. Negative:=true;
  1108. GotPlus:=true;
  1109. GotStar:=false;
  1110. Scale:=0;
  1111. end;
  1112. AS_STAR : { Scaling, with eax*4 order }
  1113. begin
  1114. Consume(AS_STAR);
  1115. hs:='';
  1116. l:=0;
  1117. case actasmtoken of
  1118. AS_LPAREN :
  1119. l:=BuildConstExpression;
  1120. AS_INTNUM:
  1121. Begin
  1122. hs:=actasmpattern;
  1123. Consume(AS_INTNUM);
  1124. end;
  1125. AS_REGISTER :
  1126. begin
  1127. case oper.opr.typ of
  1128. OPR_REFERENCE :
  1129. begin
  1130. if oper.opr.ref.scalefactor=0 then
  1131. begin
  1132. if scale<>0 then
  1133. begin
  1134. oper.opr.ref.scalefactor:=scale;
  1135. scale:=0;
  1136. end
  1137. else
  1138. Message(asmr_e_wrong_scale_factor);
  1139. end
  1140. else
  1141. Message(asmr_e_invalid_reference_syntax);
  1142. end;
  1143. OPR_LOCAL :
  1144. begin
  1145. if oper.opr.localscale=0 then
  1146. begin
  1147. if scale<>0 then
  1148. begin
  1149. oper.opr.localscale:=scale;
  1150. scale:=0;
  1151. end
  1152. else
  1153. Message(asmr_e_wrong_scale_factor);
  1154. end
  1155. else
  1156. Message(asmr_e_invalid_reference_syntax);
  1157. end;
  1158. end;
  1159. end;
  1160. else
  1161. Message(asmr_e_invalid_reference_syntax);
  1162. end;
  1163. if actasmtoken<>AS_REGISTER then
  1164. begin
  1165. if hs<>'' then
  1166. val(hs,l,code);
  1167. case oper.opr.typ of
  1168. OPR_REFERENCE :
  1169. oper.opr.ref.scalefactor:=l;
  1170. OPR_LOCAL :
  1171. oper.opr.localscale:=l;
  1172. end;
  1173. if l>9 then
  1174. Message(asmr_e_wrong_scale_factor);
  1175. end;
  1176. GotPlus:=false;
  1177. GotStar:=false;
  1178. end;
  1179. AS_REGISTER :
  1180. begin
  1181. if not((GotPlus and (not Negative)) or
  1182. GotStar) then
  1183. Message(asmr_e_invalid_reference_syntax);
  1184. hreg:=actasmregister;
  1185. Consume(AS_REGISTER);
  1186. { this register will be the index:
  1187. 1. just read a *
  1188. 2. next token is a *
  1189. 3. base register is already used }
  1190. case oper.opr.typ of
  1191. OPR_LOCAL :
  1192. begin
  1193. if (oper.opr.localindexreg<>NR_NO) then
  1194. Message(asmr_e_multiple_index);
  1195. oper.opr.localindexreg:=hreg;
  1196. if scale<>0 then
  1197. begin
  1198. oper.opr.localscale:=scale;
  1199. scale:=0;
  1200. end;
  1201. end;
  1202. OPR_REFERENCE :
  1203. begin
  1204. if (GotStar) or
  1205. (actasmtoken=AS_STAR) or
  1206. (oper.opr.ref.base<>NR_NO) then
  1207. begin
  1208. if (oper.opr.ref.index<>NR_NO) then
  1209. Message(asmr_e_multiple_index);
  1210. oper.opr.ref.index:=hreg;
  1211. if scale<>0 then
  1212. begin
  1213. oper.opr.ref.scalefactor:=scale;
  1214. scale:=0;
  1215. end;
  1216. end
  1217. else
  1218. oper.opr.ref.base:=hreg;
  1219. end;
  1220. end;
  1221. GotPlus:=false;
  1222. GotStar:=false;
  1223. end;
  1224. AS_OFFSET :
  1225. begin
  1226. Consume(AS_OFFSET);
  1227. GotOffset:=true;
  1228. end;
  1229. AS_TYPE,
  1230. AS_NOT,
  1231. AS_STRING,
  1232. AS_INTNUM,
  1233. AS_LPAREN : { Constant reference expression }
  1234. begin
  1235. if not GotPlus and not GotStar then
  1236. Message(asmr_e_invalid_reference_syntax);
  1237. BuildConstSymbolExpression(true,true,l,tempstr);
  1238. if tempstr<>'' then
  1239. begin
  1240. if GotStar then
  1241. Message(asmr_e_only_add_relocatable_symbol);
  1242. if not assigned(oper.opr.ref.symbol) then
  1243. oper.opr.ref.symbol:=objectlibrary.newasmsymbol(tempstr,AB_EXTERNAL,AT_FUNCTION)
  1244. else
  1245. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1246. end;
  1247. case oper.opr.typ of
  1248. OPR_REFERENCE :
  1249. begin
  1250. if GotStar then
  1251. oper.opr.ref.scalefactor:=l
  1252. else if (prevasmtoken = AS_STAR) then
  1253. begin
  1254. if scale<>0 then
  1255. scale:=l*scale
  1256. else
  1257. scale:=l;
  1258. end
  1259. else
  1260. begin
  1261. if negative then
  1262. Dec(oper.opr.ref.offset,l)
  1263. else
  1264. Inc(oper.opr.ref.offset,l);
  1265. end;
  1266. end;
  1267. OPR_LOCAL :
  1268. begin
  1269. if GotStar then
  1270. oper.opr.localscale:=l
  1271. else if (prevasmtoken = AS_STAR) then
  1272. begin
  1273. if scale<>0 then
  1274. scale:=l*scale
  1275. else
  1276. scale:=l;
  1277. end
  1278. else
  1279. begin
  1280. if negative then
  1281. Dec(oper.opr.localsymofs,l)
  1282. else
  1283. Inc(oper.opr.localsymofs,l);
  1284. end;
  1285. end;
  1286. end;
  1287. GotPlus:=(prevasmtoken=AS_PLUS) or
  1288. (prevasmtoken=AS_MINUS);
  1289. if GotPlus then
  1290. negative := prevasmtoken = AS_MINUS;
  1291. GotStar:=(prevasmtoken=AS_STAR);
  1292. end;
  1293. AS_RBRACKET :
  1294. begin
  1295. if GotPlus or GotStar then
  1296. Message(asmr_e_invalid_reference_syntax);
  1297. Consume(AS_RBRACKET);
  1298. break;
  1299. end;
  1300. else
  1301. Begin
  1302. Message(asmr_e_invalid_reference_syntax);
  1303. RecoverConsume(true);
  1304. break;
  1305. end;
  1306. end;
  1307. until false;
  1308. end;
  1309. Procedure ti386intreader.BuildConstantOperand(oper: tx86operand);
  1310. var
  1311. l : aint;
  1312. tempstr : string;
  1313. begin
  1314. if not (oper.opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1315. Message(asmr_e_invalid_operand_type);
  1316. BuildConstSymbolExpression(true,false,l,tempstr);
  1317. if tempstr<>'' then
  1318. begin
  1319. oper.opr.typ:=OPR_SYMBOL;
  1320. oper.opr.symofs:=l;
  1321. { the symbol already exists, but we don't know whether it is data or
  1322. a function. we can use AT_NONE }
  1323. oper.opr.symbol:=objectlibrary.newasmsymbol(tempstr,AB_EXTERNAL,AT_NONE);
  1324. end
  1325. else
  1326. begin
  1327. if oper.opr.typ=OPR_NONE then
  1328. begin
  1329. oper.opr.typ:=OPR_CONSTANT;
  1330. oper.opr.val:=l;
  1331. end
  1332. else
  1333. inc(oper.opr.val,l);
  1334. end;
  1335. end;
  1336. Procedure ti386intreader.BuildOperand(oper: tx86operand);
  1337. procedure AddLabelOperand(hl:tasmlabel);
  1338. begin
  1339. if is_calljmp(actopcode) then
  1340. begin
  1341. oper.opr.typ:=OPR_SYMBOL;
  1342. oper.opr.symbol:=hl;
  1343. end
  1344. else
  1345. begin
  1346. oper.InitRef;
  1347. oper.opr.ref.symbol:=hl;
  1348. end;
  1349. end;
  1350. var
  1351. expr : string;
  1352. tempreg : tregister;
  1353. typesize,
  1354. l : aint;
  1355. hl : tasmlabel;
  1356. toffset,
  1357. tsize : aint;
  1358. Begin
  1359. expr:='';
  1360. repeat
  1361. if actasmtoken=AS_DOT then
  1362. begin
  1363. if expr<>'' then
  1364. begin
  1365. BuildRecordOffsetSize(expr,toffset,tsize);
  1366. oper.SetSize(tsize,true);
  1367. case oper.opr.typ of
  1368. OPR_LOCAL :
  1369. begin
  1370. { don't allow direct access to fields of parameters, becuase that
  1371. will generate buggy code. Allow it only for explicit typecasting
  1372. and when the parameter is in a register (delphi compatible) }
  1373. if (not oper.hastype) and
  1374. (tvarsym(oper.opr.localsym).owner.symtabletype=parasymtable) and
  1375. (current_procinfo.procdef.proccalloption<>pocall_register) then
  1376. Message(asmr_e_cannot_access_field_directly_for_parameters);
  1377. inc(oper.opr.localsymofs,toffset)
  1378. end;
  1379. OPR_CONSTANT :
  1380. inc(oper.opr.val,toffset);
  1381. OPR_REFERENCE :
  1382. inc(oper.opr.ref.offset,toffset);
  1383. OPR_NONE :
  1384. begin
  1385. oper.opr.typ:=OPR_CONSTANT;
  1386. oper.opr.val:=toffset;
  1387. end;
  1388. else
  1389. internalerror(200309222);
  1390. end;
  1391. expr:='';
  1392. end
  1393. else
  1394. begin
  1395. { See it as a separator }
  1396. Consume(AS_DOT);
  1397. end;
  1398. end;
  1399. case actasmtoken of
  1400. AS_OFFSET,
  1401. AS_TYPE,
  1402. AS_NOT,
  1403. AS_STRING :
  1404. Begin
  1405. BuildConstantOperand(oper);
  1406. end;
  1407. AS_PLUS,
  1408. AS_MINUS,
  1409. AS_LPAREN,
  1410. AS_INTNUM :
  1411. begin
  1412. case oper.opr.typ of
  1413. OPR_REFERENCE :
  1414. inc(oper.opr.ref.offset,BuildRefConstExpression);
  1415. OPR_LOCAL :
  1416. inc(oper.opr.localsymofs,BuildConstExpression);
  1417. OPR_NONE,
  1418. OPR_CONSTANT :
  1419. BuildConstantOperand(oper);
  1420. else
  1421. Message(asmr_e_invalid_operand_type);
  1422. end;
  1423. end;
  1424. AS_ID : { A constant expression, or a Variable ref. }
  1425. Begin
  1426. { Label or Special symbol reference? }
  1427. if actasmpattern[1] = '@' then
  1428. Begin
  1429. if actasmpattern = '@RESULT' then
  1430. Begin
  1431. oper.SetupResult;
  1432. Consume(AS_ID);
  1433. end
  1434. else
  1435. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1436. begin
  1437. Message(asmr_w_CODE_and_DATA_not_supported);
  1438. Consume(AS_ID);
  1439. end
  1440. else
  1441. { Local Label }
  1442. begin
  1443. CreateLocalLabel(actasmpattern,hl,false);
  1444. Consume(AS_ID);
  1445. AddLabelOperand(hl);
  1446. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1447. Message(asmr_e_syntax_error);
  1448. end;
  1449. end
  1450. else
  1451. { support result for delphi modes }
  1452. if (m_objpas in aktmodeswitches) and (actasmpattern='RESULT') then
  1453. begin
  1454. oper.SetUpResult;
  1455. Consume(AS_ID);
  1456. end
  1457. { probably a variable or normal expression }
  1458. { or a procedure (such as in CALL ID) }
  1459. else
  1460. Begin
  1461. { is it a constant ? }
  1462. if SearchIConstant(actasmpattern,l) then
  1463. Begin
  1464. case oper.opr.typ of
  1465. OPR_REFERENCE :
  1466. inc(oper.opr.ref.offset,BuildRefConstExpression);
  1467. OPR_LOCAL :
  1468. inc(oper.opr.localsymofs,BuildRefConstExpression);
  1469. OPR_NONE,
  1470. OPR_CONSTANT :
  1471. BuildConstantOperand(oper);
  1472. else
  1473. Message(asmr_e_invalid_operand_type);
  1474. end;
  1475. end
  1476. else
  1477. { Check for pascal label }
  1478. if SearchLabel(actasmpattern,hl,false) then
  1479. begin
  1480. Consume(AS_ID);
  1481. AddLabelOperand(hl);
  1482. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1483. Message(asmr_e_syntax_error);
  1484. end
  1485. else
  1486. { is it a normal variable ? }
  1487. Begin
  1488. expr:=actasmpattern;
  1489. Consume(AS_ID);
  1490. { typecasting? }
  1491. if SearchType(expr,typesize) then
  1492. begin
  1493. oper.hastype:=true;
  1494. if (actasmtoken=AS_LPAREN) then
  1495. begin
  1496. Consume(AS_LPAREN);
  1497. BuildOperand(oper);
  1498. Consume(AS_RPAREN);
  1499. if oper.opr.typ in [OPR_REFERENCE,OPR_LOCAL] then
  1500. oper.SetSize(typesize,true);
  1501. end;
  1502. end
  1503. else
  1504. begin
  1505. if not oper.SetupVar(expr,false) then
  1506. Begin
  1507. { not a variable, check special variables.. }
  1508. if expr = 'SELF' then
  1509. oper.SetupSelf
  1510. else
  1511. Message1(sym_e_unknown_id,expr);
  1512. expr:='';
  1513. end;
  1514. end;
  1515. end;
  1516. end;
  1517. end;
  1518. AS_REGISTER : { Register, a variable reference or a constant reference }
  1519. begin
  1520. { save the type of register used. }
  1521. tempreg:=actasmregister;
  1522. Consume(AS_REGISTER);
  1523. if actasmtoken = AS_COLON then
  1524. Begin
  1525. Consume(AS_COLON);
  1526. oper.InitRef;
  1527. oper.opr.ref.segment:=tempreg;
  1528. BuildReference(oper);
  1529. end
  1530. else
  1531. { Simple register }
  1532. begin
  1533. if not (oper.opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1534. Message(asmr_e_invalid_operand_type);
  1535. oper.opr.typ:=OPR_REGISTER;
  1536. oper.opr.reg:=tempreg;
  1537. oper.SetSize(tcgsize2size[reg_cgsize(oper.opr.reg)],true);
  1538. end;
  1539. end;
  1540. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1541. Begin
  1542. BuildReference(oper);
  1543. end;
  1544. AS_SEG :
  1545. Begin
  1546. Message(asmr_e_seg_not_supported);
  1547. Consume(actasmtoken);
  1548. end;
  1549. AS_SEPARATOR,
  1550. AS_END,
  1551. AS_COMMA:
  1552. break;
  1553. else
  1554. Message(asmr_e_syn_operand);
  1555. end;
  1556. until not(actasmtoken in [AS_DOT,AS_PLUS,AS_LBRACKET]);
  1557. if not((actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) or
  1558. (oper.hastype and (actasmtoken=AS_RPAREN))) then
  1559. begin
  1560. Message(asmr_e_syntax_error);
  1561. RecoverConsume(true);
  1562. end;
  1563. end;
  1564. Procedure ti386intreader.BuildOpCode(instr : tx86instruction);
  1565. var
  1566. PrefixOp,OverrideOp: tasmop;
  1567. size,
  1568. operandnum : longint;
  1569. Begin
  1570. PrefixOp:=A_None;
  1571. OverrideOp:=A_None;
  1572. { prefix seg opcode / prefix opcode }
  1573. repeat
  1574. if is_prefix(actopcode) then
  1575. begin
  1576. with instr do
  1577. begin
  1578. OpOrder:=op_intel;
  1579. PrefixOp:=ActOpcode;
  1580. opcode:=ActOpcode;
  1581. condition:=ActCondition;
  1582. opsize:=ActOpsize;
  1583. ConcatInstruction(curlist);
  1584. end;
  1585. Consume(AS_OPCODE);
  1586. end
  1587. else
  1588. if is_override(actopcode) then
  1589. begin
  1590. with instr do
  1591. begin
  1592. OpOrder:=op_intel;
  1593. OverrideOp:=ActOpcode;
  1594. opcode:=ActOpcode;
  1595. condition:=ActCondition;
  1596. opsize:=ActOpsize;
  1597. ConcatInstruction(curlist);
  1598. end;
  1599. Consume(AS_OPCODE);
  1600. end
  1601. else
  1602. break;
  1603. { allow for newline after prefix or override }
  1604. while actasmtoken=AS_SEPARATOR do
  1605. Consume(AS_SEPARATOR);
  1606. until (actasmtoken<>AS_OPCODE);
  1607. { opcode }
  1608. if (actasmtoken <> AS_OPCODE) then
  1609. Begin
  1610. Message(asmr_e_invalid_or_missing_opcode);
  1611. RecoverConsume(false);
  1612. exit;
  1613. end;
  1614. { Fill the instr object with the current state }
  1615. with instr do
  1616. begin
  1617. OpOrder:=op_intel;
  1618. Opcode:=ActOpcode;
  1619. condition:=ActCondition;
  1620. opsize:=ActOpsize;
  1621. { Valid combination of prefix/override and instruction ? }
  1622. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1623. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1624. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1625. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1626. end;
  1627. { We are reading operands, so opcode will be an AS_ID }
  1628. operandnum:=1;
  1629. Consume(AS_OPCODE);
  1630. { Zero operand opcode ? }
  1631. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1632. begin
  1633. operandnum:=0;
  1634. exit;
  1635. end;
  1636. { Read Operands }
  1637. repeat
  1638. case actasmtoken of
  1639. { End of asm operands for this opcode }
  1640. AS_END,
  1641. AS_SEPARATOR :
  1642. break;
  1643. { Operand delimiter }
  1644. AS_COMMA :
  1645. Begin
  1646. if operandnum > Max_Operands then
  1647. Message(asmr_e_too_many_operands)
  1648. else
  1649. Inc(operandnum);
  1650. Consume(AS_COMMA);
  1651. end;
  1652. { Typecast, Constant Expression, Type Specifier }
  1653. AS_DWORD,
  1654. AS_BYTE,
  1655. AS_WORD,
  1656. AS_TBYTE,
  1657. AS_DQWORD,
  1658. AS_QWORD :
  1659. Begin
  1660. { load the size in a temp variable, so it can be set when the
  1661. operand is read }
  1662. size:=0;
  1663. Case actasmtoken of
  1664. AS_DWORD : size:=4;
  1665. AS_WORD : size:=2;
  1666. AS_BYTE : size:=1;
  1667. AS_QWORD : size:=8;
  1668. AS_DQWORD : size:=16;
  1669. AS_TBYTE : size:=extended_size;
  1670. end;
  1671. Consume(actasmtoken);
  1672. case actasmtoken of
  1673. AS_LPAREN :
  1674. begin
  1675. instr.Operands[operandnum].hastype:=true;
  1676. Consume(AS_LPAREN);
  1677. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1678. Consume(AS_RPAREN);
  1679. end;
  1680. AS_PTR :
  1681. begin
  1682. Consume(AS_PTR);
  1683. instr.Operands[operandnum].InitRef;
  1684. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1685. end;
  1686. else
  1687. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1688. end;
  1689. { now set the size which was specified by the override }
  1690. instr.Operands[operandnum].setsize(size,true);
  1691. end;
  1692. { Type specifier }
  1693. AS_NEAR,
  1694. AS_FAR :
  1695. Begin
  1696. if actasmtoken = AS_NEAR then
  1697. begin
  1698. Message(asmr_w_near_ignored);
  1699. instr.opsize:=S_NEAR;
  1700. end
  1701. else
  1702. begin
  1703. Message(asmr_w_far_ignored);
  1704. instr.opsize:=S_FAR;
  1705. end;
  1706. Consume(actasmtoken);
  1707. if actasmtoken=AS_PTR then
  1708. begin
  1709. Consume(AS_PTR);
  1710. instr.Operands[operandnum].InitRef;
  1711. end;
  1712. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1713. end;
  1714. else
  1715. BuildOperand(instr.Operands[operandnum] as tx86operand);
  1716. end; { end case }
  1717. until false;
  1718. instr.Ops:=operandnum;
  1719. end;
  1720. Procedure ti386intreader.BuildConstant(constsize: longint);
  1721. var
  1722. asmsym,
  1723. expr: string;
  1724. value : aint;
  1725. Begin
  1726. Repeat
  1727. Case actasmtoken of
  1728. AS_STRING:
  1729. Begin
  1730. { DD and DW cases }
  1731. if constsize <> 1 then
  1732. Begin
  1733. if Not PadZero(actasmpattern,constsize) then
  1734. Message(scan_f_string_exceeds_line);
  1735. end;
  1736. expr:=actasmpattern;
  1737. Consume(AS_STRING);
  1738. Case actasmtoken of
  1739. AS_COMMA:
  1740. Consume(AS_COMMA);
  1741. AS_END,
  1742. AS_SEPARATOR: ;
  1743. else
  1744. Message(asmr_e_invalid_string_expression);
  1745. end;
  1746. ConcatString(curlist,expr);
  1747. end;
  1748. AS_PLUS,
  1749. AS_MINUS,
  1750. AS_LPAREN,
  1751. AS_NOT,
  1752. AS_INTNUM,
  1753. AS_ID :
  1754. Begin
  1755. BuildConstSymbolExpression(false,false,value,asmsym);
  1756. if asmsym<>'' then
  1757. begin
  1758. if constsize<>sizeof(aint) then
  1759. Message1(asmr_w_const32bit_for_address,asmsym);
  1760. ConcatConstSymbol(curlist,asmsym,value)
  1761. end
  1762. else
  1763. ConcatConstant(curlist,value,constsize);
  1764. end;
  1765. AS_COMMA:
  1766. Consume(AS_COMMA);
  1767. AS_END,
  1768. AS_SEPARATOR:
  1769. break;
  1770. else
  1771. begin
  1772. Message(asmr_e_syn_constant);
  1773. RecoverConsume(false);
  1774. end
  1775. end;
  1776. Until false;
  1777. end;
  1778. function ti386intreader.Assemble: tlinkedlist;
  1779. Var
  1780. hl : tasmlabel;
  1781. instr : Tx86Instruction;
  1782. Begin
  1783. Message1(asmr_d_start_reading,'intel');
  1784. inexpression:=FALSE;
  1785. firsttoken:=TRUE;
  1786. { sets up all opcode and register tables in uppercase
  1787. done in the construtor now
  1788. if not _asmsorted then
  1789. Begin
  1790. SetupTables;
  1791. _asmsorted:=TRUE;
  1792. end;
  1793. }
  1794. curlist:=TAAsmoutput.Create;
  1795. { setup label linked list }
  1796. LocalLabelList:=TLocalLabelList.Create;
  1797. { start tokenizer }
  1798. c:=current_scanner.asmgetcharstart;
  1799. gettoken;
  1800. { main loop }
  1801. repeat
  1802. case actasmtoken of
  1803. AS_LLABEL:
  1804. Begin
  1805. if CreateLocalLabel(actasmpattern,hl,true) then
  1806. ConcatLabel(curlist,hl);
  1807. Consume(AS_LLABEL);
  1808. end;
  1809. AS_LABEL:
  1810. Begin
  1811. if SearchLabel(upper(actasmpattern),hl,true) then
  1812. ConcatLabel(curlist,hl)
  1813. else
  1814. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1815. Consume(AS_LABEL);
  1816. end;
  1817. AS_DW :
  1818. Begin
  1819. inexpression:=true;
  1820. Consume(AS_DW);
  1821. BuildConstant(2);
  1822. inexpression:=false;
  1823. end;
  1824. AS_DB :
  1825. Begin
  1826. inexpression:=true;
  1827. Consume(AS_DB);
  1828. BuildConstant(1);
  1829. inexpression:=false;
  1830. end;
  1831. AS_DD :
  1832. Begin
  1833. inexpression:=true;
  1834. Consume(AS_DD);
  1835. BuildConstant(4);
  1836. inexpression:=false;
  1837. end;
  1838. AS_ALIGN:
  1839. Begin
  1840. Consume(AS_ALIGN);
  1841. ConcatAlign(curlist,BuildConstExpression);
  1842. if actasmtoken<>AS_SEPARATOR then
  1843. Consume(AS_SEPARATOR);
  1844. end;
  1845. AS_OPCODE :
  1846. Begin
  1847. instr:=Tx86Instruction.Create(Tx86Operand);
  1848. BuildOpcode(instr);
  1849. with instr do
  1850. begin
  1851. { We need AT&T style operands }
  1852. Swapoperands;
  1853. { Must be done with args in ATT order }
  1854. CheckNonCommutativeOpcodes;
  1855. AddReferenceSizes;
  1856. SetInstructionOpsize;
  1857. CheckOperandSizes;
  1858. ConcatInstruction(curlist);
  1859. end;
  1860. instr.Free;
  1861. end;
  1862. AS_SEPARATOR :
  1863. Begin
  1864. Consume(AS_SEPARATOR);
  1865. end;
  1866. AS_END :
  1867. break; { end assembly block }
  1868. else
  1869. Begin
  1870. Message(asmr_e_syntax_error);
  1871. RecoverConsume(false);
  1872. end;
  1873. end; { end case }
  1874. until false;
  1875. { Check LocalLabelList }
  1876. LocalLabelList.CheckEmitted;
  1877. LocalLabelList.Free;
  1878. { Return the list in an asmnode }
  1879. assemble:=curlist;
  1880. Message1(asmr_d_finish_reading,'intel');
  1881. end;
  1882. {*****************************************************************************
  1883. Initialize
  1884. *****************************************************************************}
  1885. const
  1886. asmmode_i386_intel_info : tasmmodeinfo =
  1887. (
  1888. id : asmmode_i386_intel;
  1889. idtxt : 'INTEL';
  1890. casmreader : ti386intreader;
  1891. );
  1892. begin
  1893. RegisterAsmMode(asmmode_i386_intel_info);
  1894. end.
  1895. {
  1896. $Log$
  1897. Revision 1.77 2004-09-13 20:25:52 peter
  1898. * support byte() typecast
  1899. * support array index
  1900. Revision 1.76 2004/07/06 19:47:19 peter
  1901. * fixed parsing of strings in db
  1902. Revision 1.75 2004/06/23 14:54:46 peter
  1903. * align directive added
  1904. Revision 1.74 2004/06/20 08:55:31 florian
  1905. * logs truncated
  1906. Revision 1.73 2004/06/16 20:07:10 florian
  1907. * dwarf branch merged
  1908. Revision 1.72 2004/05/20 21:54:33 florian
  1909. + <pointer> - <pointer> result is divided by the pointer element size now
  1910. this is delphi compatible as well as resulting in the expected result for p1+(p2-p1)
  1911. Revision 1.71.2.3 2004/05/02 00:31:33 peter
  1912. * fixedi i386 compile
  1913. Revision 1.71.2.2 2004/05/01 23:36:47 peter
  1914. * assembler reader 64bit fixes
  1915. Revision 1.71.2.1 2004/05/01 16:02:10 peter
  1916. * POINTER_SIZE replaced with sizeof(aint)
  1917. * aint,aword,tconst*int moved to globtype
  1918. }