ra386int.pas 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002
  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. Begin
  570. Message(scan_f_illegal_char);
  571. end;
  572. end;
  573. end;
  574. end;
  575. function consume(t : tasmtoken):boolean;
  576. begin
  577. Consume:=true;
  578. if t<>actasmtoken then
  579. begin
  580. Message2(scan_f_syn_expected,token2str[t],token2str[actasmtoken]);
  581. Consume:=false;
  582. end;
  583. repeat
  584. gettoken;
  585. until actasmtoken<>AS_NONE;
  586. end;
  587. procedure RecoverConsume(allowcomma:boolean);
  588. begin
  589. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  590. begin
  591. if allowcomma and (actasmtoken=AS_COMMA) then
  592. break;
  593. Consume(actasmtoken);
  594. end;
  595. end;
  596. {*****************************************************************************
  597. Parsing Helpers
  598. *****************************************************************************}
  599. Procedure BuildRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  600. { Description: This routine builds up a record offset after a AS_DOT }
  601. { token is encountered. }
  602. { On entry actasmtoken should be equal to AS_DOT }
  603. var
  604. s : string;
  605. Begin
  606. offset:=0;
  607. size:=0;
  608. s:=expr;
  609. while (actasmtoken=AS_DOT) do
  610. begin
  611. Consume(AS_DOT);
  612. if actasmtoken=AS_ID then
  613. s:=s+'.'+actasmpattern;
  614. if not Consume(AS_ID) then
  615. begin
  616. RecoverConsume(true);
  617. break;
  618. end;
  619. end;
  620. if not GetRecordOffsetSize(s,offset,size) then
  621. Message(asmr_e_building_record_offset);
  622. end;
  623. Procedure BuildConstSymbolExpression(needofs,exitreg:boolean;var value:longint;var asmsym:string);
  624. var
  625. tempstr,expr,hs : string;
  626. parenlevel,l,k : longint;
  627. errorflag : boolean;
  628. prevtok : tasmtoken;
  629. hl : PAsmLabel;
  630. sym : psym;
  631. Begin
  632. { reset }
  633. value:=0;
  634. asmsym:='';
  635. errorflag:=FALSE;
  636. tempstr:='';
  637. expr:='';
  638. inexpression:=TRUE;
  639. parenlevel:=0;
  640. Repeat
  641. Case actasmtoken of
  642. AS_LPAREN:
  643. Begin
  644. Consume(AS_LPAREN);
  645. expr:=expr + '(';
  646. inc(parenlevel);
  647. end;
  648. AS_RPAREN:
  649. Begin
  650. Consume(AS_RPAREN);
  651. expr:=expr + ')';
  652. dec(parenlevel);
  653. end;
  654. AS_SHL:
  655. Begin
  656. Consume(AS_SHL);
  657. expr:=expr + '<';
  658. end;
  659. AS_SHR:
  660. Begin
  661. Consume(AS_SHR);
  662. expr:=expr + '>';
  663. end;
  664. AS_SLASH:
  665. Begin
  666. Consume(AS_SLASH);
  667. expr:=expr + '/';
  668. end;
  669. AS_MOD:
  670. Begin
  671. Consume(AS_MOD);
  672. expr:=expr + '%';
  673. end;
  674. AS_STAR:
  675. Begin
  676. Consume(AS_STAR);
  677. if exitreg and (actasmtoken=AS_REGISTER) then
  678. break;
  679. expr:=expr + '*';
  680. end;
  681. AS_PLUS:
  682. Begin
  683. Consume(AS_PLUS);
  684. if exitreg and (actasmtoken=AS_REGISTER) then
  685. break;
  686. expr:=expr + '+';
  687. end;
  688. AS_MINUS:
  689. Begin
  690. Consume(AS_MINUS);
  691. expr:=expr + '-';
  692. end;
  693. AS_AND:
  694. Begin
  695. Consume(AS_AND);
  696. expr:=expr + '&';
  697. end;
  698. AS_NOT:
  699. Begin
  700. Consume(AS_NOT);
  701. expr:=expr + '~';
  702. end;
  703. AS_XOR:
  704. Begin
  705. Consume(AS_XOR);
  706. expr:=expr + '^';
  707. end;
  708. AS_OR:
  709. Begin
  710. Consume(AS_OR);
  711. expr:=expr + '|';
  712. end;
  713. AS_INTNUM:
  714. Begin
  715. expr:=expr + actasmpattern;
  716. Consume(AS_INTNUM);
  717. end;
  718. AS_OFFSET:
  719. begin
  720. Consume(AS_OFFSET);
  721. if actasmtoken<>AS_ID then
  722. Message(asmr_e_offset_without_identifier);
  723. end;
  724. AS_TYPE:
  725. begin
  726. Consume(AS_TYPE);
  727. if actasmtoken<>AS_ID then
  728. Message(asmr_e_type_without_identifier)
  729. else
  730. begin
  731. getsym(actasmpattern,false);
  732. Consume(AS_ID);
  733. if assigned(srsym) then
  734. begin
  735. l:=0;
  736. case srsym^.typ of
  737. varsym :
  738. l:=pvarsym(srsym)^.getsize;
  739. typedconstsym :
  740. l:=ptypedconstsym(srsym)^.getsize;
  741. typesym :
  742. l:=ptypesym(srsym)^.restype.def^.size;
  743. else
  744. Message(asmr_e_wrong_sym_type);
  745. end;
  746. str(l,tempstr);
  747. expr:=expr+tempstr;
  748. end
  749. else
  750. Message1(sym_e_unknown_id,actasmpattern);
  751. end;
  752. end;
  753. AS_STRING:
  754. Begin
  755. l:=0;
  756. case Length(actasmpattern) of
  757. 1 :
  758. l:=ord(actasmpattern[1]);
  759. 2 :
  760. l:=ord(actasmpattern[2]) + ord(actasmpattern[1]) shl 8;
  761. 3 :
  762. l:=ord(actasmpattern[3]) +
  763. Ord(actasmpattern[2]) shl 8 + ord(actasmpattern[1]) shl 16;
  764. 4 :
  765. l:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  766. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  767. else
  768. Message1(asmr_e_invalid_string_as_opcode_operand,actasmpattern);
  769. end;
  770. str(l, tempstr);
  771. expr:=expr + tempstr;
  772. Consume(AS_STRING);
  773. end;
  774. AS_ID:
  775. Begin
  776. tempstr:=actasmpattern;
  777. prevtok:=prevasmtoken;
  778. consume(AS_ID);
  779. if actasmtoken=AS_DOT then
  780. begin
  781. BuildRecordOffsetSize(tempstr,l,k);
  782. str(l, tempstr);
  783. expr:=expr + tempstr;
  784. end
  785. else
  786. if SearchIConstant(tempstr,l) then
  787. begin
  788. str(l, tempstr);
  789. expr:=expr + tempstr;
  790. end
  791. else
  792. begin
  793. hs:='';
  794. if is_locallabel(tempstr) then
  795. begin
  796. CreateLocalLabel(tempstr,hl,false);
  797. hs:=hl^.name
  798. end
  799. else
  800. if SearchLabel(tempstr,hl,false) then
  801. hs:=hl^.name
  802. else
  803. begin
  804. getsym(tempstr,false);
  805. sym:=srsym;
  806. if assigned(sym) then
  807. begin
  808. case srsym^.typ of
  809. varsym :
  810. begin
  811. if sym^.owner^.symtabletype in [localsymtable,parasymtable] then
  812. Message(asmr_e_no_local_or_para_allowed);
  813. hs:=pvarsym(srsym)^.mangledname;
  814. end;
  815. typedconstsym :
  816. hs:=ptypedconstsym(srsym)^.mangledname;
  817. procsym :
  818. hs:=pprocsym(srsym)^.mangledname;
  819. else
  820. Message(asmr_e_wrong_sym_type);
  821. end;
  822. end
  823. else
  824. Message1(sym_e_unknown_id,tempstr);
  825. end;
  826. { symbol found? }
  827. if hs<>'' then
  828. begin
  829. if needofs and (prevtok<>AS_OFFSET) then
  830. Message(asmr_e_need_offset);
  831. if asmsym='' then
  832. asmsym:=hs
  833. else
  834. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  835. if (expr='') or (expr[length(expr)]='+') then
  836. begin
  837. delete(expr,length(expr),1);
  838. if not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END,AS_RBRACKET]) then
  839. Message(asmr_e_only_add_relocatable_symbol);
  840. end
  841. else
  842. Message(asmr_e_only_add_relocatable_symbol);
  843. end;
  844. end;
  845. end;
  846. AS_END,
  847. AS_RBRACKET,
  848. AS_SEPARATOR,
  849. AS_COMMA:
  850. Begin
  851. break;
  852. end;
  853. else
  854. Begin
  855. { write error only once. }
  856. if not errorflag then
  857. Message(asmr_e_invalid_constant_expression);
  858. { consume tokens until we find COMMA or SEPARATOR }
  859. Consume(actasmtoken);
  860. errorflag:=TRUE;
  861. end;
  862. end;
  863. Until false;
  864. { calculate expression }
  865. if not ErrorFlag then
  866. value:=CalculateExpression(expr)
  867. else
  868. value:=0;
  869. { no longer in an expression }
  870. inexpression:=FALSE;
  871. end;
  872. Function BuildConstExpression:longint;
  873. var
  874. l : longint;
  875. hs : string;
  876. begin
  877. BuildConstSymbolExpression(false,false,l,hs);
  878. if hs<>'' then
  879. Message(asmr_e_relocatable_symbol_not_allowed);
  880. BuildConstExpression:=l;
  881. end;
  882. Function BuildRefConstExpression:longint;
  883. var
  884. l : longint;
  885. hs : string;
  886. begin
  887. BuildConstSymbolExpression(false,true,l,hs);
  888. if hs<>'' then
  889. Message(asmr_e_relocatable_symbol_not_allowed);
  890. BuildRefConstExpression:=l;
  891. end;
  892. {****************************************************************************
  893. T386IntelOperand
  894. ****************************************************************************}
  895. type
  896. P386IntelOperand=^T386IntelOperand;
  897. T386IntelOperand=object(T386Operand)
  898. Procedure BuildOperand;virtual;
  899. private
  900. Procedure BuildReference;
  901. Procedure BuildConstant;
  902. end;
  903. Procedure T386IntelOperand.BuildReference;
  904. var
  905. k,l : longint;
  906. tempstr2,
  907. tempstr,hs : string;
  908. code : integer;
  909. hreg,
  910. oldbase : tregister;
  911. GotStar,GotOffset,HadVar,
  912. GotPlus,Negative : boolean;
  913. Begin
  914. Consume(AS_LBRACKET);
  915. InitRef;
  916. GotStar:=false;
  917. GotPlus:=true;
  918. GotOffset:=false;
  919. Negative:=false;
  920. repeat
  921. if GotOffset and (actasmtoken<>AS_ID) then
  922. Message(asmr_e_invalid_reference_syntax);
  923. Case actasmtoken of
  924. AS_ID: { Constant reference expression OR variable reference expression }
  925. Begin
  926. if not GotPlus then
  927. Message(asmr_e_invalid_reference_syntax);
  928. if actasmpattern[1] = '@' then
  929. Message(asmr_e_local_label_not_allowed_as_ref);
  930. GotStar:=false;
  931. GotPlus:=false;
  932. if SearchIConstant(actasmpattern,l) or
  933. SearchRecordType(actasmpattern) then
  934. begin
  935. l:=BuildRefConstExpression;
  936. GotPlus:=(prevasmtoken=AS_PLUS);
  937. GotStar:=(prevasmtoken=AS_STAR);
  938. if GotStar then
  939. opr.ref.scalefactor:=l
  940. else
  941. begin
  942. if negative then
  943. Dec(opr.ref.offset,l)
  944. else
  945. Inc(opr.ref.offset,l);
  946. end;
  947. end
  948. else
  949. Begin
  950. if hasvar and not GotOffset then
  951. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  952. HadVar:=hasvar and GotOffset;
  953. if negative then
  954. Message(asmr_e_only_add_relocatable_symbol);
  955. oldbase:=opr.ref.base;
  956. opr.ref.base:=R_NO;
  957. tempstr:=actasmpattern;
  958. Consume(AS_ID);
  959. { typecasting? }
  960. if (actasmtoken=AS_LPAREN) and
  961. SearchType(tempstr) then
  962. begin
  963. hastype:=true;
  964. Consume(AS_LPAREN);
  965. tempstr2:=actasmpattern;
  966. Consume(AS_ID);
  967. Consume(AS_RPAREN);
  968. if not SetupVar(tempstr2,GotOffset) then
  969. Message1(sym_e_unknown_id,tempstr2);
  970. end
  971. else
  972. if not SetupVar(tempstr,GotOffset) then
  973. Message1(sym_e_unknown_id,tempstr);
  974. { record.field ? }
  975. if actasmtoken=AS_DOT then
  976. begin
  977. BuildRecordOffsetSize(tempstr,l,k);
  978. inc(opr.ref.offset,l);
  979. end;
  980. if GotOffset then
  981. begin
  982. if hasvar and (opr.ref.base=procinfo^.framepointer) then
  983. begin
  984. opr.ref.base:=R_NO;
  985. hasvar:=hadvar;
  986. end
  987. else
  988. begin
  989. if hasvar and hadvar then
  990. Message(asmr_e_cant_have_multiple_relocatable_symbols);
  991. { should we allow ?? }
  992. end;
  993. end;
  994. { is the base register loaded by the var ? }
  995. if (opr.ref.base<>R_NO) then
  996. begin
  997. { check if we can move the old base to the index register }
  998. if (opr.ref.index<>R_NO) then
  999. Message(asmr_e_wrong_base_index)
  1000. else if assigned(procinfo^._class) and
  1001. (oldbase=self_pointer) and
  1002. (opr.ref.base=self_pointer) then
  1003. begin
  1004. Message(asmr_w_possible_object_field_bug);
  1005. { warn but accept... who knows what people
  1006. caninvent in assembler ! }
  1007. opr.ref.index:=oldbase;
  1008. end
  1009. else
  1010. opr.ref.index:=oldbase;
  1011. end
  1012. else
  1013. opr.ref.base:=oldbase;
  1014. { we can't have a Constant here so add the constant value to the
  1015. offset }
  1016. if opr.typ=OPR_CONSTANT then
  1017. begin
  1018. opr.typ:=OPR_REFERENCE;
  1019. inc(opr.ref.offset,opr.val);
  1020. end;
  1021. end;
  1022. GotOffset:=false;
  1023. end;
  1024. AS_PLUS :
  1025. Begin
  1026. Consume(AS_PLUS);
  1027. Negative:=false;
  1028. GotPlus:=true;
  1029. GotStar:=false;
  1030. end;
  1031. AS_MINUS :
  1032. begin
  1033. Consume(AS_MINUS);
  1034. Negative:=true;
  1035. GotPlus:=true;
  1036. GotStar:=false;
  1037. end;
  1038. AS_STAR : { Scaling, with eax*4 order }
  1039. begin
  1040. Consume(AS_STAR);
  1041. hs:='';
  1042. l:=0;
  1043. case actasmtoken of
  1044. AS_LPAREN :
  1045. l:=BuildConstExpression;
  1046. AS_INTNUM:
  1047. Begin
  1048. hs:=actasmpattern;
  1049. Consume(AS_INTNUM);
  1050. end;
  1051. AS_REGISTER :
  1052. begin
  1053. if opr.ref.scalefactor=0 then
  1054. Message(asmr_e_wrong_scale_factor);
  1055. end;
  1056. else
  1057. Message(asmr_e_invalid_reference_syntax);
  1058. end;
  1059. if actasmtoken<>AS_REGISTER then
  1060. begin
  1061. if hs<>'' then
  1062. val(hs,l,code);
  1063. opr.ref.scalefactor:=l;
  1064. if l>8 then
  1065. Message(asmr_e_wrong_scale_factor);
  1066. end;
  1067. GotPlus:=false;
  1068. GotStar:=false;
  1069. end;
  1070. AS_REGISTER :
  1071. begin
  1072. if (not GotPlus) and (not GotStar) then
  1073. Message(asmr_e_invalid_reference_syntax);
  1074. hreg:=actasmregister;
  1075. Consume(AS_REGISTER);
  1076. { this register will be the index:
  1077. 1. just read a *
  1078. 2. next token is a *
  1079. 3. base register is already used }
  1080. if (GotStar) or
  1081. (actasmtoken=AS_STAR) or
  1082. (opr.ref.base<>R_NO) then
  1083. begin
  1084. if (opr.ref.index<>R_NO) then
  1085. Message(asmr_e_multiple_index);
  1086. opr.ref.index:=hreg;
  1087. end
  1088. else
  1089. opr.ref.base:=hreg;
  1090. GotPlus:=false;
  1091. GotStar:=false;
  1092. end;
  1093. AS_OFFSET :
  1094. begin
  1095. Consume(AS_OFFSET);
  1096. GotOffset:=true;
  1097. end;
  1098. AS_TYPE,
  1099. AS_NOT,
  1100. AS_INTNUM,
  1101. AS_LPAREN : { Constant reference expression }
  1102. begin
  1103. if not GotPlus then
  1104. Message(asmr_e_invalid_reference_syntax);
  1105. l:=BuildRefConstExpression;
  1106. GotPlus:=(prevasmtoken=AS_PLUS);
  1107. GotStar:=(prevasmtoken=AS_STAR);
  1108. if GotStar then
  1109. opr.ref.scalefactor:=l
  1110. else
  1111. begin
  1112. if negative then
  1113. Dec(opr.ref.offset,l)
  1114. else
  1115. Inc(opr.ref.offset,l);
  1116. end;
  1117. end;
  1118. AS_RBRACKET :
  1119. begin
  1120. if GotPlus then
  1121. Message(asmr_e_invalid_reference_syntax);
  1122. Consume(AS_RBRACKET);
  1123. break;
  1124. end;
  1125. else
  1126. Begin
  1127. Message(asmr_e_invalid_reference_syntax);
  1128. RecoverConsume(true);
  1129. break;
  1130. end;
  1131. end;
  1132. until false;
  1133. end;
  1134. Procedure T386IntelOperand.BuildConstant;
  1135. var
  1136. l : longint;
  1137. tempstr : string;
  1138. begin
  1139. BuildConstSymbolExpression(true,false,l,tempstr);
  1140. if tempstr<>'' then
  1141. begin
  1142. opr.typ:=OPR_SYMBOL;
  1143. opr.symofs:=l;
  1144. opr.symbol:=newasmsymbol(tempstr);
  1145. end
  1146. else
  1147. begin
  1148. opr.typ:=OPR_CONSTANT;
  1149. opr.val:=l;
  1150. end;
  1151. end;
  1152. Procedure T386IntelOperand.BuildOperand;
  1153. var
  1154. tempstr,
  1155. expr : string;
  1156. tempreg : tregister;
  1157. l : longint;
  1158. hl : PAsmLabel;
  1159. procedure AddLabelOperand(hl:pasmlabel);
  1160. begin
  1161. if is_calljmp(actopcode) then
  1162. begin
  1163. opr.typ:=OPR_SYMBOL;
  1164. opr.symbol:=hl;
  1165. end
  1166. else
  1167. begin
  1168. InitRef;
  1169. opr.ref.symbol:=hl;
  1170. end;
  1171. end;
  1172. procedure MaybeRecordOffset;
  1173. var
  1174. l,
  1175. toffset,
  1176. tsize : longint;
  1177. begin
  1178. if not(actasmtoken in [AS_DOT,AS_PLUS,AS_MINUS]) then
  1179. exit;
  1180. l:=0;
  1181. if actasmtoken=AS_DOT then
  1182. begin
  1183. { if no type was specified before the [] then we expect the
  1184. first ID to be the type }
  1185. if expr='' then
  1186. begin
  1187. consume(AS_DOT);
  1188. if actasmtoken=AS_ID then
  1189. begin
  1190. expr:=actasmpattern;
  1191. consume(AS_ID);
  1192. { now the next one must the be the dot }
  1193. if actasmtoken<>AS_DOT then
  1194. begin
  1195. Message(asmr_e_building_record_offset);
  1196. expr:='';
  1197. end;
  1198. end
  1199. else
  1200. Message(asmr_e_no_var_type_specified)
  1201. end;
  1202. if expr<>'' then
  1203. begin
  1204. BuildRecordOffsetSize(expr,toffset,tsize);
  1205. inc(l,toffset);
  1206. SetSize(tsize,true);
  1207. end;
  1208. end;
  1209. if actasmtoken in [AS_PLUS,AS_MINUS] then
  1210. inc(l,BuildConstExpression);
  1211. if (opr.typ=OPR_REFERENCE) then
  1212. begin
  1213. { don't allow direct access to fields of parameters, becuase that
  1214. will generate buggy code. Allow it only for explicit typecasting }
  1215. if (not hastype) then
  1216. begin
  1217. case opr.ref.options of
  1218. ref_parafixup :
  1219. Message(asmr_e_cannot_access_field_directly_for_parameters);
  1220. ref_selffixup :
  1221. Message(asmr_e_cannot_access_object_field_directly);
  1222. end;
  1223. end;
  1224. inc(opr.ref.offset,l)
  1225. end
  1226. else
  1227. inc(opr.val,l);
  1228. end;
  1229. Begin
  1230. expr:='';
  1231. case actasmtoken of
  1232. AS_OFFSET,
  1233. AS_TYPE,
  1234. AS_INTNUM,
  1235. AS_PLUS,
  1236. AS_MINUS,
  1237. AS_NOT,
  1238. AS_LPAREN,
  1239. AS_STRING :
  1240. Begin
  1241. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1242. Message(asmr_e_invalid_operand_type);
  1243. BuildConstant;
  1244. end;
  1245. AS_ID : { A constant expression, or a Variable ref. }
  1246. Begin
  1247. { Label or Special symbol reference? }
  1248. if actasmpattern[1] = '@' then
  1249. Begin
  1250. if actasmpattern = '@RESULT' then
  1251. Begin
  1252. InitRef;
  1253. SetupResult;
  1254. Consume(AS_ID);
  1255. end
  1256. else
  1257. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1258. begin
  1259. Message(asmr_w_CODE_and_DATA_not_supported);
  1260. Consume(AS_ID);
  1261. end
  1262. else
  1263. { Local Label }
  1264. begin
  1265. CreateLocalLabel(actasmpattern,hl,false);
  1266. Consume(AS_ID);
  1267. AddLabelOperand(hl);
  1268. if not (actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1269. Message(asmr_e_syntax_error);
  1270. end;
  1271. end
  1272. else
  1273. { support result for delphi modes }
  1274. if (m_objpas in aktmodeswitches) and (actasmpattern='RESULT') then
  1275. begin
  1276. InitRef;
  1277. SetUpResult;
  1278. Consume(AS_ID);
  1279. end
  1280. { probably a variable or normal expression }
  1281. { or a procedure (such as in CALL ID) }
  1282. else
  1283. Begin
  1284. { is it a constant ? }
  1285. if SearchIConstant(actasmpattern,l) then
  1286. Begin
  1287. if not (opr.typ in [OPR_NONE,OPR_CONSTANT]) then
  1288. Message(asmr_e_invalid_operand_type);
  1289. BuildConstant;
  1290. end
  1291. else
  1292. { Check for pascal label }
  1293. if SearchLabel(actasmpattern,hl,false) then
  1294. begin
  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. else
  1301. { is it a normal variable ? }
  1302. Begin
  1303. InitRef;
  1304. expr:=actasmpattern;
  1305. Consume(AS_ID);
  1306. { typecasting? }
  1307. if (actasmtoken=AS_LPAREN) and
  1308. SearchType(expr) then
  1309. begin
  1310. hastype:=true;
  1311. Consume(AS_LPAREN);
  1312. tempstr:=actasmpattern;
  1313. Consume(AS_ID);
  1314. Consume(AS_RPAREN);
  1315. if SetupVar(tempstr,false) then
  1316. begin
  1317. MaybeRecordOffset;
  1318. { add a constant expression? }
  1319. if (actasmtoken=AS_PLUS) then
  1320. begin
  1321. l:=BuildConstExpression;
  1322. if opr.typ=OPR_CONSTANT then
  1323. inc(opr.val,l)
  1324. else
  1325. inc(opr.ref.offset,l);
  1326. end
  1327. end
  1328. else
  1329. Message1(sym_e_unknown_id,tempstr);
  1330. end
  1331. else
  1332. begin
  1333. if SetupVar(expr,false) then
  1334. begin
  1335. MaybeRecordOffset;
  1336. { add a constant expression? }
  1337. if (actasmtoken=AS_PLUS) then
  1338. begin
  1339. l:=BuildConstExpression;
  1340. if opr.typ=OPR_CONSTANT then
  1341. inc(opr.val,l)
  1342. else
  1343. inc(opr.ref.offset,l);
  1344. end
  1345. end
  1346. else
  1347. Begin
  1348. { not a variable, check special variables.. }
  1349. if expr = 'SELF' then
  1350. SetupSelf
  1351. else
  1352. Message1(sym_e_unknown_id,expr);
  1353. end;
  1354. end;
  1355. end;
  1356. { handle references }
  1357. if actasmtoken=AS_LBRACKET then
  1358. begin
  1359. if opr.typ=OPR_CONSTANT then
  1360. begin
  1361. l:=opr.val;
  1362. opr.typ:=OPR_REFERENCE;
  1363. reset_reference(opr.Ref);
  1364. opr.Ref.Offset:=l;
  1365. end;
  1366. BuildReference;
  1367. MaybeRecordOffset;
  1368. end;
  1369. end;
  1370. end;
  1371. AS_REGISTER : { Register, a variable reference or a constant reference }
  1372. Begin
  1373. { save the type of register used. }
  1374. tempreg:=actasmregister;
  1375. Consume(AS_REGISTER);
  1376. if actasmtoken = AS_COLON then
  1377. Begin
  1378. Consume(AS_COLON);
  1379. InitRef;
  1380. opr.ref.segment:=tempreg;
  1381. BuildReference;
  1382. end
  1383. else
  1384. { Simple register }
  1385. begin
  1386. if not (opr.typ in [OPR_NONE,OPR_REGISTER]) then
  1387. Message(asmr_e_invalid_operand_type);
  1388. opr.typ:=OPR_REGISTER;
  1389. opr.reg:=tempreg;
  1390. size:=reg_2_opsize[opr.reg];
  1391. end;
  1392. end;
  1393. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1394. Begin
  1395. InitRef;
  1396. BuildReference;
  1397. MaybeRecordOffset;
  1398. end;
  1399. AS_SEG :
  1400. Begin
  1401. Message(asmr_e_seg_not_supported);
  1402. Consume(actasmtoken);
  1403. end;
  1404. AS_SEPARATOR,
  1405. AS_END,
  1406. AS_COMMA: ;
  1407. else
  1408. Message(asmr_e_syn_operand);
  1409. end;
  1410. if not(actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1411. begin
  1412. Message(asmr_e_syntax_error);
  1413. RecoverConsume(true);
  1414. end;
  1415. end;
  1416. {*****************************************************************************
  1417. T386IntelInstruction
  1418. *****************************************************************************}
  1419. type
  1420. P386IntelInstruction=^T386IntelInstruction;
  1421. T386IntelInstruction=object(T386Instruction)
  1422. procedure InitOperands;virtual;
  1423. procedure BuildOpcode;virtual;
  1424. end;
  1425. procedure T386IntelInstruction.InitOperands;
  1426. var
  1427. i : longint;
  1428. begin
  1429. for i:=1 to 3 do
  1430. Operands[i]:=new(P386IntelOperand,Init);
  1431. end;
  1432. Procedure T386IntelInstruction.BuildOpCode;
  1433. var
  1434. PrefixOp,OverrideOp: tasmop;
  1435. size : topsize;
  1436. operandnum : longint;
  1437. Begin
  1438. PrefixOp:=A_None;
  1439. OverrideOp:=A_None;
  1440. { prefix seg opcode / prefix opcode }
  1441. repeat
  1442. if is_prefix(actopcode) then
  1443. begin
  1444. PrefixOp:=ActOpcode;
  1445. opcode:=ActOpcode;
  1446. condition:=ActCondition;
  1447. opsize:=ActOpsize;
  1448. ConcatInstruction(curlist);
  1449. Consume(AS_OPCODE);
  1450. end
  1451. else
  1452. if is_override(actopcode) then
  1453. begin
  1454. OverrideOp:=ActOpcode;
  1455. opcode:=ActOpcode;
  1456. condition:=ActCondition;
  1457. opsize:=ActOpsize;
  1458. ConcatInstruction(curlist);
  1459. Consume(AS_OPCODE);
  1460. end
  1461. else
  1462. break;
  1463. { allow for newline after prefix or override }
  1464. while actasmtoken=AS_SEPARATOR do
  1465. Consume(AS_SEPARATOR);
  1466. until (actasmtoken<>AS_OPCODE);
  1467. { opcode }
  1468. if (actasmtoken <> AS_OPCODE) then
  1469. Begin
  1470. Message(asmr_e_invalid_or_missing_opcode);
  1471. RecoverConsume(false);
  1472. exit;
  1473. end;
  1474. { Fill the instr object with the current state }
  1475. Opcode:=ActOpcode;
  1476. condition:=ActCondition;
  1477. opsize:=ActOpsize;
  1478. { Valid combination of prefix/override and instruction ? }
  1479. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1480. Message1(asmr_e_invalid_prefix_and_opcode,actasmpattern);
  1481. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1482. Message1(asmr_e_invalid_override_and_opcode,actasmpattern);
  1483. { We are reading operands, so opcode will be an AS_ID }
  1484. operandnum:=1;
  1485. Consume(AS_OPCODE);
  1486. { Zero operand opcode ? }
  1487. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1488. begin
  1489. operandnum:=0;
  1490. exit;
  1491. end;
  1492. { Read Operands }
  1493. repeat
  1494. case actasmtoken of
  1495. { End of asm operands for this opcode }
  1496. AS_END,
  1497. AS_SEPARATOR :
  1498. break;
  1499. { Operand delimiter }
  1500. AS_COMMA :
  1501. Begin
  1502. if operandnum > MaxOperands then
  1503. Message(asmr_e_too_many_operands)
  1504. else
  1505. Inc(operandnum);
  1506. Consume(AS_COMMA);
  1507. end;
  1508. { Typecast, Constant Expression, Type Specifier }
  1509. AS_DWORD,
  1510. AS_BYTE,
  1511. AS_WORD,
  1512. AS_TBYTE,
  1513. AS_QWORD :
  1514. Begin
  1515. { load the size in a temp variable, so it can be set when the
  1516. operand is read }
  1517. Case actasmtoken of
  1518. AS_DWORD : size:=S_L;
  1519. AS_WORD : size:=S_W;
  1520. AS_BYTE : size:=S_B;
  1521. AS_QWORD : begin
  1522. if (opcode=A_FCOM) or
  1523. (opcode=A_FCOMP) or
  1524. (opcode=A_FDIV) or
  1525. (opcode=A_FDIVR) or
  1526. (opcode=A_FMUL) or
  1527. (opcode=A_FSUB) or
  1528. (opcode=A_FSUBR) or
  1529. (opcode=A_FLD) or
  1530. (opcode=A_FST) or
  1531. (opcode=A_FSTP) or
  1532. (opcode=A_FADD) then
  1533. size:=S_FL
  1534. else
  1535. size:=S_IQ;
  1536. end;
  1537. AS_TBYTE : size:=S_FX;
  1538. end;
  1539. Consume(actasmtoken);
  1540. if actasmtoken=AS_PTR then
  1541. begin
  1542. Consume(AS_PTR);
  1543. Operands[operandnum]^.InitRef;
  1544. end;
  1545. Operands[operandnum]^.BuildOperand;
  1546. { now set the size which was specified by the override }
  1547. Operands[operandnum]^.size:=size;
  1548. end;
  1549. { Type specifier }
  1550. AS_NEAR,
  1551. AS_FAR :
  1552. Begin
  1553. if actasmtoken = AS_NEAR then
  1554. Message(asmr_w_near_ignored)
  1555. else
  1556. Message(asmr_w_far_ignored);
  1557. Consume(actasmtoken);
  1558. if actasmtoken=AS_PTR then
  1559. begin
  1560. Consume(AS_PTR);
  1561. Operands[operandnum]^.InitRef;
  1562. end;
  1563. Operands[operandnum]^.BuildOperand;
  1564. end;
  1565. else
  1566. Operands[operandnum]^.BuildOperand;
  1567. end; { end case }
  1568. until false;
  1569. Ops:=operandnum;
  1570. end;
  1571. Procedure BuildConstant(maxvalue: longint);
  1572. var
  1573. strlength: byte;
  1574. asmsym,
  1575. expr: string;
  1576. value : longint;
  1577. Begin
  1578. strlength:=0; { assume it is a DB }
  1579. Repeat
  1580. Case actasmtoken of
  1581. AS_STRING:
  1582. Begin
  1583. if maxvalue = $ffff then
  1584. strlength:=2
  1585. else
  1586. if maxvalue = longint($ffffffff) then
  1587. strlength:=4;
  1588. { DD and DW cases }
  1589. if strlength <> 0 then
  1590. Begin
  1591. if Not PadZero(actasmpattern,strlength) then
  1592. Message(scan_f_string_exceeds_line);
  1593. end;
  1594. expr:=actasmpattern;
  1595. Consume(AS_STRING);
  1596. Case actasmtoken of
  1597. AS_COMMA:
  1598. Consume(AS_COMMA);
  1599. AS_END,
  1600. AS_SEPARATOR: ;
  1601. else
  1602. Message(asmr_e_invalid_string_expression);
  1603. end;
  1604. ConcatString(curlist,expr);
  1605. end;
  1606. AS_PLUS,
  1607. AS_MINUS,
  1608. AS_LPAREN,
  1609. AS_NOT,
  1610. AS_INTNUM,
  1611. AS_ID :
  1612. Begin
  1613. BuildConstSymbolExpression(false,false,value,asmsym);
  1614. if asmsym<>'' then
  1615. begin
  1616. if maxvalue<>longint($ffffffff) then
  1617. Message(asmr_w_const32bit_for_address);
  1618. ConcatConstSymbol(curlist,asmsym,value)
  1619. end
  1620. else
  1621. ConcatConstant(curlist,value,maxvalue);
  1622. end;
  1623. AS_COMMA:
  1624. Consume(AS_COMMA);
  1625. AS_END,
  1626. AS_SEPARATOR:
  1627. break;
  1628. else
  1629. begin
  1630. Message(asmr_e_syn_constant);
  1631. RecoverConsume(false);
  1632. end
  1633. end;
  1634. Until false;
  1635. end;
  1636. Function Assemble: Ptree;
  1637. Var
  1638. hl : PAsmLabel;
  1639. instr : T386IntelInstruction;
  1640. Begin
  1641. Message1(asmr_d_start_reading,'intel');
  1642. inexpression:=FALSE;
  1643. firsttoken:=TRUE;
  1644. if assigned(procinfo^.returntype.def) and
  1645. (is_fpu(procinfo^.returntype.def) or
  1646. ret_in_acc(procinfo^.returntype.def)) then
  1647. procinfo^.funcret_state:=vs_assigned;
  1648. { sets up all opcode and register tables in uppercase }
  1649. if not _asmsorted then
  1650. Begin
  1651. SetupTables;
  1652. _asmsorted:=TRUE;
  1653. end;
  1654. curlist:=new(paasmoutput,init);
  1655. { setup label linked list }
  1656. new(LocalLabelList,Init);
  1657. { start tokenizer }
  1658. c:=current_scanner^.asmgetchar;
  1659. gettoken;
  1660. { main loop }
  1661. repeat
  1662. case actasmtoken of
  1663. AS_LLABEL:
  1664. Begin
  1665. if CreateLocalLabel(actasmpattern,hl,true) then
  1666. ConcatLabel(curlist,hl);
  1667. Consume(AS_LLABEL);
  1668. end;
  1669. AS_LABEL:
  1670. Begin
  1671. if SearchLabel(upper(actasmpattern),hl,true) then
  1672. ConcatLabel(curlist,hl)
  1673. else
  1674. Message1(asmr_e_unknown_label_identifier,actasmpattern);
  1675. Consume(AS_LABEL);
  1676. end;
  1677. AS_DW :
  1678. Begin
  1679. inexpression:=true;
  1680. Consume(AS_DW);
  1681. BuildConstant($ffff);
  1682. inexpression:=false;
  1683. end;
  1684. AS_DB :
  1685. Begin
  1686. inexpression:=true;
  1687. Consume(AS_DB);
  1688. BuildConstant($ff);
  1689. inexpression:=false;
  1690. end;
  1691. AS_DD :
  1692. Begin
  1693. inexpression:=true;
  1694. Consume(AS_DD);
  1695. BuildConstant($ffffffff);
  1696. inexpression:=false;
  1697. end;
  1698. AS_OPCODE :
  1699. Begin
  1700. instr.init;
  1701. instr.BuildOpcode;
  1702. { We need AT&T style operands }
  1703. instr.SwapOperands;
  1704. { Must be done with args in ATT order }
  1705. instr.CheckNonCommutativeOpcodes;
  1706. instr.AddReferenceSizes;
  1707. instr.SetInstructionOpsize;
  1708. instr.CheckOperandSizes;
  1709. instr.ConcatInstruction(curlist);
  1710. instr.done;
  1711. end;
  1712. AS_SEPARATOR :
  1713. Begin
  1714. Consume(AS_SEPARATOR);
  1715. end;
  1716. AS_END :
  1717. break; { end assembly block }
  1718. else
  1719. Begin
  1720. Message(asmr_e_syntax_error);
  1721. RecoverConsume(false);
  1722. end;
  1723. end; { end case }
  1724. until false;
  1725. { Check LocalLabelList }
  1726. LocalLabelList^.CheckEmitted;
  1727. dispose(LocalLabelList,Done);
  1728. { Return the list in an asmnode }
  1729. assemble:=genasmnode(curlist);
  1730. Message1(asmr_d_finish_reading,'intel');
  1731. end;
  1732. {*****************************************************************************
  1733. Initialize
  1734. *****************************************************************************}
  1735. var
  1736. old_exit : pointer;
  1737. procedure ra386int_exit;{$ifndef FPC}far;{$endif}
  1738. begin
  1739. if assigned(iasmops) then
  1740. dispose(iasmops,done);
  1741. if assigned(iasmregs) then
  1742. dispose(iasmregs);
  1743. exitproc:=old_exit;
  1744. end;
  1745. begin
  1746. old_exit:=exitproc;
  1747. exitproc:=@ra386int_exit;
  1748. end.
  1749. {
  1750. $Log$
  1751. Revision 1.71 2000-05-23 20:36:28 peter
  1752. + typecasting support for variables, but be carefull as word,byte can't
  1753. be used because they are reserved assembler keywords
  1754. Revision 1.70 2000/05/18 17:05:16 peter
  1755. * fixed size of const parameters in asm readers
  1756. Revision 1.69 2000/05/12 21:57:02 pierre
  1757. + use of a dictionary object
  1758. for faster opcode searching in assembler readers
  1759. implemented by Kovacs Attila Zoltan
  1760. Revision 1.68 2000/05/12 21:26:23 pierre
  1761. * fix the FDIV FDIVR FSUB FSUBR and popping equivalent
  1762. simply by swapping from reverse to normal and vice-versa
  1763. when passing from one syntax to the other !
  1764. Revision 1.67 2000/05/11 09:56:21 pierre
  1765. * fixed several compare problems between longints and
  1766. const > $80000000 that are treated as int64 constanst
  1767. by Delphi reported by Kovacs Attila Zoltan
  1768. Revision 1.66 2000/05/09 11:56:26 pierre
  1769. * Issue an error if opcode is not found
  1770. Revision 1.65 2000/05/08 13:23:04 peter
  1771. * fixed reference parsing
  1772. Revision 1.64 2000/04/29 12:51:34 peter
  1773. * fixed offset support intel reader, the gotoffset variable was not
  1774. always reset
  1775. * moved check for local/para to be only used for varsym
  1776. Revision 1.63 2000/03/28 22:11:48 pierre
  1777. + add a warning if esi is base and index in object assembler code
  1778. Revision 1.62 2000/03/27 21:18:55 pierre
  1779. * "segss" prefix in Intel is converted into "ss" in ATT
  1780. and vice-versa. Fixes web bug 892.
  1781. Revision 1.61 2000/03/15 23:10:01 pierre
  1782. * fix for bug 848 (that still genrated wrong code)
  1783. + better testing for variables used in assembler
  1784. (gives an error if variable is not directly reachable !)
  1785. Revision 1.60 2000/03/02 11:48:31 pierre
  1786. * fix for bug 848
  1787. Revision 1.59 2000/02/13 22:46:28 florian
  1788. * fixed an internalerror with writeln
  1789. * fixed arrayconstructor_to_set to force the generation of better code
  1790. and added a more strict type checking
  1791. Revision 1.58 2000/02/09 13:23:02 peter
  1792. * log truncated
  1793. Revision 1.57 2000/01/07 01:14:36 peter
  1794. * updated copyright to 2000
  1795. Revision 1.56 1999/12/18 20:00:33 florian
  1796. * Bug reported by Marco fixed: Intel assembler reader: fld qword ptr x
  1797. was read as fldq x but it must be fldl x
  1798. Revision 1.55 1999/12/01 12:42:32 peter
  1799. * fixed bug 698
  1800. * removed some notes about unused vars
  1801. Revision 1.54 1999/11/30 10:40:53 peter
  1802. + ttype, tsymlist
  1803. Revision 1.53 1999/11/17 17:05:03 pierre
  1804. * Notes/hints changes
  1805. Revision 1.52 1999/11/09 23:06:46 peter
  1806. * esi_offset -> selfpointer_offset to be newcg compatible
  1807. * hcogegen -> cgbase fixes for newcg
  1808. Revision 1.51 1999/11/06 14:34:24 peter
  1809. * truncated log to 20 revs
  1810. Revision 1.50 1999/10/01 07:59:21 peter
  1811. * fixed object field parsing
  1812. Revision 1.49 1999/09/27 23:44:58 peter
  1813. * procinfo is now a pointer
  1814. * support for result setting in sub procedure
  1815. Revision 1.48 1999/09/20 16:39:01 peter
  1816. * cs_create_smart instead of cs_smartlink
  1817. * -CX is create smartlink
  1818. * -CD is create dynamic, but does nothing atm.
  1819. Revision 1.47 1999/09/15 20:35:43 florian
  1820. * small fix to operator overloading when in MMX mode
  1821. + the compiler uses now fldz and fld1 if possible
  1822. + some fixes to floating point registers
  1823. + some math. functions (arctan, ln, sin, cos, sqrt, sqr, pi) are now inlined
  1824. * .... ???
  1825. Revision 1.46 1999/09/08 16:04:03 peter
  1826. * better support for object fields and more error checks for
  1827. field accesses which create buggy code
  1828. Revision 1.45 1999/09/07 13:03:10 peter
  1829. * better OFFSET support for reference reading
  1830. Revision 1.44 1999/09/07 07:45:41 peter
  1831. * TYPE support
  1832. Revision 1.43 1999/08/13 21:28:36 peter
  1833. * more reference types support
  1834. * arraydef size returns elementsize, also for multiple indexing array
  1835. Revision 1.42 1999/08/04 00:23:27 florian
  1836. * renamed i386asm and i386base to cpuasm and cpubase
  1837. Revision 1.41 1999/07/24 11:17:16 peter
  1838. * suffix parsing for at&t fixed for things like movsbl
  1839. * string constants are now handle correctly and also allowed in
  1840. constant expressions
  1841. }