ra386int.pas 52 KB

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