ra386int.pas 51 KB

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