ra386int.pas 55 KB

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