ra386int.pas 45 KB

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