ra386int.pas 52 KB

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