ra386int.pas 48 KB

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