ra386int.pas 50 KB

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