ra386int.pas 53 KB

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