ra386int.pas 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043
  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. current_scanner^.illegal_char(c);
  570. end;
  571. end;
  572. end;
  573. function consume(t : tasmtoken):boolean;
  574. begin
  575. Consume:=true;
  576. if t<>actasmtoken then
  577. begin
  578. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  579. Consume:=false;
  580. end;
  581. repeat
  582. gettoken;
  583. until actasmtoken<>AS_NONE;
  584. end;
  585. procedure RecoverConsume(allowcomma:boolean);
  586. begin
  587. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  588. begin
  589. if allowcomma and (actasmtoken=AS_COMMA) then
  590. break;
  591. Consume(actasmtoken);
  592. end;
  593. end;
  594. {*****************************************************************************
  595. Parsing Helpers
  596. *****************************************************************************}
  597. Procedure BuildRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  598. { Description: This routine builds up a record offset after a AS_DOT }
  599. { token is encountered. }
  600. { On entry actasmtoken should be equal to AS_DOT }
  601. var
  602. s : string;
  603. Begin
  604. offset:=0;
  605. size:=0;
  606. s:=expr;
  607. while (actasmtoken=AS_DOT) do
  608. begin
  609. Consume(AS_DOT);
  610. if actasmtoken=AS_ID then
  611. s:=s+'.'+actasmpattern;
  612. if not Consume(AS_ID) then
  613. begin
  614. RecoverConsume(true);
  615. break;
  616. end;
  617. end;
  618. if not GetRecordOffsetSize(s,offset,size) then
  619. Message(asmr_e_building_record_offset);
  620. end;
  621. Procedure BuildConstSymbolExpression(needofs,exitreg:boolean;var value:longint;var asmsym:string);
  622. var
  623. tempstr,expr,hs : string;
  624. parenlevel,l,k : longint;
  625. errorflag : boolean;
  626. prevtok : tasmtoken;
  627. hl : PAsmLabel;
  628. sym : psym;
  629. Begin
  630. { reset }
  631. value:=0;
  632. asmsym:='';
  633. errorflag:=FALSE;
  634. tempstr:='';
  635. expr:='';
  636. inexpression:=TRUE;
  637. parenlevel:=0;
  638. Repeat
  639. Case actasmtoken of
  640. AS_LPAREN:
  641. Begin
  642. Consume(AS_LPAREN);
  643. expr:=expr + '(';
  644. inc(parenlevel);
  645. end;
  646. AS_RPAREN:
  647. Begin
  648. Consume(AS_RPAREN);
  649. expr:=expr + ')';
  650. dec(parenlevel);
  651. end;
  652. AS_SHL:
  653. Begin
  654. Consume(AS_SHL);
  655. expr:=expr + '<';
  656. end;
  657. AS_SHR:
  658. Begin
  659. Consume(AS_SHR);
  660. expr:=expr + '>';
  661. end;
  662. AS_SLASH:
  663. Begin
  664. Consume(AS_SLASH);
  665. expr:=expr + '/';
  666. end;
  667. AS_MOD:
  668. Begin
  669. Consume(AS_MOD);
  670. expr:=expr + '%';
  671. end;
  672. AS_STAR:
  673. Begin
  674. Consume(AS_STAR);
  675. if exitreg and (actasmtoken=AS_REGISTER) then
  676. break;
  677. expr:=expr + '*';
  678. end;
  679. AS_PLUS:
  680. Begin
  681. Consume(AS_PLUS);
  682. if exitreg and (actasmtoken=AS_REGISTER) then
  683. break;
  684. expr:=expr + '+';
  685. end;
  686. AS_MINUS:
  687. Begin
  688. Consume(AS_MINUS);
  689. expr:=expr + '-';
  690. end;
  691. AS_AND:
  692. Begin
  693. Consume(AS_AND);
  694. expr:=expr + '&';
  695. end;
  696. AS_NOT:
  697. Begin
  698. Consume(AS_NOT);
  699. expr:=expr + '~';
  700. end;
  701. AS_XOR:
  702. Begin
  703. Consume(AS_XOR);
  704. expr:=expr + '^';
  705. end;
  706. AS_OR:
  707. Begin
  708. Consume(AS_OR);
  709. expr:=expr + '|';
  710. end;
  711. AS_INTNUM:
  712. Begin
  713. expr:=expr + actasmpattern;
  714. Consume(AS_INTNUM);
  715. end;
  716. AS_OFFSET:
  717. begin
  718. Consume(AS_OFFSET);
  719. if actasmtoken<>AS_ID then
  720. Message(asmr_e_offset_without_identifier);
  721. end;
  722. AS_TYPE:
  723. begin
  724. l:=0;
  725. Consume(AS_TYPE);
  726. if actasmtoken<>AS_ID then
  727. Message(asmr_e_type_without_identifier)
  728. else
  729. begin
  730. tempstr:=actasmpattern;
  731. Consume(AS_ID);
  732. if actasmtoken=AS_DOT then
  733. BuildRecordOffsetSize(tempstr,k,l)
  734. else
  735. begin
  736. getsym(tempstr,false);
  737. if assigned(srsym) then
  738. begin
  739. case srsym^.typ of
  740. varsym :
  741. l:=pvarsym(srsym)^.getsize;
  742. typedconstsym :
  743. l:=ptypedconstsym(srsym)^.getsize;
  744. typesym :
  745. l:=ptypesym(srsym)^.restype.def^.size;
  746. else
  747. Message(asmr_e_wrong_sym_type);
  748. end;
  749. end
  750. else
  751. Message1(sym_e_unknown_id,tempstr);
  752. end;
  753. end;
  754. str(l, tempstr);
  755. expr:=expr + tempstr;
  756. end;
  757. AS_STRING:
  758. Begin
  759. l:=0;
  760. case Length(actasmpattern) of
  761. 1 :
  762. l:=ord(actasmpattern[1]);
  763. 2 :
  764. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  765. 3 :
  766. l:=ord(actasmpattern[3]) +
  767. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  768. 4 :
  769. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  770. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  771. else
  772. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  773. end;
  774. str(l, tempstr);
  775. expr:=expr + tempstr;
  776. Consume(AS_STRING);
  777. end;
  778. AS_ID:
  779. Begin
  780. hs:='';
  781. tempstr:=actasmpattern;
  782. prevtok:=prevasmtoken;
  783. consume(AS_ID);
  784. if SearchIConstant(tempstr,l) then
  785. begin
  786. str(l, tempstr);
  787. expr:=expr + tempstr;
  788. end
  789. else
  790. begin
  791. if is_locallabel(tempstr) then
  792. begin
  793. CreateLocalLabel(tempstr,hl,false);
  794. hs:=hl^.name
  795. end
  796. else
  797. if SearchLabel(tempstr,hl,false) then
  798. hs:=hl^.name
  799. else
  800. begin
  801. getsym(tempstr,false);
  802. sym:=srsym;
  803. if assigned(sym) then
  804. begin
  805. case srsym^.typ of
  806. varsym :
  807. begin
  808. if sym^.owner^.symtabletype in [localsymtable,parasymtable] then
  809. Message(asmr_e_no_local_or_para_allowed);
  810. hs:=pvarsym(srsym)^.mangledname;
  811. end;
  812. typedconstsym :
  813. hs:=ptypedconstsym(srsym)^.mangledname;
  814. procsym :
  815. hs:=pprocsym(srsym)^.mangledname;
  816. typesym :
  817. begin
  818. if not(ptypesym(srsym)^.restype.def^.deftype in [recorddef,objectdef]) then
  819. Message(asmr_e_wrong_sym_type);
  820. end;
  821. else
  822. Message(asmr_e_wrong_sym_type);
  823. end;
  824. end
  825. else
  826. Message1(sym_e_unknown_id,tempstr);
  827. end;
  828. { symbol found? }
  829. if hs<>'' then
  830. begin
  831. if needofs and (prevtok<>AS_OFFSET) then
  832. Message(asmr_e_need_offset);
  833. if asmsym='' then
  834. asmsym:=hs
  835. else
  836. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  837. if (expr='') or (expr[length(expr)]='+') then
  838. begin
  839. { don't remove the + if there could be a record field }
  840. if actasmtoken<>AS_DOT then
  841. delete(expr,length(expr),1);
  842. end
  843. else
  844. Message(asmr_e_only_add_relocatable_symbol);
  845. end;
  846. if actasmtoken=AS_DOT then
  847. begin
  848. BuildRecordOffsetSize(tempstr,l,k);
  849. str(l, tempstr);
  850. expr:=expr + tempstr;
  851. end
  852. else
  853. begin
  854. if (expr='') or (expr[length(expr)] in ['+','-','/','*']) then
  855. delete(expr,length(expr),1);
  856. end;
  857. end;
  858. { check if there are wrong operator used like / or mod etc. }
  859. if (hs<>'') and not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  860. Message(asmr_e_only_add_relocatable_symbol);
  861. end;
  862. AS_END,
  863. AS_RBRACKET,
  864. AS_SEPARATOR,
  865. AS_COMMA:
  866. Begin
  867. break;
  868. end;
  869. else
  870. Begin
  871. { write error only once. }
  872. if not errorflag then
  873. Message(asmr_e_invalid_constant_expression);
  874. { consume tokens until we find COMMA or SEPARATOR }
  875. Consume(actasmtoken);
  876. errorflag:=TRUE;
  877. end;
  878. end;
  879. Until false;
  880. { calculate expression }
  881. if not ErrorFlag then
  882. value:=CalculateExpression(expr)
  883. else
  884. value:=0;
  885. { no longer in an expression }
  886. inexpression:=FALSE;
  887. end;
  888. Function BuildConstExpression:longint;
  889. var
  890. l : longint;
  891. hs : string;
  892. begin
  893. BuildConstSymbolExpression(false,false,l,hs);
  894. if hs<>'' then
  895. Message(asmr_e_relocatable_symbol_not_allowed);
  896. BuildConstExpression:=l;
  897. end;
  898. Function BuildRefConstExpression:longint;
  899. var
  900. l : longint;
  901. hs : string;
  902. begin
  903. BuildConstSymbolExpression(false,true,l,hs);
  904. if hs<>'' then
  905. Message(asmr_e_relocatable_symbol_not_allowed);
  906. BuildRefConstExpression:=l;
  907. end;
  908. {****************************************************************************
  909. T386IntelOperand
  910. ****************************************************************************}
  911. type
  912. P386IntelOperand=^T386IntelOperand;
  913. T386IntelOperand=object(T386Operand)
  914. Procedure BuildOperand;virtual;
  915. private
  916. Procedure BuildReference;
  917. Procedure BuildConstant;
  918. end;
  919. Procedure T386IntelOperand.BuildReference;
  920. var
  921. k,l : longint;
  922. tempstr2,
  923. tempstr,hs : string;
  924. code : integer;
  925. hreg,
  926. oldbase : tregister;
  927. GotStar,GotOffset,HadVar,
  928. GotPlus,Negative : boolean;
  929. Begin
  930. Consume(AS_LBRACKET);
  931. InitRef;
  932. GotStar:=false;
  933. GotPlus:=true;
  934. GotOffset:=false;
  935. Negative:=false;
  936. repeat
  937. if GotOffset and (actasmtoken<>AS_ID) then
  938. Message(asmr_e_invalid_reference_syntax);
  939. Case actasmtoken of
  940. AS_ID: { Constant reference expression OR variable reference expression }
  941. Begin
  942. if not GotPlus then
  943. Message(asmr_e_invalid_reference_syntax);
  944. if actasmpattern[1] = '@' then
  945. Message(asmr_e_local_label_not_allowed_as_ref);
  946. GotStar:=false;
  947. GotPlus:=false;
  948. if SearchIConstant(actasmpattern,l) or
  949. SearchRecordType(actasmpattern) then
  950. begin
  951. l:=BuildRefConstExpression;
  952. GotPlus:=(prevasmtoken=AS_PLUS);
  953. GotStar:=(prevasmtoken=AS_STAR);
  954. if GotStar then
  955. opr.ref.scalefactor:=l
  956. else
  957. begin
  958. if negative then
  959. Dec(opr.ref.offset,l)
  960. else
  961. Inc(opr.ref.offset,l);
  962. end;
  963. end
  964. else
  965. Begin
  966. if hasvar and not GotOffset then
  967. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  968. HadVar:=hasvar and GotOffset;
  969. if negative then
  970. Message(asmr_e_only_add_relocatable_symbol);
  971. oldbase:=opr.ref.base;
  972. opr.ref.base:=R_NO;
  973. tempstr:=actasmpattern;
  974. Consume(AS_ID);
  975. { typecasting? }
  976. if (actasmtoken=AS_LPAREN) and
  977. SearchType(tempstr) then
  978. begin
  979. hastype:=true;
  980. Consume(AS_LPAREN);
  981. tempstr2:=actasmpattern;
  982. Consume(AS_ID);
  983. Consume(AS_RPAREN);
  984. if not SetupVar(tempstr2,GotOffset) then
  985. Message1(sym_e_unknown_id,tempstr2);
  986. end
  987. else
  988. if not SetupVar(tempstr,GotOffset) then
  989. Message1(sym_e_unknown_id,tempstr);
  990. { record.field ? }
  991. if actasmtoken=AS_DOT then
  992. begin
  993. BuildRecordOffsetSize(tempstr,l,k);
  994. inc(opr.ref.offset,l);
  995. end;
  996. if GotOffset then
  997. begin
  998. if hasvar and (opr.ref.base=procinfo^.framepointer) then
  999. begin
  1000. opr.ref.base:=R_NO;
  1001. hasvar:=hadvar;
  1002. end
  1003. else
  1004. begin
  1005. if hasvar and hadvar then
  1006. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1007. { should we allow ?? }
  1008. end;
  1009. end;
  1010. { is the base register loaded by the var ? }
  1011. if (opr.ref.base<>R_NO) then
  1012. begin
  1013. { check if we can move the old base to the index register }
  1014. if (opr.ref.index<>R_NO) then
  1015. Message(asmr_e_wrong_base_index)
  1016. else if assigned(procinfo^._class) and
  1017. (oldbase=self_pointer) and
  1018. (opr.ref.base=self_pointer) then
  1019. begin
  1020. Message(asmr_w_possible_object_field_bug);
  1021. { warn but accept... who knows what people
  1022. caninvent in assembler ! }
  1023. opr.ref.index:=oldbase;
  1024. end
  1025. else
  1026. opr.ref.index:=oldbase;
  1027. end
  1028. else
  1029. opr.ref.base:=oldbase;
  1030. { we can't have a Constant here so add the constant value to the
  1031. offset }
  1032. if opr.typ=OPR_CONSTANT then
  1033. begin
  1034. opr.typ:=OPR_REFERENCE;
  1035. inc(opr.ref.offset,opr.val);
  1036. end;
  1037. end;
  1038. GotOffset:=false;
  1039. end;
  1040. AS_PLUS :
  1041. Begin
  1042. Consume(AS_PLUS);
  1043. Negative:=false;
  1044. GotPlus:=true;
  1045. GotStar:=false;
  1046. end;
  1047. AS_MINUS :
  1048. begin
  1049. Consume(AS_MINUS);
  1050. Negative:=true;
  1051. GotPlus:=true;
  1052. GotStar:=false;
  1053. end;
  1054. AS_STAR : { Scaling, with eax*4 order }
  1055. begin
  1056. Consume(AS_STAR);
  1057. hs:='';
  1058. l:=0;
  1059. case actasmtoken of
  1060. AS_LPAREN :
  1061. l:=BuildConstExpression;
  1062. AS_INTNUM:
  1063. Begin
  1064. hs:=actasmpattern;
  1065. Consume(AS_INTNUM);
  1066. end;
  1067. AS_REGISTER :
  1068. begin
  1069. if opr.ref.scalefactor=0 then
  1070. Message(asmr_e_wrong_scale_factor);
  1071. end;
  1072. else
  1073. Message(asmr_e_invalid_reference_syntax);
  1074. end;
  1075. if actasmtoken<>AS_REGISTER then
  1076. begin
  1077. if hs<>'' then
  1078. val(hs,l,code);
  1079. opr.ref.scalefactor:=l;
  1080. if l>8 then
  1081. Message(asmr_e_wrong_scale_factor);
  1082. end;
  1083. GotPlus:=false;
  1084. GotStar:=false;
  1085. end;
  1086. AS_REGISTER :
  1087. begin
  1088. if (not GotPlus) and (not GotStar) then
  1089. Message(asmr_e_invalid_reference_syntax);
  1090. hreg:=actasmregister;
  1091. Consume(AS_REGISTER);
  1092. { this register will be the index:
  1093. 1. just read a *
  1094. 2. next token is a *
  1095. 3. base register is already used }
  1096. if (GotStar) or
  1097. (actasmtoken=AS_STAR) or
  1098. (opr.ref.base<>R_NO) then
  1099. begin
  1100. if (opr.ref.index<>R_NO) then
  1101. Message(asmr_e_multiple_index);
  1102. opr.ref.index:=hreg;
  1103. end
  1104. else
  1105. opr.ref.base:=hreg;
  1106. GotPlus:=false;
  1107. GotStar:=false;
  1108. end;
  1109. AS_OFFSET :
  1110. begin
  1111. Consume(AS_OFFSET);
  1112. GotOffset:=true;
  1113. end;
  1114. AS_TYPE,
  1115. AS_NOT,
  1116. AS_INTNUM,
  1117. AS_LPAREN : { Constant reference expression }
  1118. begin
  1119. if not GotPlus then
  1120. Message(asmr_e_invalid_reference_syntax);
  1121. BuildConstSymbolExpression(true,true,l,tempstr);
  1122. if tempstr<>'' then
  1123. begin
  1124. if GotStar then
  1125. Message(asmr_e_only_add_relocatable_symbol);
  1126. if not assigned(opr.ref.symbol) then
  1127. opr.ref.symbol:=newasmsymbol(tempstr)
  1128. else
  1129. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  1130. end;
  1131. if GotStar then
  1132. opr.ref.scalefactor:=l
  1133. else
  1134. begin
  1135. if negative then
  1136. Dec(opr.ref.offset,l)
  1137. else
  1138. Inc(opr.ref.offset,l);
  1139. end;
  1140. GotPlus:=(prevasmtoken=AS_PLUS);
  1141. GotStar:=(prevasmtoken=AS_STAR);
  1142. end;
  1143. AS_RBRACKET :
  1144. begin
  1145. if GotPlus then
  1146. Message(asmr_e_invalid_reference_syntax);
  1147. Consume(AS_RBRACKET);
  1148. break;
  1149. end;
  1150. else
  1151. Begin
  1152. Message(asmr_e_invalid_reference_syntax);
  1153. RecoverConsume(true);
  1154. break;
  1155. end;
  1156. end;
  1157. until false;
  1158. end;
  1159. Procedure T386IntelOperand.BuildConstant;
  1160. var
  1161. l : longint;
  1162. tempstr : string;
  1163. begin
  1164. BuildConstSymbolExpression(true,false,l,tempstr);
  1165. if tempstr<>'' then
  1166. begin
  1167. opr.typ:=OPR_SYMBOL;
  1168. opr.symofs:=l;
  1169. opr.symbol:=newasmsymbol(tempstr);
  1170. end
  1171. else
  1172. begin
  1173. opr.typ:=OPR_CONSTANT;
  1174. opr.val:=l;
  1175. end;
  1176. end;
  1177. Procedure T386IntelOperand.BuildOperand;
  1178. var
  1179. tempstr,
  1180. expr : string;
  1181. tempreg : tregister;
  1182. l : longint;
  1183. hl : PAsmLabel;
  1184. procedure AddLabelOperand(hl:pasmlabel);
  1185. begin
  1186. if is_calljmp(actopcode) then
  1187. begin
  1188. opr.typ:=OPR_SYMBOL;
  1189. opr.symbol:=hl;
  1190. end
  1191. else
  1192. begin
  1193. InitRef;
  1194. opr.ref.symbol:=hl;
  1195. end;
  1196. end;
  1197. procedure MaybeRecordOffset;
  1198. var
  1199. l,
  1200. toffset,
  1201. tsize : longint;
  1202. begin
  1203. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  1204. exit;
  1205. l:=0;
  1206. if actasmtoken=AS_DOT then
  1207. begin
  1208. { if no type was specified before the [] then we expect the
  1209. first ID to be the type }
  1210. if expr='' then
  1211. begin
  1212. consume(AS_DOT);
  1213. if actasmtoken=AS_ID then
  1214. begin
  1215. expr:=actasmpattern;
  1216. consume(AS_ID);
  1217. { now the next one must the be the dot }
  1218. if actasmtoken<>AS_DOT then
  1219. begin
  1220. Message(asmr_e_building_record_offset);
  1221. expr:='';
  1222. end;
  1223. end
  1224. else
  1225. Message(asmr_e_no_var_type_specified)
  1226. end;
  1227. if expr<>'' then
  1228. begin
  1229. BuildRecordOffsetSize(expr,toffset,tsize);
  1230. inc(l,toffset);
  1231. SetSize(tsize,true);
  1232. end;
  1233. end;
  1234. if actasmtoken in [AS_PLUS,AS_MINUS] then
  1235. inc(l,BuildConstExpression);
  1236. if (opr.typ=OPR_REFERENCE) then
  1237. begin
  1238. { don't allow direct access to fields of parameters, becuase that
  1239. will generate buggy code. Allow it only for explicit typecasting }
  1240. if (not hastype) then
  1241. begin
  1242. case opr.ref.options of
  1243. ref_parafixup :
  1244. Message(asmr_e_cannot_access_field_directly_for_parameters);
  1245. ref_selffixup :
  1246. Message(asmr_e_cannot_access_object_field_directly);
  1247. end;
  1248. end;
  1249. inc(opr.ref.offset,l)
  1250. end
  1251. else
  1252. inc(opr.val,l);
  1253. end;
  1254. Begin
  1255. expr:='';
  1256. case actasmtoken of
  1257. AS_OFFSET,
  1258. AS_TYPE,
  1259. AS_INTNUM,
  1260. AS_PLUS,
  1261. AS_MINUS,
  1262. AS_NOT,
  1263. AS_LPAREN,
  1264. AS_STRING :
  1265. Begin
  1266. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1267. Message(asmr_e_invalid_operand_type);
  1268. BuildConstant;
  1269. end;
  1270. AS_ID : { A constant expression, or a Variable ref. }
  1271. Begin
  1272. { Label or Special symbol reference? }
  1273. if actasmpattern[1] = '@' then
  1274. Begin
  1275. if actasmpattern = '@RESULT' then
  1276. Begin
  1277. InitRef;
  1278. SetupResult;
  1279. Consume(AS_ID);
  1280. end
  1281. else
  1282. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1283. begin
  1284. Message(asmr_w_CODE_and_DATA_not_supported);
  1285. Consume(AS_ID);
  1286. end
  1287. else
  1288. { Local Label }
  1289. begin
  1290. CreateLocalLabel(actasmpattern,hl,false);
  1291. Consume(AS_ID);
  1292. AddLabelOperand(hl);
  1293. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1294. Message(asmr_e_syntax_error);
  1295. end;
  1296. end
  1297. else
  1298. { support result for delphi modes }
  1299. if (m_objpas in aktmodeswitches) and (actasmpattern='RESULT') then
  1300. begin
  1301. InitRef;
  1302. SetUpResult;
  1303. Consume(AS_ID);
  1304. end
  1305. { probably a variable or normal expression }
  1306. { or a procedure (such as in CALL ID) }
  1307. else
  1308. Begin
  1309. { is it a constant ? }
  1310. if SearchIConstant(actasmpattern,l) then
  1311. Begin
  1312. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1313. Message(asmr_e_invalid_operand_type);
  1314. BuildConstant;
  1315. end
  1316. else
  1317. { Check for pascal label }
  1318. if SearchLabel(actasmpattern,hl,false) then
  1319. begin
  1320. Consume(AS_ID);
  1321. AddLabelOperand(hl);
  1322. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1323. Message(asmr_e_syntax_error);
  1324. end
  1325. else
  1326. { is it a normal variable ? }
  1327. Begin
  1328. InitRef;
  1329. expr:=actasmpattern;
  1330. Consume(AS_ID);
  1331. { typecasting? }
  1332. if (actasmtoken=AS_LPAREN) and
  1333. SearchType(expr) then
  1334. begin
  1335. hastype:=true;
  1336. Consume(AS_LPAREN);
  1337. tempstr:=actasmpattern;
  1338. Consume(AS_ID);
  1339. Consume(AS_RPAREN);
  1340. if SetupVar(tempstr,false) then
  1341. begin
  1342. MaybeRecordOffset;
  1343. { add a constant expression? }
  1344. if (actasmtoken=AS_PLUS) then
  1345. begin
  1346. l:=BuildConstExpression;
  1347. if opr.typ=OPR_CONSTANT then
  1348. inc(opr.val,l)
  1349. else
  1350. inc(opr.ref.offset,l);
  1351. end
  1352. end
  1353. else
  1354. Message1(sym_e_unknown_id,tempstr);
  1355. end
  1356. else
  1357. begin
  1358. if SetupVar(expr,false) then
  1359. begin
  1360. MaybeRecordOffset;
  1361. { add a constant expression? }
  1362. if (actasmtoken=AS_PLUS) then
  1363. begin
  1364. l:=BuildConstExpression;
  1365. if opr.typ=OPR_CONSTANT then
  1366. inc(opr.val,l)
  1367. else
  1368. inc(opr.ref.offset,l);
  1369. end
  1370. end
  1371. else
  1372. Begin
  1373. { not a variable, check special variables.. }
  1374. if expr = 'SELF' then
  1375. SetupSelf
  1376. else
  1377. Message1(sym_e_unknown_id,expr);
  1378. end;
  1379. end;
  1380. end;
  1381. { handle references }
  1382. if actasmtoken=AS_LBRACKET then
  1383. begin
  1384. if opr.typ=OPR_CONSTANT then
  1385. begin
  1386. l:=opr.val;
  1387. opr.typ:=OPR_REFERENCE;
  1388. reset_reference(opr.Ref);
  1389. opr.Ref.Offset:=l;
  1390. end;
  1391. BuildReference;
  1392. MaybeRecordOffset;
  1393. end;
  1394. end;
  1395. end;
  1396. AS_REGISTER : { Register, a variable reference or a constant reference }
  1397. Begin
  1398. { save the type of register used. }
  1399. tempreg:=actasmregister;
  1400. Consume(AS_REGISTER);
  1401. if actasmtoken = AS_COLON then
  1402. Begin
  1403. Consume(AS_COLON);
  1404. InitRef;
  1405. opr.ref.segment:=tempreg;
  1406. BuildReference;
  1407. end
  1408. else
  1409. { Simple register }
  1410. begin
  1411. if not (opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1412. Message(asmr_e_invalid_operand_type);
  1413. opr.typ:=OPR_REGISTER;
  1414. opr.reg:=tempreg;
  1415. size:=reg_2_opsize[opr.reg];
  1416. end;
  1417. end;
  1418. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1419. Begin
  1420. InitRef;
  1421. BuildReference;
  1422. MaybeRecordOffset;
  1423. end;
  1424. AS_SEG :
  1425. Begin
  1426. Message(asmr_e_seg_not_supported);
  1427. Consume(actasmtoken);
  1428. end;
  1429. AS_SEPARATOR,
  1430. AS_END,
  1431. AS_COMMA: ;
  1432. else
  1433. Message(asmr_e_syn_operand);
  1434. end;
  1435. if not(actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1436. begin
  1437. Message(asmr_e_syntax_error);
  1438. RecoverConsume(true);
  1439. end;
  1440. end;
  1441. {*****************************************************************************
  1442. T386IntelInstruction
  1443. *****************************************************************************}
  1444. type
  1445. P386IntelInstruction=^T386IntelInstruction;
  1446. T386IntelInstruction=object(T386Instruction)
  1447. procedure InitOperands;virtual;
  1448. procedure BuildOpcode;virtual;
  1449. end;
  1450. procedure T386IntelInstruction.InitOperands;
  1451. var
  1452. i : longint;
  1453. begin
  1454. for i:=1 to 3 do
  1455. Operands[i]:=new(P386IntelOperand,Init);
  1456. end;
  1457. Procedure T386IntelInstruction.BuildOpCode;
  1458. var
  1459. PrefixOp,OverrideOp: tasmop;
  1460. size : topsize;
  1461. operandnum : longint;
  1462. Begin
  1463. PrefixOp:=A_None;
  1464. OverrideOp:=A_None;
  1465. { prefix seg opcode / prefix opcode }
  1466. repeat
  1467. if is_prefix(actopcode) then
  1468. begin
  1469. PrefixOp:=ActOpcode;
  1470. opcode:=ActOpcode;
  1471. condition:=ActCondition;
  1472. opsize:=ActOpsize;
  1473. ConcatInstruction(curlist);
  1474. Consume(AS_OPCODE);
  1475. end
  1476. else
  1477. if is_override(actopcode) then
  1478. begin
  1479. OverrideOp:=ActOpcode;
  1480. opcode:=ActOpcode;
  1481. condition:=ActCondition;
  1482. opsize:=ActOpsize;
  1483. ConcatInstruction(curlist);
  1484. Consume(AS_OPCODE);
  1485. end
  1486. else
  1487. break;
  1488. { allow for newline after prefix or override }
  1489. while actasmtoken=AS_SEPARATOR do
  1490. Consume(AS_SEPARATOR);
  1491. until (actasmtoken<>AS_OPCODE);
  1492. { opcode }
  1493. if (actasmtoken <> AS_OPCODE) then
  1494. Begin
  1495. Message(asmr_e_invalid_or_missing_opcode);
  1496. RecoverConsume(false);
  1497. exit;
  1498. end;
  1499. { Fill the instr object with the current state }
  1500. Opcode:=ActOpcode;
  1501. condition:=ActCondition;
  1502. opsize:=ActOpsize;
  1503. { Valid combination of prefix/override and instruction ? }
  1504. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1505. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1506. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1507. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1508. { We are reading operands, so opcode will be an AS_ID }
  1509. operandnum:=1;
  1510. Consume(AS_OPCODE);
  1511. { Zero operand opcode ? }
  1512. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1513. begin
  1514. operandnum:=0;
  1515. exit;
  1516. end;
  1517. { Read Operands }
  1518. repeat
  1519. case actasmtoken of
  1520. { End of asm operands for this opcode }
  1521. AS_END,
  1522. AS_SEPARATOR :
  1523. break;
  1524. { Operand delimiter }
  1525. AS_COMMA :
  1526. Begin
  1527. if operandnum > MaxOperands then
  1528. Message(asmr_e_too_many_operands)
  1529. else
  1530. Inc(operandnum);
  1531. Consume(AS_COMMA);
  1532. end;
  1533. { Typecast, Constant Expression, Type Specifier }
  1534. AS_DWORD,
  1535. AS_BYTE,
  1536. AS_WORD,
  1537. AS_TBYTE,
  1538. AS_QWORD :
  1539. Begin
  1540. { load the size in a temp variable, so it can be set when the
  1541. operand is read }
  1542. Case actasmtoken of
  1543. AS_DWORD : size:=S_L;
  1544. AS_WORD : size:=S_W;
  1545. AS_BYTE : size:=S_B;
  1546. AS_QWORD : begin
  1547. if (opcode=A_FCOM) or
  1548. (opcode=A_FCOMP) or
  1549. (opcode=A_FDIV) or
  1550. (opcode=A_FDIVR) or
  1551. (opcode=A_FMUL) or
  1552. (opcode=A_FSUB) or
  1553. (opcode=A_FSUBR) or
  1554. (opcode=A_FLD) or
  1555. (opcode=A_FST) or
  1556. (opcode=A_FSTP) or
  1557. (opcode=A_FADD) then
  1558. size:=S_FL
  1559. else
  1560. size:=S_IQ;
  1561. end;
  1562. AS_TBYTE : size:=S_FX;
  1563. end;
  1564. Consume(actasmtoken);
  1565. if actasmtoken=AS_PTR then
  1566. begin
  1567. Consume(AS_PTR);
  1568. Operands[operandnum]^.InitRef;
  1569. end;
  1570. Operands[operandnum]^.BuildOperand;
  1571. { now set the size which was specified by the override }
  1572. Operands[operandnum]^.size:=size;
  1573. end;
  1574. { Type specifier }
  1575. AS_NEAR,
  1576. AS_FAR :
  1577. Begin
  1578. if actasmtoken = AS_NEAR then
  1579. Message(asmr_w_near_ignored)
  1580. else
  1581. Message(asmr_w_far_ignored);
  1582. Consume(actasmtoken);
  1583. if actasmtoken=AS_PTR then
  1584. begin
  1585. Consume(AS_PTR);
  1586. Operands[operandnum]^.InitRef;
  1587. end;
  1588. Operands[operandnum]^.BuildOperand;
  1589. end;
  1590. else
  1591. Operands[operandnum]^.BuildOperand;
  1592. end; { end case }
  1593. until false;
  1594. Ops:=operandnum;
  1595. end;
  1596. Procedure BuildConstant(maxvalue: longint);
  1597. var
  1598. strlength: byte;
  1599. asmsym,
  1600. expr: string;
  1601. value : longint;
  1602. Begin
  1603. strlength:=0; { assume it is a DB }
  1604. Repeat
  1605. Case actasmtoken of
  1606. AS_STRING:
  1607. Begin
  1608. if maxvalue = $ffff then
  1609. strlength:=2
  1610. else
  1611. if maxvalue = longint($ffffffff) then
  1612. strlength:=4;
  1613. { DD and DW cases }
  1614. if strlength <> 0 then
  1615. Begin
  1616. if Not PadZero(actasmpattern,strlength) then
  1617. Message(scan_f_string_exceeds_line);
  1618. end;
  1619. expr:=actasmpattern;
  1620. Consume(AS_STRING);
  1621. Case actasmtoken of
  1622. AS_COMMA:
  1623. Consume(AS_COMMA);
  1624. AS_END,
  1625. AS_SEPARATOR: ;
  1626. else
  1627. Message(asmr_e_invalid_string_expression);
  1628. end;
  1629. ConcatString(curlist,expr);
  1630. end;
  1631. AS_PLUS,
  1632. AS_MINUS,
  1633. AS_LPAREN,
  1634. AS_NOT,
  1635. AS_INTNUM,
  1636. AS_ID :
  1637. Begin
  1638. BuildConstSymbolExpression(false,false,value,asmsym);
  1639. if asmsym<>'' then
  1640. begin
  1641. if maxvalue<>longint($ffffffff) then
  1642. Message(asmr_w_const32bit_for_address);
  1643. ConcatConstSymbol(curlist,asmsym,value)
  1644. end
  1645. else
  1646. ConcatConstant(curlist,value,maxvalue);
  1647. end;
  1648. AS_COMMA:
  1649. Consume(AS_COMMA);
  1650. AS_END,
  1651. AS_SEPARATOR:
  1652. break;
  1653. else
  1654. begin
  1655. Message(asmr_e_syn_constant);
  1656. RecoverConsume(false);
  1657. end
  1658. end;
  1659. Until false;
  1660. end;
  1661. Function Assemble: Ptree;
  1662. Var
  1663. hl : PAsmLabel;
  1664. instr : T386IntelInstruction;
  1665. Begin
  1666. Message1(asmr_d_start_reading,'intel');
  1667. inexpression:=FALSE;
  1668. firsttoken:=TRUE;
  1669. if assigned(procinfo^.returntype.def) and
  1670. (is_fpu(procinfo^.returntype.def) or
  1671. ret_in_acc(procinfo^.returntype.def)) then
  1672. procinfo^.funcret_state:=vs_assigned;
  1673. { sets up all opcode and register tables in uppercase }
  1674. if not _asmsorted then
  1675. Begin
  1676. SetupTables;
  1677. _asmsorted:=TRUE;
  1678. end;
  1679. curlist:=new(paasmoutput,init);
  1680. { setup label linked list }
  1681. new(LocalLabelList,Init);
  1682. { start tokenizer }
  1683. c:=current_scanner^.asmgetchar;
  1684. gettoken;
  1685. { main loop }
  1686. repeat
  1687. case actasmtoken of
  1688. AS_LLABEL:
  1689. Begin
  1690. if CreateLocalLabel(actasmpattern,hl,true) then
  1691. ConcatLabel(curlist,hl);
  1692. Consume(AS_LLABEL);
  1693. end;
  1694. AS_LABEL:
  1695. Begin
  1696. if SearchLabel(upper(actasmpattern),hl,true) then
  1697. ConcatLabel(curlist,hl)
  1698. else
  1699. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1700. Consume(AS_LABEL);
  1701. end;
  1702. AS_DW :
  1703. Begin
  1704. inexpression:=true;
  1705. Consume(AS_DW);
  1706. BuildConstant($ffff);
  1707. inexpression:=false;
  1708. end;
  1709. AS_DB :
  1710. Begin
  1711. inexpression:=true;
  1712. Consume(AS_DB);
  1713. BuildConstant($ff);
  1714. inexpression:=false;
  1715. end;
  1716. AS_DD :
  1717. Begin
  1718. inexpression:=true;
  1719. Consume(AS_DD);
  1720. BuildConstant($ffffffff);
  1721. inexpression:=false;
  1722. end;
  1723. AS_OPCODE :
  1724. Begin
  1725. instr.init;
  1726. instr.BuildOpcode;
  1727. { We need AT&T style operands }
  1728. instr.SwapOperands;
  1729. { Must be done with args in ATT order }
  1730. instr.CheckNonCommutativeOpcodes;
  1731. instr.AddReferenceSizes;
  1732. instr.SetInstructionOpsize;
  1733. instr.CheckOperandSizes;
  1734. instr.ConcatInstruction(curlist);
  1735. instr.done;
  1736. end;
  1737. AS_SEPARATOR :
  1738. Begin
  1739. Consume(AS_SEPARATOR);
  1740. end;
  1741. AS_END :
  1742. break; { end assembly block }
  1743. else
  1744. Begin
  1745. Message(asmr_e_syntax_error);
  1746. RecoverConsume(false);
  1747. end;
  1748. end; { end case }
  1749. until false;
  1750. { Check LocalLabelList }
  1751. LocalLabelList^.CheckEmitted;
  1752. dispose(LocalLabelList,Done);
  1753. { Return the list in an asmnode }
  1754. assemble:=genasmnode(curlist);
  1755. Message1(asmr_d_finish_reading,'intel');
  1756. end;
  1757. {*****************************************************************************
  1758. Initialize
  1759. *****************************************************************************}
  1760. var
  1761. old_exit : pointer;
  1762. procedure ra386int_exit;{$ifndef FPC}far;{$endif}
  1763. begin
  1764. if assigned(iasmops) then
  1765. dispose(iasmops,done);
  1766. if assigned(iasmregs) then
  1767. dispose(iasmregs);
  1768. exitproc:=old_exit;
  1769. end;
  1770. begin
  1771. old_exit:=exitproc;
  1772. exitproc:=@ra386int_exit;
  1773. end.
  1774. {
  1775. $Log$
  1776. Revision 1.76 2000-06-18 19:09:30 peter
  1777. * fixed + record.field expressions
  1778. Revision 1.75 2000/06/18 18:07:06 peter
  1779. * use new illegal_char method
  1780. Revision 1.74 2000/06/15 18:07:08 peter
  1781. * fix constant parsing which gave an error when constants were used
  1782. Revision 1.73 2000/06/14 19:02:41 peter
  1783. * fixed TYPE with records and fields
  1784. * added TYPE support for ATT reader else it wouldn't be possible to
  1785. get the size of a type/variable
  1786. Revision 1.72 2000/06/14 16:52:09 peter
  1787. * fixed reference parsing
  1788. Revision 1.71 2000/05/23 20:36:28 peter
  1789. + typecasting support for variables, but be carefull as word,byte can't
  1790. be used because they are reserved assembler keywords
  1791. Revision 1.70 2000/05/18 17:05:16 peter
  1792. * fixed size of const parameters in asm readers
  1793. Revision 1.69 2000/05/12 21:57:02 pierre
  1794. + use of a dictionary object
  1795. for faster opcode searching in assembler readers
  1796. implemented by Kovacs Attila Zoltan
  1797. Revision 1.68 2000/05/12 21:26:23 pierre
  1798. * fix the FDIV FDIVR FSUB FSUBR and popping equivalent
  1799. simply by swapping from reverse to normal and vice-versa
  1800. when passing from one syntax to the other !
  1801. Revision 1.67 2000/05/11 09:56:21 pierre
  1802. * fixed several compare problems between longints and
  1803. const > $80000000 that are treated as int64 constanst
  1804. by Delphi reported by Kovacs Attila Zoltan
  1805. Revision 1.66 2000/05/09 11:56:26 pierre
  1806. * Issue an error if opcode is not found
  1807. Revision 1.65 2000/05/08 13:23:04 peter
  1808. * fixed reference parsing
  1809. Revision 1.64 2000/04/29 12:51:34 peter
  1810. * fixed offset support intel reader, the gotoffset variable was not
  1811. always reset
  1812. * moved check for local/para to be only used for varsym
  1813. Revision 1.63 2000/03/28 22:11:48 pierre
  1814. + add a warning if esi is base and index in object assembler code
  1815. Revision 1.62 2000/03/27 21:18:55 pierre
  1816. * "segss" prefix in Intel is converted into "ss" in ATT
  1817. and vice-versa. Fixes web bug 892.
  1818. Revision 1.61 2000/03/15 23:10:01 pierre
  1819. * fix for bug 848 (that still genrated wrong code)
  1820. + better testing for variables used in assembler
  1821. (gives an error if variable is not directly reachable !)
  1822. Revision 1.60 2000/03/02 11:48:31 pierre
  1823. * fix for bug 848
  1824. Revision 1.59 2000/02/13 22:46:28 florian
  1825. * fixed an internalerror with writeln
  1826. * fixed arrayconstructor_to_set to force the generation of better code
  1827. and added a more strict type checking
  1828. Revision 1.58 2000/02/09 13:23:02 peter
  1829. * log truncated
  1830. Revision 1.57 2000/01/07 01:14:36 peter
  1831. * updated copyright to 2000
  1832. Revision 1.56 1999/12/18 20:00:33 florian
  1833. * Bug reported by Marco fixed: Intel assembler reader: fld qword ptr x
  1834. was read as fldq x but it must be fldl x
  1835. Revision 1.55 1999/12/01 12:42:32 peter
  1836. * fixed bug 698
  1837. * removed some notes about unused vars
  1838. Revision 1.54 1999/11/30 10:40:53 peter
  1839. + ttype, tsymlist
  1840. Revision 1.53 1999/11/17 17:05:03 pierre
  1841. * Notes/hints changes
  1842. Revision 1.52 1999/11/09 23:06:46 peter
  1843. * esi_offset -> selfpointer_offset to be newcg compatible
  1844. * hcogegen -> cgbase fixes for newcg
  1845. Revision 1.51 1999/11/06 14:34:24 peter
  1846. * truncated log to 20 revs
  1847. Revision 1.50 1999/10/01 07:59:21 peter
  1848. * fixed object field parsing
  1849. Revision 1.49 1999/09/27 23:44:58 peter
  1850. * procinfo is now a pointer
  1851. * support for result setting in sub procedure
  1852. Revision 1.48 1999/09/20 16:39:01 peter
  1853. * cs_create_smart instead of cs_smartlink
  1854. * -CX is create smartlink
  1855. * -CD is create dynamic, but does nothing atm.
  1856. Revision 1.47 1999/09/15 20:35:43 florian
  1857. * small fix to operator overloading when in MMX mode
  1858. + the compiler uses now fldz and fld1 if possible
  1859. + some fixes to floating point registers
  1860. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  1861. * .... ???
  1862. Revision 1.46 1999/09/08 16:04:03 peter
  1863. * better support for object fields and more error checks for
  1864. field accesses which create buggy code
  1865. Revision 1.45 1999/09/07 13:03:10 peter
  1866. * better OFFSET support for reference reading
  1867. Revision 1.44 1999/09/07 07:45:41 peter
  1868. * TYPE support
  1869. Revision 1.43 1999/08/13 21:28:36 peter
  1870. * more reference types support
  1871. * arraydef size returns elementsize, also for multiple indexing array
  1872. Revision 1.42 1999/08/04 00:23:27 florian
  1873. * renamed i386asm and i386base to cpuasm and cpubase
  1874. Revision 1.41 1999/07/24 11:17:16 peter
  1875. * suffix parsing for at&t fixed for things like movsbl
  1876. * string constants are now handle correctly and also allowed in
  1877. constant expressions
  1878. }