ra386int.pas 56 KB

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