ra386int.pas 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  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_ID:
  715. Begin
  716. tempstr:=actasmpattern;
  717. prevtok:=prevasmtoken;
  718. consume(AS_ID);
  719. if actasmtoken=AS_DOT then
  720. begin
  721. BuildRecordOffsetSize(tempstr,l,k);
  722. str(l, tempstr);
  723. expr:=expr + tempstr;
  724. end
  725. else
  726. if SearchIConstant(tempstr,l) then
  727. begin
  728. str(l, tempstr);
  729. expr:=expr + tempstr;
  730. end
  731. else
  732. begin
  733. hs:='';
  734. if is_locallabel(tempstr) then
  735. begin
  736. CreateLocalLabel(tempstr,hl,false);
  737. hs:=hl^.name
  738. end
  739. else
  740. if SearchLabel(tempstr,hl,false) then
  741. hs:=hl^.name
  742. else
  743. begin
  744. getsym(tempstr,false);
  745. sym:=srsym;
  746. if assigned(sym) then
  747. begin
  748. if sym^.owner^.symtabletype in [localsymtable,parasymtable] then
  749. Message(asmr_e_no_local_or_para_allowed);
  750. case srsym^.typ of
  751. varsym :
  752. hs:=pvarsym(srsym)^.mangledname;
  753. typedconstsym :
  754. hs:=ptypedconstsym(srsym)^.mangledname;
  755. procsym :
  756. hs:=pprocsym(srsym)^.mangledname;
  757. else
  758. Message(asmr_e_wrong_sym_type);
  759. end;
  760. end
  761. else
  762. Message1(sym_e_unknown_id,tempstr);
  763. end;
  764. { symbol found? }
  765. if hs<>'' then
  766. begin
  767. if needofs and (prevtok<>AS_OFFSET) then
  768. Message(asmr_e_need_offset);
  769. if asmsym='' then
  770. asmsym:=hs
  771. else
  772. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  773. if (expr='') or (expr[length(expr)]='+') then
  774. begin
  775. delete(expr,length(expr),1);
  776. if not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END]) then
  777. Message(asmr_e_only_add_relocatable_symbol);
  778. end
  779. else
  780. Message(asmr_e_only_add_relocatable_symbol);
  781. end;
  782. end;
  783. end;
  784. AS_END,
  785. AS_RBRACKET,
  786. AS_SEPARATOR,
  787. AS_COMMA:
  788. Begin
  789. break;
  790. end;
  791. else
  792. Begin
  793. { write error only once. }
  794. if not errorflag then
  795. Message(asmr_e_invalid_constant_expression);
  796. { consume tokens until we find COMMA or SEPARATOR }
  797. Consume(actasmtoken);
  798. errorflag:=TRUE;
  799. end;
  800. end;
  801. Until false;
  802. { calculate expression }
  803. if not ErrorFlag then
  804. value:=CalculateExpression(expr)
  805. else
  806. value:=0;
  807. { no longer in an expression }
  808. inexpression:=FALSE;
  809. end;
  810. Function BuildConstExpression:longint;
  811. var
  812. l : longint;
  813. hs : string;
  814. begin
  815. BuildConstSymbolExpression(false,false,l,hs);
  816. if hs<>'' then
  817. Message(asmr_e_relocatable_symbol_not_allowed);
  818. BuildConstExpression:=l;
  819. end;
  820. Function BuildRefConstExpression:longint;
  821. var
  822. l : longint;
  823. hs : string;
  824. begin
  825. BuildConstSymbolExpression(false,true,l,hs);
  826. if hs<>'' then
  827. Message(asmr_e_relocatable_symbol_not_allowed);
  828. BuildRefConstExpression:=l;
  829. end;
  830. {****************************************************************************
  831. T386IntelOperand
  832. ****************************************************************************}
  833. type
  834. P386IntelOperand=^T386IntelOperand;
  835. T386IntelOperand=object(T386Operand)
  836. Procedure BuildOperand;virtual;
  837. private
  838. Procedure BuildReference;
  839. Procedure BuildConstant;
  840. end;
  841. Procedure T386IntelOperand.BuildReference;
  842. var
  843. l : longint;
  844. hs : string;
  845. code : integer;
  846. hreg,
  847. oldbase : tregister;
  848. GotStar,
  849. GotPlus,Negative : boolean;
  850. Begin
  851. Consume(AS_LBRACKET);
  852. InitRef;
  853. GotStar:=false;
  854. GotPlus:=true;
  855. Negative:=false;
  856. repeat
  857. Case actasmtoken of
  858. AS_ID: { Constant reference expression OR variable reference expression }
  859. Begin
  860. if not GotPlus then
  861. Message(asmr_e_invalid_reference_syntax);
  862. if actasmpattern[1] = '@' then
  863. Message(asmr_e_local_symbol_not_allowed_as_ref);
  864. GotStar:=false;
  865. GotPlus:=false;
  866. if SearchIConstant(actasmpattern,l) then
  867. begin
  868. l:=BuildRefConstExpression;
  869. GotPlus:=(prevasmtoken=AS_PLUS);
  870. GotStar:=(prevasmtoken=AS_STAR);
  871. if GotStar then
  872. opr.ref.scalefactor:=l
  873. else
  874. begin
  875. if negative then
  876. Dec(opr.ref.offset,l)
  877. else
  878. Inc(opr.ref.offset,l);
  879. end;
  880. end
  881. else
  882. Begin
  883. if hasvar then
  884. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  885. if negative then
  886. Message(asmr_e_only_add_relocatable_symbol);
  887. oldbase:=opr.ref.base;
  888. opr.ref.base:=R_NO;
  889. if not SetupVar(actasmpattern) then
  890. Message1(sym_e_unknown_id,actasmpattern);
  891. { is the base register loaded by the var ? }
  892. if (opr.ref.base<>R_NO) then
  893. begin
  894. { check if we can move the old base to the index register }
  895. if (opr.ref.index<>R_NO) then
  896. Message(asmr_e_wrong_base_index)
  897. else
  898. opr.ref.index:=oldbase;
  899. end
  900. else
  901. opr.ref.base:=oldbase;
  902. { we can't have a Constant here so add the constant value to the
  903. offset }
  904. if opr.typ=OPR_CONSTANT then
  905. begin
  906. opr.typ:=OPR_REFERENCE;
  907. inc(opr.ref.offset,opr.val);
  908. end;
  909. Consume(AS_ID);
  910. end;
  911. end;
  912. AS_PLUS :
  913. Begin
  914. Consume(AS_PLUS);
  915. Negative:=false;
  916. GotPlus:=true;
  917. GotStar:=false;
  918. end;
  919. AS_MINUS :
  920. begin
  921. Consume(AS_MINUS);
  922. Negative:=true;
  923. GotPlus:=true;
  924. GotStar:=false;
  925. end;
  926. AS_STAR : { Scaling, with eax*4 order }
  927. begin
  928. Consume(AS_STAR);
  929. hs:='';
  930. l:=0;
  931. case actasmtoken of
  932. AS_LPAREN :
  933. l:=BuildConstExpression;
  934. AS_INTNUM:
  935. Begin
  936. hs:=actasmpattern;
  937. Consume(AS_INTNUM);
  938. end;
  939. AS_REGISTER :
  940. begin
  941. if opr.ref.scalefactor=0 then
  942. Message(asmr_e_wrong_scale_factor);
  943. end;
  944. else
  945. Message(asmr_e_invalid_reference_syntax);
  946. end;
  947. if actasmtoken<>AS_REGISTER then
  948. begin
  949. if hs<>'' then
  950. val(hs,l,code);
  951. opr.ref.scalefactor:=l
  952. end;
  953. GotPlus:=false;
  954. GotStar:=false;
  955. end;
  956. AS_REGISTER :
  957. begin
  958. if (not GotPlus) and (not GotStar) then
  959. Message(asmr_e_invalid_reference_syntax);
  960. hreg:=actasmregister;
  961. Consume(AS_REGISTER);
  962. { this register will be the index:
  963. 1. just read a *
  964. 2. next token is a *
  965. 3. base register is already used }
  966. if (GotStar) or
  967. (actasmtoken=AS_STAR) or
  968. (opr.ref.base<>R_NO) then
  969. begin
  970. if (opr.ref.index<>R_NO) then
  971. Message(asmr_e_multiple_index);
  972. opr.ref.index:=hreg;
  973. end
  974. else
  975. opr.ref.base:=hreg;
  976. GotPlus:=false;
  977. GotStar:=false;
  978. end;
  979. AS_NOT,
  980. AS_INTNUM,
  981. AS_LPAREN : { Constant reference expression }
  982. begin
  983. if not GotPlus then
  984. Message(asmr_e_invalid_reference_syntax);
  985. l:=BuildRefConstExpression;
  986. GotPlus:=(prevasmtoken=AS_PLUS);
  987. GotStar:=(prevasmtoken=AS_STAR);
  988. if GotStar then
  989. opr.ref.scalefactor:=l
  990. else
  991. begin
  992. if negative then
  993. Dec(opr.ref.offset,l)
  994. else
  995. Inc(opr.ref.offset,l);
  996. end;
  997. end;
  998. AS_RBRACKET :
  999. begin
  1000. if GotPlus then
  1001. Message(asmr_e_invalid_reference_syntax);
  1002. Consume(AS_RBRACKET);
  1003. break;
  1004. end;
  1005. else
  1006. Begin
  1007. Message(asmr_e_invalid_reference_syntax);
  1008. RecoverConsume(true);
  1009. break;
  1010. end;
  1011. end;
  1012. until false;
  1013. end;
  1014. Procedure T386IntelOperand.BuildConstant;
  1015. var
  1016. l : longint;
  1017. tempstr : string;
  1018. begin
  1019. BuildConstSymbolExpression(true,false,l,tempstr);
  1020. if tempstr<>'' then
  1021. begin
  1022. opr.typ:=OPR_SYMBOL;
  1023. opr.symofs:=l;
  1024. opr.symbol:=newasmsymbol(tempstr);
  1025. end
  1026. else
  1027. begin
  1028. opr.typ:=OPR_CONSTANT;
  1029. opr.val:=l;
  1030. end;
  1031. end;
  1032. Procedure T386IntelOperand.BuildOperand;
  1033. procedure AddLabelOperand(hl:pasmlabel);
  1034. begin
  1035. if is_calljmp(actopcode) then
  1036. begin
  1037. opr.typ:=OPR_SYMBOL;
  1038. opr.symbol:=hl;
  1039. end
  1040. else
  1041. begin
  1042. InitRef;
  1043. opr.ref.symbol:=hl;
  1044. end;
  1045. end;
  1046. var
  1047. expr,
  1048. tempstr : string;
  1049. tempreg : tregister;
  1050. l,
  1051. toffset,
  1052. tsize : longint;
  1053. hl : PAsmLabel;
  1054. Begin
  1055. tempstr:='';
  1056. expr:='';
  1057. case actasmtoken of
  1058. AS_OFFSET,
  1059. AS_INTNUM,
  1060. AS_PLUS,
  1061. AS_MINUS,
  1062. AS_NOT,
  1063. AS_LPAREN :
  1064. Begin
  1065. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1066. Message(asmr_e_invalid_operand_type);
  1067. BuildConstant;
  1068. end;
  1069. AS_STRING :
  1070. Begin
  1071. if not (opr.typ in [OPR_NONE]) then
  1072. Message(asmr_e_invalid_operand_type);
  1073. if not PadZero(actasmpattern,4) then
  1074. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  1075. opr.typ:=OPR_CONSTANT;
  1076. opr.val:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1077. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1078. Consume(AS_STRING);
  1079. end;
  1080. AS_ID : { A constant expression, or a Variable ref. }
  1081. Begin
  1082. { Label or Special symbol reference? }
  1083. if actasmpattern[1] = '@' then
  1084. Begin
  1085. if actasmpattern = '@RESULT' then
  1086. Begin
  1087. InitRef;
  1088. SetupResult;
  1089. end
  1090. else
  1091. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1092. Message(asmr_w_CODE_and_DATA_not_supported)
  1093. else
  1094. { Local Label }
  1095. begin
  1096. CreateLocalLabel(actasmpattern,hl,false);
  1097. Consume(AS_ID);
  1098. AddLabelOperand(hl);
  1099. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1100. Message(asmr_e_syntax_error);
  1101. end;
  1102. end
  1103. else
  1104. { support result for delphi modes }
  1105. if (m_objpas in aktmodeswitches) and (actasmpattern='RESULT') then
  1106. begin
  1107. InitRef;
  1108. SetUpResult;
  1109. Consume(AS_ID);
  1110. end
  1111. { probably a variable or normal expression }
  1112. { or a procedure (such as in CALL ID) }
  1113. else
  1114. Begin
  1115. { is it a constant ? }
  1116. if SearchIConstant(actasmpattern,l) then
  1117. Begin
  1118. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1119. Message(asmr_e_invalid_operand_type);
  1120. BuildConstant;
  1121. end
  1122. else
  1123. { Check for pascal label }
  1124. if SearchLabel(actasmpattern,hl,false) then
  1125. begin
  1126. Consume(AS_ID);
  1127. AddLabelOperand(hl);
  1128. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1129. Message(asmr_e_syntax_error);
  1130. end
  1131. else
  1132. { is it a normal variable ? }
  1133. Begin
  1134. InitRef;
  1135. if not SetupVar(actasmpattern) then
  1136. Begin
  1137. { not a variable, check special variables.. }
  1138. if actasmpattern = 'SELF' then
  1139. SetupSelf
  1140. else
  1141. Message1(sym_e_unknown_id,actasmpattern);
  1142. end;
  1143. l:=0;
  1144. expr:=actasmpattern;
  1145. Consume(AS_ID);
  1146. if actasmtoken=AS_LBRACKET then
  1147. begin
  1148. opr.typ:=OPR_REFERENCE;
  1149. reset_reference(opr.Ref);
  1150. BuildReference;
  1151. end;
  1152. if actasmtoken=AS_DOT then
  1153. begin
  1154. if expr='' then
  1155. Message(asmr_e_no_var_type_specified)
  1156. else
  1157. begin
  1158. BuildRecordOffsetSize(expr,toffset,tsize);
  1159. inc(l,toffset);
  1160. SetSize(tsize);
  1161. end;
  1162. end;
  1163. if actasmtoken in [AS_PLUS,AS_MINUS] then
  1164. inc(l,BuildConstExpression);
  1165. if opr.typ=OPR_REFERENCE then
  1166. inc(opr.ref.offset,l)
  1167. else
  1168. inc(opr.val,l);
  1169. end;
  1170. end;
  1171. end;
  1172. AS_REGISTER : { Register, a variable reference or a constant reference }
  1173. Begin
  1174. { save the type of register used. }
  1175. tempreg:=actasmregister;
  1176. Consume(AS_REGISTER);
  1177. if actasmtoken = AS_COLON then
  1178. Begin
  1179. Consume(AS_COLON);
  1180. InitRef;
  1181. opr.ref.segment:=tempreg;
  1182. BuildReference;
  1183. end
  1184. else
  1185. { Simple register }
  1186. begin
  1187. if not (opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1188. Message(asmr_e_invalid_operand_type);
  1189. opr.typ:=OPR_REGISTER;
  1190. opr.reg:=tempreg;
  1191. size:=reg_2_opsize[opr.reg];
  1192. end;
  1193. end;
  1194. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1195. Begin
  1196. BuildReference;
  1197. end;
  1198. AS_SEG :
  1199. Begin
  1200. Message(asmr_e_seg_not_supported);
  1201. Consume(actasmtoken);
  1202. end;
  1203. AS_SEPARATOR,
  1204. AS_END,
  1205. AS_COMMA: ;
  1206. else
  1207. Message(asmr_e_syn_operand);
  1208. end;
  1209. if not(actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1210. begin
  1211. Message(asmr_e_syntax_error);
  1212. RecoverConsume(true);
  1213. end;
  1214. end;
  1215. {*****************************************************************************
  1216. T386IntelInstruction
  1217. *****************************************************************************}
  1218. type
  1219. P386IntelInstruction=^T386IntelInstruction;
  1220. T386IntelInstruction=object(T386Instruction)
  1221. procedure InitOperands;virtual;
  1222. procedure BuildOpcode;virtual;
  1223. end;
  1224. procedure T386IntelInstruction.InitOperands;
  1225. var
  1226. i : longint;
  1227. begin
  1228. for i:=1to 3 do
  1229. Operands[i]:=new(P386IntelOperand,Init);
  1230. end;
  1231. Procedure T386IntelInstruction.BuildOpCode;
  1232. var
  1233. PrefixOp,OverrideOp: tasmop;
  1234. expr : string;
  1235. size : topsize;
  1236. operandnum : longint;
  1237. Begin
  1238. expr:='';
  1239. PrefixOp:=A_None;
  1240. OverrideOp:=A_None;
  1241. { prefix seg opcode / prefix opcode }
  1242. repeat
  1243. if is_prefix(actopcode) then
  1244. begin
  1245. PrefixOp:=ActOpcode;
  1246. opcode:=ActOpcode;
  1247. condition:=ActCondition;
  1248. opsize:=ActOpsize;
  1249. ConcatInstruction(curlist);
  1250. Consume(AS_OPCODE);
  1251. end
  1252. else
  1253. if is_override(actopcode) then
  1254. begin
  1255. OverrideOp:=ActOpcode;
  1256. opcode:=ActOpcode;
  1257. condition:=ActCondition;
  1258. opsize:=ActOpsize;
  1259. ConcatInstruction(curlist);
  1260. Consume(AS_OPCODE);
  1261. end
  1262. else
  1263. break;
  1264. until (actasmtoken<>AS_OPCODE);
  1265. { opcode }
  1266. if (actasmtoken <> AS_OPCODE) then
  1267. Begin
  1268. Message(asmr_e_invalid_or_missing_opcode);
  1269. RecoverConsume(false);
  1270. exit;
  1271. end;
  1272. { Fill the instr object with the current state }
  1273. Opcode:=ActOpcode;
  1274. condition:=ActCondition;
  1275. opsize:=ActOpsize;
  1276. { Valid combination of prefix/override and instruction ? }
  1277. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1278. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1279. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1280. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1281. { We are reading operands, so opcode will be an AS_ID }
  1282. operandnum:=1;
  1283. Consume(AS_OPCODE);
  1284. { Zero operand opcode ? }
  1285. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1286. begin
  1287. operandnum:=0;
  1288. exit;
  1289. end;
  1290. { Read Operands }
  1291. repeat
  1292. case actasmtoken of
  1293. { End of asm operands for this opcode }
  1294. AS_END,
  1295. AS_SEPARATOR :
  1296. break;
  1297. { Operand delimiter }
  1298. AS_COMMA :
  1299. Begin
  1300. if operandnum > MaxOperands then
  1301. Message(asmr_e_too_many_operands)
  1302. else
  1303. Inc(operandnum);
  1304. Consume(AS_COMMA);
  1305. end;
  1306. { Typecast, Constant Expression, Type Specifier }
  1307. AS_DWORD,
  1308. AS_BYTE,
  1309. AS_WORD,
  1310. AS_TBYTE,
  1311. AS_QWORD :
  1312. Begin
  1313. { load the size in a temp variable, so it can be set when the
  1314. operand is read }
  1315. Case actasmtoken of
  1316. AS_DWORD : size:=S_L;
  1317. AS_WORD : size:=S_W;
  1318. AS_BYTE : size:=S_B;
  1319. AS_QWORD : size:=S_IQ;
  1320. AS_TBYTE : size:=S_FX;
  1321. end;
  1322. Consume(actasmtoken);
  1323. if actasmtoken=AS_PTR then
  1324. begin
  1325. Consume(AS_PTR);
  1326. Operands[operandnum]^.InitRef;
  1327. end;
  1328. Operands[operandnum]^.BuildOperand;
  1329. { now set the size which was specified by the override }
  1330. Operands[operandnum]^.size:=size;
  1331. end;
  1332. { Type specifier }
  1333. AS_NEAR,
  1334. AS_FAR :
  1335. Begin
  1336. if actasmtoken = AS_NEAR then
  1337. Message(asmr_w_near_ignored)
  1338. else
  1339. Message(asmr_w_far_ignored);
  1340. Consume(actasmtoken);
  1341. if actasmtoken=AS_PTR then
  1342. begin
  1343. Consume(AS_PTR);
  1344. Operands[operandnum]^.InitRef;
  1345. end;
  1346. Operands[operandnum]^.BuildOperand;
  1347. end;
  1348. else
  1349. Operands[operandnum]^.BuildOperand;
  1350. end; { end case }
  1351. until false;
  1352. Ops:=operandnum;
  1353. end;
  1354. Procedure BuildConstant(maxvalue: longint);
  1355. var
  1356. strlength: byte;
  1357. asmsym,
  1358. expr: string;
  1359. value : longint;
  1360. Begin
  1361. strlength:=0; { assume it is a DB }
  1362. Repeat
  1363. Case actasmtoken of
  1364. AS_STRING:
  1365. Begin
  1366. if maxvalue = $ffff then
  1367. strlength:=2
  1368. else
  1369. if maxvalue = $ffffffff then
  1370. strlength:=4;
  1371. { DD and DW cases }
  1372. if strlength <> 0 then
  1373. Begin
  1374. if Not PadZero(actasmpattern,strlength) then
  1375. Message(scan_f_string_exceeds_line);
  1376. end;
  1377. expr:=actasmpattern;
  1378. Consume(AS_STRING);
  1379. Case actasmtoken of
  1380. AS_COMMA:
  1381. Consume(AS_COMMA);
  1382. AS_END,
  1383. AS_SEPARATOR: ;
  1384. else
  1385. Message(asmr_e_invalid_string_expression);
  1386. end;
  1387. ConcatString(curlist,expr);
  1388. end;
  1389. AS_PLUS,
  1390. AS_MINUS,
  1391. AS_LPAREN,
  1392. AS_NOT,
  1393. AS_INTNUM,
  1394. AS_ID :
  1395. Begin
  1396. BuildConstSymbolExpression(false,false,value,asmsym);
  1397. if asmsym<>'' then
  1398. begin
  1399. if maxvalue<>$ffffffff then
  1400. Message(asmr_w_const32bit_for_address);
  1401. ConcatConstSymbol(curlist,asmsym,value)
  1402. end
  1403. else
  1404. ConcatConstant(curlist,value,maxvalue);
  1405. end;
  1406. AS_COMMA:
  1407. Consume(AS_COMMA);
  1408. AS_END,
  1409. AS_SEPARATOR:
  1410. break;
  1411. else
  1412. begin
  1413. Message(asmr_e_syn_constant);
  1414. RecoverConsume(false);
  1415. end
  1416. end;
  1417. Until false;
  1418. end;
  1419. Function Assemble: Ptree;
  1420. Var
  1421. hl : PAsmLabel;
  1422. instr : T386IntelInstruction;
  1423. Begin
  1424. Message1(asmr_d_start_reading,'intel');
  1425. inexpression:=FALSE;
  1426. firsttoken:=TRUE;
  1427. if assigned(procinfo.retdef) and
  1428. (is_fpu(procinfo.retdef) or
  1429. ret_in_acc(procinfo.retdef)) then
  1430. procinfo.funcret_is_valid:=true;
  1431. { sets up all opcode and register tables in uppercase }
  1432. if not _asmsorted then
  1433. Begin
  1434. SetupTables;
  1435. _asmsorted:=TRUE;
  1436. end;
  1437. curlist:=new(paasmoutput,init);
  1438. { setup label linked list }
  1439. new(LocalLabelList,Init);
  1440. { start tokenizer }
  1441. c:=current_scanner^.asmgetchar;
  1442. gettoken;
  1443. { main loop }
  1444. repeat
  1445. case actasmtoken of
  1446. AS_LLABEL:
  1447. Begin
  1448. if CreateLocalLabel(actasmpattern,hl,true) then
  1449. ConcatLabel(curlist,hl);
  1450. Consume(AS_LLABEL);
  1451. end;
  1452. AS_LABEL:
  1453. Begin
  1454. if SearchLabel(upper(actasmpattern),hl,true) then
  1455. ConcatLabel(curlist,hl)
  1456. else
  1457. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1458. Consume(AS_LABEL);
  1459. end;
  1460. AS_DW :
  1461. Begin
  1462. inexpression:=true;
  1463. Consume(AS_DW);
  1464. BuildConstant($ffff);
  1465. inexpression:=false;
  1466. end;
  1467. AS_DB :
  1468. Begin
  1469. inexpression:=true;
  1470. Consume(AS_DB);
  1471. BuildConstant($ff);
  1472. inexpression:=false;
  1473. end;
  1474. AS_DD :
  1475. Begin
  1476. inexpression:=true;
  1477. Consume(AS_DD);
  1478. BuildConstant($ffffffff);
  1479. inexpression:=false;
  1480. end;
  1481. AS_OPCODE :
  1482. Begin
  1483. instr.init;
  1484. instr.BuildOpcode;
  1485. { We need AT&T style operands }
  1486. instr.SwapOperands;
  1487. instr.AddReferenceSizes;
  1488. instr.SetInstructionOpsize;
  1489. instr.CheckOperandSizes;
  1490. instr.ConcatInstruction(curlist);
  1491. instr.done;
  1492. end;
  1493. AS_SEPARATOR :
  1494. Begin
  1495. Consume(AS_SEPARATOR);
  1496. end;
  1497. AS_END :
  1498. break; { end assembly block }
  1499. else
  1500. Begin
  1501. Message(asmr_e_syntax_error);
  1502. RecoverConsume(false);
  1503. end;
  1504. end; { end case }
  1505. until false;
  1506. { Check LocalLabelList }
  1507. LocalLabelList^.CheckEmitted;
  1508. dispose(LocalLabelList,Done);
  1509. { Return the list in an asmnode }
  1510. assemble:=genasmnode(curlist);
  1511. Message1(asmr_d_finish_reading,'intel');
  1512. end;
  1513. {*****************************************************************************
  1514. Initialize
  1515. *****************************************************************************}
  1516. var
  1517. old_exit : pointer;
  1518. procedure ra386int_exit;{$ifndef FPC}far;{$endif}
  1519. begin
  1520. if assigned(iasmops) then
  1521. dispose(iasmops);
  1522. if assigned(iasmregs) then
  1523. dispose(iasmregs);
  1524. exitproc:=old_exit;
  1525. end;
  1526. begin
  1527. old_exit:=exitproc;
  1528. exitproc:=@ra386int_exit;
  1529. end.
  1530. {
  1531. $Log$
  1532. Revision 1.39 1999-06-28 16:02:32 peter
  1533. * merged
  1534. Revision 1.37.2.3 1999/06/28 15:52:36 peter
  1535. * better recovery in main loop
  1536. Revision 1.37.2.2 1999/06/22 14:20:22 peter
  1537. * fixed parsing and writing of fpureg
  1538. Revision 1.37.2.1 1999/06/21 16:43:00 peter
  1539. * fixed label and opcode on the same line
  1540. Revision 1.37 1999/06/08 11:52:00 peter
  1541. * fixed some intel bugs with scale parsing
  1542. * end is now also a separator in many more cases
  1543. Revision 1.36 1999/06/01 19:56:37 peter
  1544. * fixed llabel with delete the first @
  1545. Revision 1.35 1999/05/27 19:44:59 peter
  1546. * removed oldasm
  1547. * plabel -> pasmlabel
  1548. * -a switches to source writing automaticly
  1549. * assembler readers OOPed
  1550. * asmsymbol automaticly external
  1551. * jumptables and other label fixes for asm readers
  1552. Revision 1.34 1999/05/21 13:55:16 peter
  1553. * NEWLAB for label as symbol
  1554. Revision 1.33 1999/05/05 22:22:03 peter
  1555. * updated messages
  1556. Revision 1.32 1999/05/04 21:45:02 florian
  1557. * changes to compile it with Delphi 4.0
  1558. Revision 1.31 1999/05/01 13:48:41 peter
  1559. * merged nasm compiler
  1560. Revision 1.6 1999/04/26 23:26:18 peter
  1561. * redesigned record offset parsing to support nested records
  1562. * normal compiler uses the redesigned createvarinstr()
  1563. Revision 1.5 1999/04/20 11:01:24 peter
  1564. * better tokenpos info
  1565. Revision 1.4 1999/04/14 09:07:46 peter
  1566. * asm reader improvements
  1567. Revision 1.3 1999/03/06 17:24:27 peter
  1568. * rewritten intel parser a lot, especially reference reading
  1569. * size checking added for asm parsers
  1570. Revision 1.2 1999/03/02 02:56:31 peter
  1571. + stabs support for binary writers
  1572. * more fixes and missing updates from the previous commit :(
  1573. Revision 1.1 1999/03/01 15:46:26 peter
  1574. * ag386bin finally make cycles correct
  1575. * prefixes are now also normal opcodes
  1576. }