ra386int.pas 45 KB

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