ra386int.pas 56 KB

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