ra386int.pas 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751
  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. const
  69. newline = #10;
  70. firsttoken : boolean = TRUE;
  71. operandnum : byte = 0;
  72. var
  73. _asmsorted : boolean;
  74. inexpression : boolean;
  75. curlist : paasmoutput;
  76. c : char;
  77. prevasmtoken : tasmtoken;
  78. actasmtoken : tasmtoken;
  79. actasmpattern : string;
  80. actasmregister : tregister;
  81. actopcode : tasmop;
  82. actopsize : topsize;
  83. actcondition : tasmcond;
  84. Instr : TInstruction;
  85. labellist : TAsmLabelList;
  86. iasmops : ^op2strtable;
  87. iasmregs : ^reg2strtable;
  88. Procedure SetupTables;
  89. { creates uppercased symbol tables for speed access }
  90. var
  91. i : tasmop;
  92. j : tregister;
  93. Begin
  94. Message(assem_d_creating_lookup_tables);
  95. { opcodes }
  96. new(iasmops);
  97. for i:=firstop to lastop do
  98. iasmops^[i] := upper(int_op2str[i]);
  99. { registers }
  100. new(iasmregs);
  101. for j:=firstreg to lastreg do
  102. iasmregs^[j] := upper(int_reg2str[j]);
  103. end;
  104. {---------------------------------------------------------------------}
  105. { Routines for the tokenizing }
  106. {---------------------------------------------------------------------}
  107. function is_asmopcode(const s: string):boolean;
  108. {*********************************************************************}
  109. { FUNCTION is_asmopcode(s: string):Boolean }
  110. { Description: Determines if the s string is a valid opcode }
  111. { It sets also actopcode and actcondition }
  112. { if so returns TRUE otherwise returns FALSE. }
  113. {*********************************************************************}
  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. Procedure GetToken;
  197. {*********************************************************************}
  198. { FUNCTION GetToken: tasmtoken; }
  199. { Description: This routine returns intel assembler tokens and }
  200. { does some minor syntax error checking. }
  201. {*********************************************************************}
  202. var
  203. len : longint;
  204. forcelabel : boolean;
  205. begin
  206. { save old token and reset new token }
  207. prevasmtoken:=actasmtoken;
  208. actasmtoken:=AS_NONE;
  209. { reset }
  210. forcelabel:=FALSE;
  211. actasmpattern:='';
  212. { while space and tab , continue scan... }
  213. while (c in [' ',#9]) do
  214. c:=current_scanner^.asmgetchar;
  215. { get token pos }
  216. if not (c in [newline,#13,'{',';']) then
  217. current_scanner^.gettokenpos;
  218. { Local Label, Label, Directive, Prefix or Opcode }
  219. if firsttoken and not (c in [newline,#13,'{',';']) then
  220. begin
  221. firsttoken:=FALSE;
  222. if c = '@' then
  223. begin
  224. actasmtoken:=AS_LLABEL; { this is a local label }
  225. { Let us point to the next character }
  226. c:=current_scanner^.asmgetchar;
  227. end;
  228. len:=0;
  229. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  230. begin
  231. { if there is an at_sign, then this must absolutely be a label }
  232. if c = '@' then
  233. forcelabel:=TRUE;
  234. inc(len);
  235. actasmpattern[len]:=c;
  236. c:=current_scanner^.asmgetchar;
  237. end;
  238. actasmpattern[0]:=chr(len);
  239. uppervar(actasmpattern);
  240. { label ? }
  241. if c = ':' then
  242. begin
  243. case actasmtoken of
  244. AS_NONE:
  245. actasmtoken:=AS_LABEL;
  246. AS_LLABEL: ; { do nothing }
  247. end; { end case }
  248. { let us point to the next character }
  249. c:=current_scanner^.asmgetchar;
  250. exit;
  251. end;
  252. { Are we trying to create an identifier with }
  253. { an at-sign...? }
  254. if forcelabel then
  255. Message(assem_e_none_label_contain_at);
  256. { opcode ? }
  257. If is_asmopcode(actasmpattern) then
  258. Begin
  259. { check if we are in an expression }
  260. { then continue with asm directives }
  261. if not inexpression then
  262. exit;
  263. end;
  264. if is_asmdirective(actasmpattern) then
  265. exit;
  266. Message1(assem_e_invalid_operand,actasmpattern);
  267. actasmtoken:=AS_NONE;
  268. exit;
  269. end
  270. else { else firsttoken }
  271. begin
  272. case c of
  273. '@' : { possiblities : - local label reference , such as in jmp @local1 }
  274. { - @Result, @Code or @Data special variables. }
  275. begin
  276. actasmpattern:=c;
  277. c:=current_scanner^.asmgetchar;
  278. while c in ['A'..'Z','a'..'z','0'..'9','_','@'] do
  279. begin
  280. actasmpattern:=actasmpattern + c;
  281. c:=current_scanner^.asmgetchar;
  282. end;
  283. uppervar(actasmpattern);
  284. actasmtoken:=AS_ID;
  285. exit;
  286. end;
  287. 'A'..'Z','a'..'z','_': { identifier, register, opcode, prefix or directive }
  288. begin
  289. actasmpattern:=c;
  290. c:=current_scanner^.asmgetchar;
  291. while c in ['A'..'Z','a'..'z','0'..'9','_'] do
  292. begin
  293. actasmpattern:=actasmpattern + c;
  294. c:=current_scanner^.asmgetchar;
  295. end;
  296. uppervar(actasmpattern);
  297. { after prefix we allow also a new opcode }
  298. If (operandnum=0) and is_asmopcode(actasmpattern) then
  299. Begin
  300. { if we are not in a constant }
  301. { expression than this is an }
  302. { opcode. }
  303. if not inexpression then
  304. exit;
  305. end;
  306. if is_register(actasmpattern) then
  307. exit;
  308. if is_asmdirective(actasmpattern) then
  309. exit;
  310. if is_asmoperator(actasmpattern) then
  311. exit;
  312. actasmtoken:=AS_ID;
  313. exit;
  314. end;
  315. '&' : { override operator... not supported }
  316. begin
  317. Message(assem_w_override_op_not_supported);
  318. c:=current_scanner^.asmgetchar;
  319. actasmtoken:=AS_NONE;
  320. end;
  321. '''' : { string or character }
  322. begin
  323. actasmpattern:='';
  324. repeat
  325. if c = '''' then
  326. begin
  327. c:=current_scanner^.asmgetchar;
  328. if c=newline then
  329. begin
  330. Message(scan_f_string_exceeds_line);
  331. break;
  332. end;
  333. repeat
  334. if c='''' then
  335. begin
  336. c:=current_scanner^.asmgetchar;
  337. if c='''' then
  338. begin
  339. actasmpattern:=actasmpattern+'''';
  340. c:=current_scanner^.asmgetchar;
  341. if c=newline then
  342. begin
  343. Message(scan_f_string_exceeds_line);
  344. break;
  345. end;
  346. end
  347. else
  348. break;
  349. end
  350. else
  351. begin
  352. actasmpattern:=actasmpattern+c;
  353. c:=current_scanner^.asmgetchar;
  354. if c=newline then
  355. begin
  356. Message(scan_f_string_exceeds_line);
  357. break
  358. end;
  359. end;
  360. until false; { end repeat }
  361. end
  362. else
  363. break; { end if }
  364. until false;
  365. actasmtoken:=AS_STRING;
  366. exit;
  367. end;
  368. '"' : { string or character }
  369. begin
  370. actasmpattern:='';
  371. repeat
  372. if c = '"' then
  373. begin
  374. c:=current_scanner^.asmgetchar;
  375. if c=newline then
  376. begin
  377. Message(scan_f_string_exceeds_line);
  378. break;
  379. end;
  380. repeat
  381. if c='"' then
  382. begin
  383. c:=current_scanner^.asmgetchar;
  384. if c='"' then
  385. begin
  386. actasmpattern:=actasmpattern+'"';
  387. c:=current_scanner^.asmgetchar;
  388. if c=newline then
  389. begin
  390. Message(scan_f_string_exceeds_line);
  391. break;
  392. end;
  393. end
  394. else
  395. break;
  396. end
  397. else
  398. begin
  399. actasmpattern:=actasmpattern+c;
  400. c:=current_scanner^.asmgetchar;
  401. if c=newline then
  402. begin
  403. Message(scan_f_string_exceeds_line);
  404. break
  405. end;
  406. end;
  407. until false; { end repeat }
  408. end
  409. else
  410. break; { end if }
  411. until false;
  412. actasmtoken:=AS_STRING;
  413. exit;
  414. end;
  415. '$' :
  416. begin
  417. c:=current_scanner^.asmgetchar;
  418. while c in ['0'..'9','A'..'F','a'..'f'] do
  419. begin
  420. actasmpattern:=actasmpattern + c;
  421. c:=current_scanner^.asmgetchar;
  422. end;
  423. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  424. actasmtoken:=AS_INTNUM;
  425. exit;
  426. end;
  427. ',' :
  428. begin
  429. actasmtoken:=AS_COMMA;
  430. c:=current_scanner^.asmgetchar;
  431. exit;
  432. end;
  433. '[' :
  434. begin
  435. actasmtoken:=AS_LBRACKET;
  436. c:=current_scanner^.asmgetchar;
  437. exit;
  438. end;
  439. ']' :
  440. begin
  441. actasmtoken:=AS_RBRACKET;
  442. c:=current_scanner^.asmgetchar;
  443. exit;
  444. end;
  445. '(' :
  446. begin
  447. actasmtoken:=AS_LPAREN;
  448. c:=current_scanner^.asmgetchar;
  449. exit;
  450. end;
  451. ')' :
  452. begin
  453. actasmtoken:=AS_RPAREN;
  454. c:=current_scanner^.asmgetchar;
  455. exit;
  456. end;
  457. ':' :
  458. begin
  459. actasmtoken:=AS_COLON;
  460. c:=current_scanner^.asmgetchar;
  461. exit;
  462. end;
  463. '.' :
  464. begin
  465. actasmtoken:=AS_DOT;
  466. c:=current_scanner^.asmgetchar;
  467. exit;
  468. end;
  469. '+' :
  470. begin
  471. actasmtoken:=AS_PLUS;
  472. c:=current_scanner^.asmgetchar;
  473. exit;
  474. end;
  475. '-' :
  476. begin
  477. actasmtoken:=AS_MINUS;
  478. c:=current_scanner^.asmgetchar;
  479. exit;
  480. end;
  481. '*' :
  482. begin
  483. actasmtoken:=AS_STAR;
  484. c:=current_scanner^.asmgetchar;
  485. exit;
  486. end;
  487. '/' :
  488. begin
  489. actasmtoken:=AS_SLASH;
  490. c:=current_scanner^.asmgetchar;
  491. exit;
  492. end;
  493. '0'..'9':
  494. begin
  495. actasmpattern:=c;
  496. c:=current_scanner^.asmgetchar;
  497. { Get the possible characters }
  498. while c in ['0'..'9','A'..'F','a'..'f'] do
  499. begin
  500. actasmpattern:=actasmpattern + c;
  501. c:=current_scanner^.asmgetchar;
  502. end;
  503. { Get ending character }
  504. uppervar(actasmpattern);
  505. c:=upcase(c);
  506. { possibly a binary number. }
  507. if (actasmpattern[length(actasmpattern)] = 'B') and (c <> 'H') then
  508. Begin
  509. { Delete the last binary specifier }
  510. delete(actasmpattern,length(actasmpattern),1);
  511. actasmpattern:=tostr(ValBinary(actasmpattern));
  512. actasmtoken:=AS_INTNUM;
  513. exit;
  514. end
  515. else
  516. Begin
  517. case c of
  518. 'O' :
  519. Begin
  520. actasmpattern:=tostr(ValOctal(actasmpattern));
  521. actasmtoken:=AS_INTNUM;
  522. c:=current_scanner^.asmgetchar;
  523. exit;
  524. end;
  525. 'H' :
  526. Begin
  527. actasmpattern:=tostr(ValHexaDecimal(actasmpattern));
  528. actasmtoken:=AS_INTNUM;
  529. c:=current_scanner^.asmgetchar;
  530. exit;
  531. end;
  532. else { must be an integer number }
  533. begin
  534. actasmpattern:=tostr(ValDecimal(actasmpattern));
  535. actasmtoken:=AS_INTNUM;
  536. exit;
  537. end;
  538. end;
  539. end;
  540. end;
  541. ';','{',#13,newline :
  542. begin
  543. c:=current_scanner^.asmgetchar;
  544. firsttoken:=TRUE;
  545. actasmtoken:=AS_SEPARATOR;
  546. exit;
  547. end;
  548. else
  549. Begin
  550. Message(scan_f_illegal_char);
  551. end;
  552. end;
  553. end;
  554. end;
  555. procedure consume(t : tasmtoken);
  556. begin
  557. if t<>actasmtoken then
  558. Message(assem_e_syntax_error);
  559. gettoken;
  560. { if the token must be ignored, then }
  561. { get another token to parse. }
  562. if actasmtoken = AS_NONE then
  563. gettoken;
  564. end;
  565. procedure RecoverConsume(allowcomma:boolean);
  566. begin
  567. While not (actasmtoken in [AS_SEPARATOR,AS_END]) do
  568. begin
  569. if allowcomma and (actasmtoken=AS_COMMA) then
  570. break;
  571. Consume(actasmtoken);
  572. end;
  573. end;
  574. {*****************************************************************************
  575. Parsing Helpers
  576. *****************************************************************************}
  577. Procedure BuildRecordOffsetSize(const expr: string;var offset:longint;var size:longint);
  578. { Description: This routine builds up a record offset after a AS_DOT }
  579. { token is encountered. }
  580. { On entry actasmtoken should be equal to AS_DOT }
  581. var
  582. s : string;
  583. Begin
  584. offset:=0;
  585. size:=0;
  586. s:=expr;
  587. while (actasmtoken=AS_DOT) do
  588. begin
  589. Consume(AS_DOT);
  590. if actasmtoken=AS_ID then
  591. begin
  592. s:=s+'.'+actasmpattern;
  593. Consume(AS_ID);
  594. end
  595. else
  596. begin
  597. Message(assem_e_syntax_error);
  598. RecoverConsume(true);
  599. break;
  600. end;
  601. end;
  602. if not GetRecordOffsetSize(s,offset,size) then
  603. Message(assem_e_syntax_error);
  604. end;
  605. Procedure BuildConstSymbolExpression(needofs:boolean;var value:longint;var asmsym:string);
  606. {*********************************************************************}
  607. { FUNCTION BuildConstExpression(false): longint }
  608. { Description: This routine calculates a constant expression to }
  609. { a given value. The return value is the value calculated from }
  610. { the expression. }
  611. { The following tokens (not strings) are recognized: }
  612. { (,),SHL,SHR,/,*,NOT,OR,XOR,AND,MOD,+/-,numbers,ID to constants. }
  613. {*********************************************************************}
  614. { ENTRY: On entry the token should be any valid expression token. }
  615. { EXIT: On Exit the token points to any token after the closing }
  616. { RBRACKET }
  617. { ERROR RECOVERY: Tries to find COMMA or SEPARATOR token by consuming }
  618. { invalid tokens. }
  619. {*********************************************************************}
  620. var
  621. tempstr,expr,hs : string;
  622. parenlevel,l,k : longint;
  623. errorflag : boolean;
  624. prevtok : tasmtoken;
  625. hl : plabel;
  626. sym : psym;
  627. Begin
  628. { reset }
  629. value:=0;
  630. asmsym:='';
  631. errorflag:=FALSE;
  632. tempstr:='';
  633. expr:='';
  634. inexpression:=TRUE;
  635. parenlevel:=0;
  636. Repeat
  637. Case actasmtoken of
  638. AS_LPAREN:
  639. Begin
  640. Consume(AS_LPAREN);
  641. expr:=expr + '(';
  642. inc(parenlevel);
  643. end;
  644. AS_RPAREN:
  645. Begin
  646. Consume(AS_RPAREN);
  647. expr:=expr + ')';
  648. dec(parenlevel);
  649. end;
  650. AS_SHL:
  651. Begin
  652. Consume(AS_SHL);
  653. expr:=expr + '<';
  654. end;
  655. AS_SHR:
  656. Begin
  657. Consume(AS_SHR);
  658. expr:=expr + '>';
  659. end;
  660. AS_SLASH:
  661. Begin
  662. Consume(AS_SLASH);
  663. expr:=expr + '/';
  664. end;
  665. AS_MOD:
  666. Begin
  667. Consume(AS_MOD);
  668. expr:=expr + '%';
  669. end;
  670. AS_STAR:
  671. Begin
  672. Consume(AS_STAR);
  673. expr:=expr + '*';
  674. end;
  675. AS_PLUS:
  676. Begin
  677. Consume(AS_PLUS);
  678. expr:=expr + '+';
  679. end;
  680. AS_MINUS:
  681. Begin
  682. Consume(AS_MINUS);
  683. expr:=expr + '-';
  684. end;
  685. AS_AND:
  686. Begin
  687. Consume(AS_AND);
  688. expr:=expr + '&';
  689. end;
  690. AS_NOT:
  691. Begin
  692. Consume(AS_NOT);
  693. expr:=expr + '~';
  694. end;
  695. AS_XOR:
  696. Begin
  697. Consume(AS_XOR);
  698. expr:=expr + '^';
  699. end;
  700. AS_OR:
  701. Begin
  702. Consume(AS_OR);
  703. expr:=expr + '|';
  704. end;
  705. AS_INTNUM:
  706. Begin
  707. expr:=expr + actasmpattern;
  708. Consume(AS_INTNUM);
  709. end;
  710. AS_OFFSET:
  711. begin
  712. Consume(AS_OFFSET);
  713. if actasmtoken<>AS_ID then
  714. Comment(V_Error,'assem_e_offset_without_identifier');
  715. end;
  716. AS_ID:
  717. Begin
  718. tempstr:=actasmpattern;
  719. prevtok:=prevasmtoken;
  720. consume(AS_ID);
  721. if actasmtoken=AS_DOT then
  722. begin
  723. BuildRecordOffsetSize(tempstr,l,k);
  724. str(l, tempstr);
  725. expr:=expr + tempstr;
  726. end
  727. else
  728. if SearchIConstant(tempstr,l) then
  729. begin
  730. str(l, tempstr);
  731. expr:=expr + tempstr;
  732. end
  733. else
  734. begin
  735. hs:='';
  736. if SearchLabel(tempstr,hl) then
  737. hs:=lab2str(hl)
  738. else
  739. begin
  740. getsym(tempstr,false);
  741. sym:=srsym;
  742. if assigned(sym) then
  743. begin
  744. if sym^.owner^.symtabletype in [localsymtable,parasymtable] then
  745. Writeln('can''t use local variable or parameters here');
  746. case srsym^.typ of
  747. varsym :
  748. hs:=pvarsym(srsym)^.mangledname;
  749. typedconstsym :
  750. hs:=ptypedconstsym(srsym)^.mangledname;
  751. procsym :
  752. hs:=pprocsym(srsym)^.mangledname;
  753. else
  754. Writeln('Error: wrong sym type');
  755. end;
  756. end
  757. else
  758. Message1(assem_e_unknown_id,tempstr);
  759. end;
  760. { symbol found? }
  761. if hs<>'' then
  762. begin
  763. if needofs and (prevtok<>AS_OFFSET) then
  764. Comment(V_Error,'assem_e_need_offset');
  765. if asmsym='' then
  766. asmsym:=hs
  767. else
  768. Comment(V_Error,'assem_e_cant_have_multiple_relocatable_symbols');
  769. if (expr='') or (expr[length(expr)]='+') then
  770. begin
  771. delete(expr,length(expr),1);
  772. if not(actasmtoken in [AS_MINUS,AS_PLUS,AS_COMMA,AS_SEPARATOR,AS_END]) then
  773. Comment(V_Error,'assem_e_only_add_relocatable_symbol');
  774. end
  775. else
  776. Comment(V_Error,'assem_e_only_add_relocatable_symbol');
  777. end;
  778. end;
  779. end;
  780. AS_RBRACKET,
  781. AS_SEPARATOR,
  782. AS_COMMA:
  783. Begin
  784. break;
  785. end;
  786. else
  787. Begin
  788. { write error only once. }
  789. if not errorflag then
  790. Message(assem_e_invalid_constant_expression);
  791. { consume tokens until we find COMMA or SEPARATOR }
  792. Consume(actasmtoken);
  793. errorflag:=TRUE;
  794. end;
  795. end;
  796. Until false;
  797. { calculate expression }
  798. if not ErrorFlag then
  799. value:=CalculateExpression(expr)
  800. else
  801. value:=0;
  802. { no longer in an expression }
  803. inexpression:=FALSE;
  804. end;
  805. Function BuildConstExpression:longint;
  806. var
  807. l : longint;
  808. hs : string;
  809. begin
  810. BuildConstSymbolExpression(false,l,hs);
  811. if hs<>'' then
  812. Comment(V_Error,'no relocatable symbols allowed');
  813. BuildConstExpression:=l;
  814. end;
  815. Procedure BuildBracketExpression(var Instr: TInstruction);
  816. {*********************************************************************}
  817. { PROCEDURE BuildBracketExpression }
  818. { Description: This routine builds up an expression after a LBRACKET }
  819. { token is encountered. }
  820. { On entry actasmtoken should be equal to AS_LBRACKET. }
  821. { var_prefix : Should be set to true if variable identifier has }
  822. { been defined, such as in ID[ }
  823. {*********************************************************************}
  824. { EXIT CONDITION: On exit the routine should point to either the }
  825. { AS_COMMA or AS_SEPARATOR token. }
  826. {*********************************************************************}
  827. var
  828. l : longint;
  829. hs : string;
  830. code : word;
  831. hreg,
  832. oldbase : tregister;
  833. GotPlus,Negative : boolean;
  834. Begin
  835. Consume(AS_LBRACKET);
  836. initAsmRef(instr,operandnum);
  837. GotPlus:=true;
  838. Negative:=false;
  839. repeat
  840. Case actasmtoken of
  841. AS_ID: { Constant reference expression OR variable reference expression }
  842. Begin
  843. if not GotPlus then
  844. Message(assem_e_invalid_reference_syntax);
  845. if actasmpattern[1] = '@' then
  846. Message(assem_e_local_symbol_not_allowed_as_ref);
  847. if SearchIConstant(actasmpattern,l) then
  848. begin
  849. l:=BuildConstExpression;
  850. if actasmtoken=AS_STAR then
  851. instr.operands[operandnum].ref.scalefactor:=l
  852. else
  853. begin
  854. if negative then
  855. Dec(instr.operands[operandnum].ref.offset,l)
  856. else
  857. Inc(instr.operands[operandnum].ref.offset,l);
  858. end;
  859. end
  860. else
  861. Begin
  862. if instr.operands[operandnum].hasvar then
  863. writeln('can''t have more than one variable in an reference');
  864. if negative then
  865. writeln('can''t substract relocatable variable');
  866. oldbase:=instr.operands[operandnum].ref.base;
  867. instr.operands[operandnum].ref.base:=R_NO;
  868. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  869. Message1(assem_e_unknown_id,actasmpattern);
  870. { is the base register loaded by the var ? }
  871. if (instr.operands[operandnum].ref.base<>R_NO) then
  872. begin
  873. { check if we can move the old base to the index register }
  874. if (instr.operands[operandnum].ref.index<>R_NO) then
  875. writeln('invalid base and index register usage')
  876. else
  877. instr.operands[operandnum].ref.index:=oldbase;
  878. end
  879. else
  880. instr.operands[operandnum].ref.base:=oldbase;
  881. { we can't have a Constant here so add the constant value to the
  882. offset }
  883. if instr.operands[operandnum].operandtype=OPR_CONSTANT then
  884. begin
  885. instr.operands[operandnum].operandtype:=OPR_REFERENCE;
  886. inc(instr.operands[operandnum].ref.offset,instr.operands[operandnum].val);
  887. end;
  888. Consume(AS_ID);
  889. end;
  890. GotPlus:=false;
  891. end;
  892. AS_PLUS :
  893. Begin
  894. Consume(AS_PLUS);
  895. Negative:=false;
  896. GotPlus:=true;
  897. end;
  898. AS_MINUS :
  899. begin
  900. Consume(AS_MINUS);
  901. Negative:=true;
  902. GotPlus:=true;
  903. end;
  904. AS_STAR : { Scaling }
  905. begin
  906. Consume(AS_STAR);
  907. hs:='';
  908. l:=0;
  909. case actasmtoken of
  910. AS_LPAREN :
  911. l:=BuildConstExpression;
  912. AS_INTNUM:
  913. Begin
  914. hs:=actasmpattern;
  915. Consume(AS_INTNUM);
  916. end;
  917. AS_REGISTER :
  918. begin
  919. if instr.operands[operandnum].ref.scalefactor=0 then
  920. Comment(V_Error,'wrong scale factor specified');
  921. end;
  922. else
  923. Message(assem_e_invalid_reference_syntax);
  924. end;
  925. if actasmtoken<>AS_REGISTER then
  926. begin
  927. if hs<>'' then
  928. val(hs,l,code);
  929. instr.operands[operandnum].ref.scalefactor:=l
  930. end;
  931. GotPlus:=false;
  932. end;
  933. AS_REGISTER :
  934. begin
  935. if (not GotPlus) and (actasmtoken<>AS_STAR) then
  936. Message(assem_e_invalid_reference_syntax);
  937. hreg:=actasmregister;
  938. Consume(AS_REGISTER);
  939. { this register will be the index }
  940. if (actasmtoken=AS_STAR) or
  941. (instr.operands[operandnum].ref.base<>R_NO) then
  942. begin
  943. if (instr.operands[operandnum].ref.index<>R_NO) then
  944. writeln('Error: multiple index register usage');
  945. instr.operands[operandnum].ref.index:=hreg;
  946. end
  947. else
  948. instr.operands[operandnum].ref.base:=hreg;
  949. GotPlus:=false;
  950. end;
  951. AS_NOT,
  952. AS_INTNUM,
  953. AS_LPAREN : { Constant reference expression }
  954. begin
  955. if not GotPlus then
  956. Message(assem_e_invalid_reference_syntax);
  957. l:=BuildConstExpression;
  958. if actasmtoken=AS_STAR then
  959. instr.operands[operandnum].ref.scalefactor:=l
  960. else
  961. begin
  962. if negative then
  963. Dec(instr.operands[operandnum].ref.offset,l)
  964. else
  965. Inc(instr.operands[operandnum].ref.offset,l);
  966. end;
  967. GotPlus:=false;
  968. end;
  969. AS_RBRACKET :
  970. begin
  971. if GotPlus then
  972. Message(assem_e_invalid_reference_syntax);
  973. Consume(AS_RBRACKET);
  974. break;
  975. end;
  976. else
  977. Begin
  978. Message(assem_e_invalid_reference_syntax);
  979. RecoverConsume(true);
  980. break;
  981. end;
  982. end;
  983. until false;
  984. end;
  985. Procedure BuildOperand(var instr: TInstruction);
  986. Procedure BuildConstOperand(var instr: TInstruction);
  987. var
  988. l : longint;
  989. tempstr : string;
  990. begin
  991. BuildConstSymbolExpression(true,l,tempstr);
  992. if tempstr<>'' then
  993. begin
  994. instr.operands[operandnum].operandtype:=OPR_SYMBOL;
  995. instr.operands[operandnum].symofs:=l;
  996. instr.operands[operandnum].symbol:=newasmsymbol(tempstr);
  997. end
  998. else
  999. begin
  1000. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1001. instr.operands[operandnum].val:=l;
  1002. end;
  1003. end;
  1004. var
  1005. expr,
  1006. tempstr : string;
  1007. tempreg : tregister;
  1008. lab : Pasmlabel;
  1009. l,
  1010. toffset,
  1011. tsize : longint;
  1012. hl : plabel;
  1013. Begin
  1014. tempstr:='';
  1015. expr:='';
  1016. case actasmtoken of
  1017. AS_OFFSET,
  1018. AS_INTNUM,
  1019. AS_PLUS,
  1020. AS_MINUS,
  1021. AS_NOT,
  1022. AS_LPAREN :
  1023. Begin
  1024. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  1025. Message(assem_e_invalid_operand_type);
  1026. BuildConstOperand(instr);
  1027. end;
  1028. AS_STRING :
  1029. Begin
  1030. if not (instr.operands[operandnum].operandtype in [OPR_NONE]) then
  1031. Message(assem_e_invalid_operand_type);
  1032. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1033. if not PadZero(actasmpattern,4) then
  1034. Message1(assem_e_invalid_string_as_opcode_operand,actasmpattern);
  1035. instr.operands[operandnum].val:=ord(actasmpattern[4]) + ord(actasmpattern[3]) shl 8 +
  1036. Ord(actasmpattern[2]) shl 16 + ord(actasmpattern[1]) shl 24;
  1037. Consume(AS_STRING);
  1038. end;
  1039. AS_ID : { A constant expression, or a Variable ref. }
  1040. Begin
  1041. { Label or Special symbol reference? }
  1042. if actasmpattern[1] = '@' then
  1043. Begin
  1044. if actasmpattern = '@RESULT' then
  1045. Begin
  1046. initAsmRef(instr,operandnum);
  1047. SetUpResult(instr,operandnum);
  1048. end
  1049. else
  1050. if (actasmpattern = '@CODE') or (actasmpattern = '@DATA') then
  1051. Message(assem_w_CODE_and_DATA_not_supported)
  1052. else
  1053. Begin
  1054. delete(actasmpattern,1,1);
  1055. if actasmpattern = '' then
  1056. Message(assem_e_null_label_ref_not_allowed);
  1057. lab:=labellist.search(actasmpattern);
  1058. { check if the label is already defined }
  1059. { if so, we then check if the plabel is }
  1060. { non-nil, if so we add it to instruction }
  1061. if assigned(lab) then
  1062. Begin
  1063. if assigned(lab^.lab) then
  1064. Begin
  1065. instr.operands[operandnum].operandtype:=OPR_LABINSTR;
  1066. instr.operands[operandnum].hl:=lab^.lab;
  1067. instr.labeled:=TRUE;
  1068. end;
  1069. end
  1070. else
  1071. { the label does not exist, create it }
  1072. { emit the opcode, but set that the }
  1073. { label has not been emitted }
  1074. Begin
  1075. getlabel(hl);
  1076. labellist.insert(actasmpattern,hl,FALSE);
  1077. instr.operands[operandnum].operandtype:=OPR_LABINSTR;
  1078. instr.operands[operandnum].hl:=hl;
  1079. instr.labeled:=TRUE;
  1080. end;
  1081. end;
  1082. Consume(AS_ID);
  1083. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1084. Message(assem_e_syntax_error);
  1085. end
  1086. else
  1087. { support result for delphi modes }
  1088. if (m_objpas in aktmodeswitches) and (actasmpattern='RESULT') then
  1089. begin
  1090. initAsmRef(instr,operandnum);
  1091. SetUpResult(instr,operandnum);
  1092. Consume(AS_ID);
  1093. end
  1094. { probably a variable or normal expression }
  1095. { or a procedure (such as in CALL ID) }
  1096. else
  1097. Begin
  1098. { is it a constant ? }
  1099. if SearchIConstant(actasmpattern,l) then
  1100. Begin
  1101. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_CONSTANT]) then
  1102. Message(assem_e_invalid_operand_type);
  1103. BuildConstOperand(instr);
  1104. end
  1105. else
  1106. { is it a label variable ? }
  1107. if SearchLabel(actasmpattern,hl) then
  1108. Begin
  1109. instr.operands[operandnum].operandtype:=OPR_LABINSTR;
  1110. instr.operands[operandnum].hl:=hl;
  1111. instr.labeled:=TRUE;
  1112. Consume(AS_ID);
  1113. if not (actasmtoken in [AS_SEPARATOR,AS_COMMA]) then
  1114. Message(assem_e_syntax_error);
  1115. end
  1116. else
  1117. { is it a normal variable ? }
  1118. Begin
  1119. initAsmRef(instr,operandnum);
  1120. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  1121. Begin
  1122. { not a variable.. }
  1123. { check special variables.. }
  1124. if actasmpattern = 'SELF' then
  1125. Begin
  1126. if assigned(procinfo._class) then
  1127. Begin
  1128. instr.operands[operandnum].ref.offset:=procinfo.ESI_offset;
  1129. instr.operands[operandnum].ref.base:=procinfo.framepointer;
  1130. end
  1131. else
  1132. Message(assem_e_cannot_use_SELF_outside_a_method);
  1133. end
  1134. else
  1135. Message1(assem_e_unknown_id,actasmpattern);
  1136. end;
  1137. l:=0;
  1138. expr:=actasmpattern;
  1139. Consume(AS_ID);
  1140. if actasmtoken=AS_LBRACKET then
  1141. begin
  1142. instr.operands[operandnum].operandtype:=OPR_REFERENCE;
  1143. reset_reference(Instr.Operands[OperandNum].Ref);
  1144. BuildBracketExpression(instr);
  1145. end;
  1146. if actasmtoken=AS_DOT then
  1147. begin
  1148. if expr='' then
  1149. writeln('No type specified!')
  1150. else
  1151. begin
  1152. BuildRecordOffsetSize(expr,toffset,tsize);
  1153. inc(l,toffset);
  1154. SetOperandSize(instr,operandnum,tsize);
  1155. end;
  1156. end;
  1157. if actasmtoken in [AS_PLUS,AS_MINUS] then
  1158. inc(l,BuildConstExpression);
  1159. if instr.operands[operandnum].operandtype=OPR_REFERENCE then
  1160. inc(instr.operands[operandnum].ref.offset,l)
  1161. else
  1162. inc(instr.operands[operandnum].val,l);
  1163. end;
  1164. end;
  1165. end;
  1166. AS_REGISTER : { Register, a variable reference or a constant reference }
  1167. Begin
  1168. { save the type of register used. }
  1169. tempreg:=actasmregister;
  1170. Consume(AS_REGISTER);
  1171. if actasmtoken = AS_COLON then
  1172. Begin
  1173. Consume(AS_COLON);
  1174. if actasmtoken <> AS_LBRACKET then
  1175. Message(assem_e_syn_start_with_bracket)
  1176. else
  1177. Begin
  1178. initAsmRef(instr,operandnum);
  1179. instr.operands[operandnum].ref.segment:=tempreg;
  1180. BuildBracketExpression(instr);
  1181. end;
  1182. end
  1183. else
  1184. { Simple register }
  1185. begin
  1186. if not (instr.operands[operandnum].operandtype in [OPR_NONE,OPR_REGISTER]) then
  1187. Message(assem_e_invalid_operand_type);
  1188. instr.operands[operandnum].operandtype:=OPR_REGISTER;
  1189. instr.operands[operandnum].reg:=tempreg;
  1190. instr.operands[operandnum].size:=reg_2_opsize[instr.operands[operandnum].reg];
  1191. end;
  1192. end;
  1193. AS_LBRACKET: { a variable reference, register ref. or a constant reference }
  1194. Begin
  1195. BuildBracketExpression(instr);
  1196. end;
  1197. AS_SEG :
  1198. Begin
  1199. Comment(V_Error,'assem_e_seg_not_supported');
  1200. Consume(actasmtoken);
  1201. end;
  1202. AS_SEPARATOR,
  1203. AS_COMMA: ;
  1204. else
  1205. Message(assem_e_syn_opcode_operand);
  1206. end;
  1207. if not(actasmtoken in [AS_END,AS_SEPARATOR,AS_COMMA]) then
  1208. begin
  1209. Message(assem_e_syntax_error);
  1210. RecoverConsume(true);
  1211. end;
  1212. end;
  1213. Procedure BuildConstant(maxvalue: longint);
  1214. {*********************************************************************}
  1215. { PROCEDURE BuildConstant }
  1216. { Description: This routine takes care of parsing a DB,DD,or DW }
  1217. { line and adding those to the assembler node. Expressions, range- }
  1218. { checking are fullly taken care of. }
  1219. { maxvalue: $ff -> indicates that this is a DB node. }
  1220. { $ffff -> indicates that this is a DW node. }
  1221. { $ffffffff -> indicates that this is a DD node. }
  1222. {*********************************************************************}
  1223. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  1224. {*********************************************************************}
  1225. var
  1226. strlength: byte;
  1227. asmsym,
  1228. expr: string;
  1229. value : longint;
  1230. Begin
  1231. strlength:=0; { assume it is a DB }
  1232. Repeat
  1233. Case actasmtoken of
  1234. AS_STRING:
  1235. Begin
  1236. if maxvalue = $ffff then
  1237. strlength:=2
  1238. else
  1239. if maxvalue = $ffffffff then
  1240. strlength:=4;
  1241. { DD and DW cases }
  1242. if strlength <> 0 then
  1243. Begin
  1244. if Not PadZero(actasmpattern,strlength) then
  1245. Message(scan_f_string_exceeds_line);
  1246. end;
  1247. expr:=actasmpattern;
  1248. Consume(AS_STRING);
  1249. Case actasmtoken of
  1250. AS_COMMA:
  1251. Consume(AS_COMMA);
  1252. AS_SEPARATOR: ;
  1253. else
  1254. Message(assem_e_invalid_string_expression);
  1255. end;
  1256. ConcatString(curlist,expr);
  1257. end;
  1258. AS_PLUS,
  1259. AS_MINUS,
  1260. AS_LPAREN,
  1261. AS_NOT,
  1262. AS_INTNUM,
  1263. AS_ID :
  1264. Begin
  1265. BuildConstSymbolExpression(false,value,asmsym);
  1266. if asmsym<>'' then
  1267. begin
  1268. if maxvalue<>$ffffffff then
  1269. Comment(V_Warning,'32bit constant created for address');
  1270. ConcatConstSymbol(curlist,asmsym,value)
  1271. end
  1272. else
  1273. ConcatConstant(curlist,value,maxvalue);
  1274. end;
  1275. AS_COMMA:
  1276. Consume(AS_COMMA);
  1277. AS_SEPARATOR:
  1278. break;
  1279. else
  1280. Message(assem_f_internal_error_in_buildconstant);
  1281. end;
  1282. Until false;
  1283. end;
  1284. Procedure BuildOpCode;
  1285. {*********************************************************************}
  1286. { PROCEDURE BuildOpcode; }
  1287. { Description: Parses the intel opcode and operands, and writes it }
  1288. { in the TInstruction object. }
  1289. {*********************************************************************}
  1290. { EXIT CONDITION: On exit the routine should point to AS_SEPARATOR. }
  1291. { On ENTRY: Token should point to AS_OPCODE }
  1292. {*********************************************************************}
  1293. var
  1294. PrefixOp,OverrideOp: tasmop;
  1295. expr : string;
  1296. size : topsize;
  1297. Begin
  1298. expr:='';
  1299. PrefixOp:=A_None;
  1300. OverrideOp:=A_None;
  1301. { prefix seg opcode / prefix opcode }
  1302. repeat
  1303. if is_prefix(actopcode) then
  1304. begin
  1305. if (PrefixOp<>A_None) or (OverrideOp<>A_None) then
  1306. Message(assem_w_repeat_prefix_and_seg_override);
  1307. PrefixOp:=ActOpcode;
  1308. instr.opcode:=ActOpcode;
  1309. instr.condition:=ActCondition;
  1310. instr.opsize:=ActOpsize;
  1311. ConcatInstruction(curlist,instr);
  1312. Consume(AS_OPCODE);
  1313. end
  1314. else
  1315. if is_override(actopcode) then
  1316. begin
  1317. if (PrefixOp<>A_None) or (OverrideOp<>A_None) then
  1318. Message(assem_w_repeat_prefix_and_seg_override);
  1319. OverrideOp:=ActOpcode;
  1320. instr.opcode:=ActOpcode;
  1321. instr.condition:=ActCondition;
  1322. instr.opsize:=ActOpsize;
  1323. ConcatInstruction(curlist,instr);
  1324. Consume(AS_OPCODE);
  1325. end
  1326. else
  1327. break;
  1328. until (actasmtoken<>AS_OPCODE);
  1329. { opcode }
  1330. if (actasmtoken <> AS_OPCODE) then
  1331. Begin
  1332. Message(assem_e_invalid_or_missing_opcode);
  1333. RecoverConsume(false);
  1334. exit;
  1335. end;
  1336. { Fill the instr object with the current state }
  1337. instr.Opcode:=ActOpcode;
  1338. instr.condition:=ActCondition;
  1339. instr.opsize:=ActOpsize;
  1340. { Valid combination of prefix/override and instruction ? }
  1341. if (prefixop<>A_NONE) and (NOT CheckPrefix(PrefixOp,actopcode)) then
  1342. Message1(assem_e_invalid_prefix_and_opcode,actasmpattern);
  1343. if (overrideop<>A_NONE) and (NOT CheckOverride(OverrideOp,ActOpcode)) then
  1344. Message1(assem_e_invalid_override_and_opcode,actasmpattern);
  1345. { We are reading operands, so opcode will be an AS_ID }
  1346. operandnum:=1;
  1347. Consume(AS_OPCODE);
  1348. { Zero operand opcode ? }
  1349. if actasmtoken in [AS_SEPARATOR,AS_END] then
  1350. begin
  1351. operandnum:=0;
  1352. exit;
  1353. end;
  1354. { Read Operands }
  1355. repeat
  1356. case actasmtoken of
  1357. { End of asm operands for this opcode }
  1358. AS_END,
  1359. AS_SEPARATOR :
  1360. break;
  1361. { Operand delimiter }
  1362. AS_COMMA :
  1363. Begin
  1364. if operandnum > MaxOperands then
  1365. Message(assem_e_too_many_operands)
  1366. else
  1367. Inc(operandnum);
  1368. Consume(AS_COMMA);
  1369. end;
  1370. { Typecast, Constant Expression, Type Specifier }
  1371. AS_DWORD,
  1372. AS_BYTE,
  1373. AS_WORD,
  1374. AS_TBYTE,
  1375. AS_QWORD :
  1376. Begin
  1377. { load the size in a temp variable, so it can be set when the
  1378. operand is read }
  1379. Case actasmtoken of
  1380. AS_DWORD : size:=S_L;
  1381. AS_WORD : size:=S_W;
  1382. AS_BYTE : size:=S_B;
  1383. AS_QWORD : size:=S_IQ;
  1384. AS_TBYTE : size:=S_FX;
  1385. end;
  1386. Consume(actasmtoken);
  1387. Case actasmtoken of
  1388. { Reference }
  1389. AS_PTR :
  1390. Begin
  1391. Consume(AS_PTR);
  1392. BuildOperand(instr);
  1393. end;
  1394. { Possibly a typecast or a constant }
  1395. { expression. }
  1396. AS_LPAREN :
  1397. Begin
  1398. if actasmtoken = AS_ID then
  1399. Begin
  1400. { Case vartype of }
  1401. { LOCAL: Replace by offset and }
  1402. { BP in treference. }
  1403. { GLOBAL: Replace by mangledname}
  1404. { in symbol of treference }
  1405. { Check if next token = RPAREN }
  1406. { otherwise syntax error. }
  1407. initAsmRef(instr,operandnum);
  1408. if not CreateVarInstr(instr,actasmpattern,operandnum) then
  1409. Message1(assem_e_unknown_id,actasmpattern);
  1410. end
  1411. else
  1412. begin
  1413. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1414. instr.operands[operandnum].val:=BuildConstExpression;
  1415. end;
  1416. end;
  1417. else
  1418. BuildOperand(instr);
  1419. end;
  1420. { now set the size which was specified by the override }
  1421. instr.operands[operandnum].size:=size;
  1422. end;
  1423. { Type specifier }
  1424. AS_NEAR,
  1425. AS_FAR :
  1426. Begin
  1427. if actasmtoken = AS_NEAR then
  1428. Message(assem_w_near_ignored)
  1429. else
  1430. Message(assem_w_far_ignored);
  1431. Consume(actasmtoken);
  1432. if actasmtoken = AS_PTR then
  1433. begin
  1434. initAsmRef(instr,operandnum);
  1435. Consume(AS_PTR);
  1436. end;
  1437. BuildOperand(instr);
  1438. end;
  1439. { Constant expression }
  1440. AS_LPAREN :
  1441. Begin
  1442. instr.operands[operandnum].operandtype:=OPR_CONSTANT;
  1443. instr.operands[operandnum].val:=BuildConstExpression;
  1444. end;
  1445. else
  1446. BuildOperand(instr);
  1447. end; { end case }
  1448. until false;
  1449. end;
  1450. Function Assemble: Ptree;
  1451. {*********************************************************************}
  1452. { PROCEDURE Assemble; }
  1453. { Description: Parses the intel assembler syntax, parsing is done }
  1454. { according to the rules in the Turbo Pascal manual. }
  1455. {*********************************************************************}
  1456. Var
  1457. hl : plabel;
  1458. labelptr : pasmlabel;
  1459. Begin
  1460. Message(assem_d_start_intel);
  1461. inexpression:=FALSE;
  1462. firsttoken:=TRUE;
  1463. operandnum:=0;
  1464. if assigned(procinfo.retdef) and
  1465. (is_fpu(procinfo.retdef) or
  1466. ret_in_acc(procinfo.retdef)) then
  1467. procinfo.funcret_is_valid:=true;
  1468. { sets up all opcode and register tables in uppercase }
  1469. if not _asmsorted then
  1470. Begin
  1471. SetupTables;
  1472. _asmsorted:=TRUE;
  1473. end;
  1474. curlist:=new(paasmoutput,init);
  1475. { setup label linked list }
  1476. labellist.init;
  1477. c:=current_scanner^.asmgetchar;
  1478. gettoken;
  1479. repeat
  1480. case actasmtoken of
  1481. AS_LLABEL :
  1482. Begin
  1483. labelptr:=labellist.search(actasmpattern);
  1484. if not assigned(labelptr) then
  1485. Begin
  1486. getlabel(hl);
  1487. labellist.insert(actasmpattern,hl,TRUE);
  1488. ConcatLabel(curlist,hl);
  1489. end
  1490. else
  1491. { the label has already been inserted into the }
  1492. { label list, either as an intruction label (in }
  1493. { this case it has not been emitted), or as a }
  1494. { duplicate local symbol (in this case it has }
  1495. { already been emitted). }
  1496. Begin
  1497. if labelptr^.emitted then
  1498. Message1(assem_e_dup_local_sym,'@'+labelptr^.name^)
  1499. else
  1500. Begin
  1501. if assigned(labelptr^.lab) then
  1502. ConcatLabel(curlist,labelptr^.lab);
  1503. labelptr^.emitted:=TRUE;
  1504. end;
  1505. end;
  1506. Consume(AS_LLABEL);
  1507. end;
  1508. AS_LABEL :
  1509. Begin
  1510. if SearchLabel(actasmpattern,hl) then
  1511. ConcatLabel(curlist,hl)
  1512. else
  1513. Message1(assem_e_unknown_label_identifer,actasmpattern);
  1514. Consume(AS_LABEL);
  1515. end;
  1516. AS_DW :
  1517. Begin
  1518. inexpression:=true;
  1519. Consume(AS_DW);
  1520. BuildConstant($ffff);
  1521. inexpression:=false;
  1522. end;
  1523. AS_DB :
  1524. Begin
  1525. inexpression:=true;
  1526. Consume(AS_DB);
  1527. BuildConstant($ff);
  1528. inexpression:=false;
  1529. end;
  1530. AS_DD :
  1531. Begin
  1532. inexpression:=true;
  1533. Consume(AS_DD);
  1534. BuildConstant($ffffffff);
  1535. inexpression:=false;
  1536. end;
  1537. AS_OPCODE :
  1538. Begin
  1539. instr.init;
  1540. BuildOpcode;
  1541. instr.ops:=operandnum;
  1542. { We need AT&T style operands }
  1543. SwapOperands(instr);
  1544. AddReferenceSizes(instr);
  1545. SetInstructionOpsize(instr);
  1546. CheckOperandSizes(instr);
  1547. ConcatInstruction(curlist,instr);
  1548. instr.done;
  1549. operandnum:=0;
  1550. end;
  1551. AS_SEPARATOR :
  1552. Begin
  1553. Consume(AS_SEPARATOR);
  1554. { let us go back to the first operand }
  1555. operandnum:=0;
  1556. end;
  1557. AS_END :
  1558. break; { end assembly block }
  1559. else
  1560. Begin
  1561. Message(assem_e_assemble_node_syntax_error);
  1562. { error recovery }
  1563. Consume(actasmtoken);
  1564. end;
  1565. end; { end case }
  1566. until false;
  1567. { check if there were undefined symbols. }
  1568. { if so, then list each of those undefined }
  1569. { labels. }
  1570. if assigned(labellist.First) then
  1571. Begin
  1572. labelptr:=labellist.First;
  1573. if labellist.First <> nil then
  1574. Begin
  1575. { first label }
  1576. if not labelptr^.emitted then
  1577. Message1(assem_e_unknown_local_sym,'@'+labelptr^.name^);
  1578. { other labels ... }
  1579. While (labelptr^.Next <> nil) do
  1580. Begin
  1581. labelptr:=labelptr^.Next;
  1582. if not labelptr^.emitted then
  1583. Message1(assem_e_unknown_local_sym,'@'+labelptr^.name^);
  1584. end;
  1585. end;
  1586. end;
  1587. assemble:=genasmnode(curlist);
  1588. labellist.done;
  1589. Message(assem_d_finish_intel);
  1590. end;
  1591. {*****************************************************************************
  1592. Initialize
  1593. *****************************************************************************}
  1594. var
  1595. old_exit : pointer;
  1596. procedure ra386int_exit;{$ifndef FPC}far;{$endif}
  1597. begin
  1598. if assigned(iasmops) then
  1599. dispose(iasmops);
  1600. if assigned(iasmregs) then
  1601. dispose(iasmregs);
  1602. exitproc:=old_exit;
  1603. end;
  1604. begin
  1605. old_exit:=exitproc;
  1606. exitproc:=@ra386int_exit;
  1607. end.
  1608. {
  1609. $Log$
  1610. Revision 1.31 1999-05-01 13:48:41 peter
  1611. * merged nasm compiler
  1612. Revision 1.6 1999/04/26 23:26:18 peter
  1613. * redesigned record offset parsing to support nested records
  1614. * normal compiler uses the redesigned createvarinstr()
  1615. Revision 1.5 1999/04/20 11:01:24 peter
  1616. * better tokenpos info
  1617. Revision 1.4 1999/04/14 09:07:46 peter
  1618. * asm reader improvements
  1619. Revision 1.3 1999/03/06 17:24:27 peter
  1620. * rewritten intel parser a lot, especially reference reading
  1621. * size checking added for asm parsers
  1622. Revision 1.2 1999/03/02 02:56:31 peter
  1623. + stabs support for binary writers
  1624. * more fixes and missing updates from the previous commit :(
  1625. Revision 1.1 1999/03/01 15:46:26 peter
  1626. * ag386bin finally make cycles correct
  1627. * prefixes are now also normal opcodes
  1628. }