ra386int.pas 52 KB

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