ra386int.pas 52 KB

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