ra386int.pas 52 KB

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