ra386int.pas 48 KB

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