ra386int.pas 46 KB

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