ra386int.pas 52 KB

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