ra386int.pas 44 KB

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