rautils.pas 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  1. {
  2. $Id$
  3. Copyright (c) 1998-2000 by Carl Eric Codere and Peter Vreman
  4. This unit implements some support routines for assembler parsing
  5. independent of the processor
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. **********************************************************************}
  18. Unit RAUtils;
  19. Interface
  20. Uses
  21. strings,
  22. cobjects,
  23. globtype,systems,verbose,globals,files,
  24. symconst,symtable,aasm,cpubase,cpuasm
  25. {$ifdef NEWCG}
  26. ,cgbase
  27. {$else}
  28. ,hcodegen
  29. {$endif}
  30. ;
  31. Const
  32. RPNMax = 10; { I think you only need 4, but just to be safe }
  33. OpMax = 25;
  34. maxoperands = 3; { Maximum operands for assembler instructions }
  35. {---------------------------------------------------------------------
  36. Local Label Management
  37. ---------------------------------------------------------------------}
  38. Type
  39. { Each local label has this structure associated with it }
  40. PLocalLabel = ^TLocalLabel;
  41. TLocalLabel = object(TNamedIndexObject)
  42. Emitted : boolean;
  43. constructor Init(const n:string);
  44. function Getpasmlabel:pasmlabel;
  45. private
  46. lab : pasmlabel;
  47. end;
  48. PLocalLabelList = ^TLocalLabelList;
  49. TLocalLabelList = Object(TDictionary)
  50. procedure CheckEmitted;
  51. end;
  52. var
  53. LocalLabelList : PLocalLabelList;
  54. function CreateLocalLabel(const s: string; var hl: pasmlabel; emit:boolean):boolean;
  55. Function SearchLabel(const s: string; var hl: pasmlabel;emit:boolean): boolean;
  56. {---------------------------------------------------------------------
  57. Instruction management
  58. ---------------------------------------------------------------------}
  59. type
  60. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,OPR_REFERENCE,OPR_REGISTER);
  61. TOprRec = record
  62. case typ:TOprType of
  63. OPR_NONE : ();
  64. OPR_CONSTANT : (val:longint);
  65. OPR_SYMBOL : (symbol:PAsmSymbol;symofs:longint);
  66. OPR_REFERENCE : (ref:treference);
  67. OPR_REGISTER : (reg:tregister);
  68. end;
  69. POperand = ^TOperand;
  70. TOperand = object
  71. size : topsize;
  72. hasvar : boolean; { if the operand is loaded with a variable }
  73. opr : TOprRec;
  74. constructor init;
  75. destructor done;virtual;
  76. Procedure BuildOperand;virtual;
  77. Procedure SetSize(_size:longint);
  78. Function SetupResult:boolean;
  79. Function SetupSelf:boolean;
  80. Function SetupOldEBP:boolean;
  81. Function SetupVar(const hs:string;GetOffset : boolean): Boolean;
  82. Function SetupDirectVar(const hs:string): Boolean;
  83. Procedure InitRef;
  84. end;
  85. PInstruction = ^TInstruction;
  86. TInstruction = object
  87. opcode : tasmop;
  88. opsize : topsize;
  89. condition : tasmcond;
  90. ops : byte;
  91. operands : array[1..maxoperands] of POperand;
  92. constructor init;
  93. destructor done;virtual;
  94. Procedure InitOperands;virtual;
  95. Procedure BuildOpcode;virtual;
  96. procedure ConcatInstruction(p:PAasmoutput);virtual;
  97. Procedure SwapOperands;
  98. end;
  99. {---------------------------------------------------------------------}
  100. { Expression parser types }
  101. {---------------------------------------------------------------------}
  102. TExprOperator = record
  103. ch: char; { operator }
  104. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  105. end;
  106. String15 = String[15];
  107. {**********************************************************************}
  108. { The following operators are supported: }
  109. { '+' : addition }
  110. { '-' : subtraction }
  111. { '*' : multiplication }
  112. { '/' : modulo division }
  113. { '^' : exclusive or }
  114. { '<' : shift left }
  115. { '>' : shift right }
  116. { '&' : bitwise and }
  117. { '|' : bitwise or }
  118. { '~' : bitwise complement }
  119. { '%' : modulo division }
  120. { nnn: longint numbers }
  121. { ( and ) parenthesis }
  122. {**********************************************************************}
  123. TExprParse = Object
  124. public
  125. Constructor Init;
  126. Destructor Done;
  127. Function Evaluate(Expr: String): longint;
  128. Function Priority(_Operator: Char): Integer; virtual;
  129. private
  130. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  131. RPNTop : Integer;
  132. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  133. OpTop : Integer;
  134. Procedure RPNPush(Num: Longint);
  135. Function RPNPop: Longint;
  136. Procedure RPNCalc(token: String15; prefix: boolean);
  137. Procedure OpPush(_Operator: char; prefix: boolean);
  138. { In reality returns TExprOperaotr }
  139. Procedure OpPop(var _Operator:TExprOperator);
  140. end;
  141. { Evaluate an expression string to a longint }
  142. Function CalculateExpression(const expression: string): longint;
  143. {---------------------------------------------------------------------}
  144. { String routines }
  145. {---------------------------------------------------------------------}
  146. Function ValDecimal(const S:String):longint;
  147. Function ValOctal(const S:String):longint;
  148. Function ValBinary(const S:String):longint;
  149. Function ValHexaDecimal(const S:String):longint;
  150. Function PadZero(Var s: String; n: byte): Boolean;
  151. Function EscapeToPascal(const s:string): string;
  152. {---------------------------------------------------------------------
  153. Symbol helper routines
  154. ---------------------------------------------------------------------}
  155. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  156. Function SearchRecordType(const s:string): boolean;
  157. Function SearchIConstant(const s:string; var l:longint): boolean;
  158. {---------------------------------------------------------------------
  159. Instruction generation routines
  160. ---------------------------------------------------------------------}
  161. Procedure ConcatPasString(p : paasmoutput;s:string);
  162. Procedure ConcatDirect(p : paasmoutput;s:string);
  163. Procedure ConcatLabel(p: paasmoutput;var l : pasmlabel);
  164. Procedure ConcatConstant(p : paasmoutput;value: longint; maxvalue: longint);
  165. Procedure ConcatConstSymbol(p : paasmoutput;const sym:string;l:longint);
  166. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  167. Procedure ConcatString(p : paasmoutput;s:string);
  168. procedure ConcatAlign(p:paasmoutput;l:longint);
  169. Procedure ConcatPublic(p:paasmoutput;const s : string);
  170. Procedure ConcatLocal(p:paasmoutput;const s : string);
  171. Procedure ConcatGlobalBss(const s : string;size : longint);
  172. Procedure ConcatLocalBss(const s : string;size : longint);
  173. Implementation
  174. {*************************************************************************
  175. TExprParse
  176. *************************************************************************}
  177. Constructor TExprParse.Init;
  178. Begin
  179. end;
  180. Procedure TExprParse.RPNPush(Num : longint);
  181. { Add an operand to the top of the RPN stack }
  182. begin
  183. if RPNTop < RPNMax then
  184. begin
  185. Inc(RPNTop);
  186. RPNStack[RPNTop]:=Num;
  187. end
  188. else
  189. Message(asmr_e_ev_stack_overflow);
  190. end;
  191. Function TExprParse.RPNPop : longint;
  192. { Get the operand at the top of the RPN stack }
  193. begin
  194. if RPNTop > 0 then
  195. begin
  196. RPNPop:=RPNStack[RPNTop];
  197. Dec(RPNTop);
  198. end
  199. else
  200. Message(asmr_e_ev_stack_underflow);
  201. end;
  202. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  203. Var
  204. Temp : longint;
  205. LocalError : Integer;
  206. begin
  207. { Handle operators }
  208. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  209. Case Token[1] of
  210. '+' :
  211. Begin
  212. if not prefix then
  213. RPNPush(RPNPop + RPNPop);
  214. end;
  215. '-' :
  216. Begin
  217. if prefix then
  218. RPNPush(-(RPNPop))
  219. else
  220. RPNPush(RPNPop - RPNPop);
  221. end;
  222. '*' : RPNPush(RPNPop * RPNPop);
  223. '&' : RPNPush(RPNPop AND RPNPop);
  224. '|' : RPNPush(RPNPop OR RPNPop);
  225. '~' : RPNPush(NOT RPNPop);
  226. '<' : RPNPush(RPNPop SHL RPNPop);
  227. '>' : RPNPush(RPNPop SHR RPNPop);
  228. '%' :
  229. begin
  230. Temp:=RPNPop;
  231. if Temp <> 0 then
  232. RPNPush(RPNPop mod Temp)
  233. else
  234. Message(asmr_e_ev_zero_divide);
  235. end;
  236. '^' : RPNPush(RPNPop XOR RPNPop);
  237. '/' :
  238. begin
  239. Temp:=RPNPop;
  240. if Temp <> 0 then
  241. RPNPush(RPNPop div Temp)
  242. else
  243. Message(asmr_e_ev_zero_divide);
  244. end;
  245. end
  246. else
  247. begin
  248. { Convert String to number and add to stack }
  249. if token='-2147483648' then
  250. begin
  251. temp:=$80000000;
  252. localerror:=0;
  253. end
  254. else
  255. Val(Token, Temp, LocalError);
  256. if LocalError = 0 then
  257. RPNPush(Temp)
  258. else
  259. Message(asmr_e_ev_invalid_number);
  260. end;
  261. end;
  262. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  263. { Add an operator onto top of the stack }
  264. begin
  265. if OpTop < OpMax then
  266. begin
  267. Inc(OpTop);
  268. OpStack[OpTop].ch:=_Operator;
  269. OpStack[OpTop].is_prefix:=prefix;
  270. end
  271. else
  272. Message(asmr_e_ev_stack_overflow);
  273. end;
  274. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  275. { Get operator at the top of the stack }
  276. begin
  277. if OpTop > 0 then
  278. begin
  279. _Operator:=OpStack[OpTop];
  280. Dec(OpTop);
  281. end
  282. else
  283. Message(asmr_e_ev_stack_underflow);
  284. end;
  285. Function TExprParse.Priority(_Operator : Char) : Integer;
  286. { Return priority of operator }
  287. { The greater the priority, the higher the precedence }
  288. begin
  289. Case _Operator OF
  290. '(' :
  291. Priority:=0;
  292. '+', '-' :
  293. Priority:=1;
  294. '*', '/','%','<','>' :
  295. Priority:=2;
  296. '|','&','^','~' :
  297. Priority:=0;
  298. else
  299. Message(asmr_e_ev_invalid_op);
  300. end;
  301. end;
  302. Function TExprParse.Evaluate(Expr : String):longint;
  303. Var
  304. I : Integer;
  305. Token : String15;
  306. opr : TExprOperator;
  307. begin
  308. Evaluate:=0;
  309. { Reset stacks }
  310. OpTop :=0;
  311. RPNTop:=0;
  312. Token :='';
  313. { nothing to do ? }
  314. if Expr='' then
  315. exit;
  316. For I:=1 to Length(Expr) DO
  317. begin
  318. if Expr[I] in ['0'..'9'] then
  319. begin { Build multi-digit numbers }
  320. Token:=Token + Expr[I];
  321. if I = Length(Expr) then { Send last one to calculator }
  322. RPNCalc(Token,false);
  323. end
  324. else
  325. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  326. begin
  327. if Token <> '' then
  328. begin { Send last built number to calc. }
  329. RPNCalc(Token,false);
  330. Token:='';
  331. end;
  332. Case Expr[I] OF
  333. '(' : OpPush('(',false);
  334. ')' : begin
  335. While OpStack[OpTop].ch <> '(' DO
  336. Begin
  337. OpPop(opr);
  338. RPNCalc(opr.ch,opr.is_prefix);
  339. end;
  340. OpPop(opr); { Pop off and ignore the '(' }
  341. end;
  342. '+','-','~' : Begin
  343. { workaround for -2147483648 }
  344. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  345. begin
  346. token:='-';
  347. expr[i]:='+';
  348. end;
  349. { if start of expression then surely a prefix }
  350. { or if previous char was also an operator }
  351. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  352. OpPush(Expr[I],true)
  353. else
  354. Begin
  355. { Evaluate all higher priority operators }
  356. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  357. Begin
  358. OpPop(opr);
  359. RPNCalc(opr.ch,opr.is_prefix);
  360. end;
  361. OpPush(Expr[I],false);
  362. End;
  363. end;
  364. '*', '/',
  365. '^','|','&',
  366. '%','<','>' : begin
  367. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  368. Begin
  369. OpPop(opr);
  370. RPNCalc(opr.ch,opr.is_prefix);
  371. end;
  372. OpPush(Expr[I],false);
  373. end;
  374. end; { Case }
  375. end
  376. else
  377. Message(asmr_e_ev_invalid_op); { Handle bad input error }
  378. end;
  379. { Pop off the remaining operators }
  380. While OpTop > 0 do
  381. Begin
  382. OpPop(opr);
  383. RPNCalc(opr.ch,opr.is_prefix);
  384. end;
  385. { The result is stored on the top of the stack }
  386. Evaluate:=RPNPop;
  387. end;
  388. Destructor TExprParse.Done;
  389. Begin
  390. end;
  391. Function CalculateExpression(const expression: string): longint;
  392. var
  393. expr: TExprParse;
  394. Begin
  395. expr.Init;
  396. CalculateExpression:=expr.Evaluate(expression);
  397. expr.Done;
  398. end;
  399. {*************************************************************************}
  400. { String conversions/utils }
  401. {*************************************************************************}
  402. Function EscapeToPascal(const s:string): string;
  403. { converts a C styled string - which contains escape }
  404. { characters to a pascal style string. }
  405. var
  406. i,len : longint;
  407. hs : string;
  408. temp : string;
  409. c : char;
  410. Begin
  411. hs:='';
  412. len:=0;
  413. i:=0;
  414. while (i<length(s)) and (len<255) do
  415. begin
  416. Inc(i);
  417. if (s[i]='\') and (i<length(s)) then
  418. Begin
  419. inc(i);
  420. case s[i] of
  421. '\' :
  422. c:='\';
  423. 'b':
  424. c:=#8;
  425. 'f':
  426. c:=#12;
  427. 'n':
  428. c:=#10;
  429. 'r':
  430. c:=#13;
  431. 't':
  432. c:=#9;
  433. '"':
  434. c:='"';
  435. '0'..'7':
  436. Begin
  437. temp:=s[i];
  438. temp:=temp+s[i+1];
  439. temp:=temp+s[i+2];
  440. inc(i,2);
  441. c:=chr(ValOctal(temp));
  442. end;
  443. 'x':
  444. Begin
  445. temp:=s[i+1];
  446. temp:=temp+s[i+2];
  447. inc(i,2);
  448. c:=chr(ValHexaDecimal(temp));
  449. end;
  450. else
  451. Begin
  452. Message1(asmr_e_escape_seq_ignored,s[i]);
  453. c:=s[i];
  454. end;
  455. end;
  456. end
  457. else
  458. c:=s[i];
  459. inc(len);
  460. hs[len]:=c;
  461. end;
  462. hs[0]:=chr(len);
  463. EscapeToPascal:=hs;
  464. end;
  465. Function ValDecimal(const S:String):longint;
  466. { Converts a decimal string to longint }
  467. var
  468. vs,c : longint;
  469. Begin
  470. vs:=0;
  471. for c:=1 to length(s) do
  472. begin
  473. vs:=vs*10;
  474. if s[c] in ['0'..'9'] then
  475. inc(vs,ord(s[c])-ord('0'))
  476. else
  477. begin
  478. Message1(asmr_e_error_converting_decimal,s);
  479. ValDecimal:=0;
  480. exit;
  481. end;
  482. end;
  483. ValDecimal:=vs;
  484. end;
  485. Function ValOctal(const S:String):longint;
  486. { Converts an octal string to longint }
  487. var
  488. vs,c : longint;
  489. Begin
  490. vs:=0;
  491. for c:=1 to length(s) do
  492. begin
  493. vs:=vs shl 3;
  494. if s[c] in ['0'..'7'] then
  495. inc(vs,ord(s[c])-ord('0'))
  496. else
  497. begin
  498. Message1(asmr_e_error_converting_octal,s);
  499. ValOctal:=0;
  500. exit;
  501. end;
  502. end;
  503. ValOctal:=vs;
  504. end;
  505. Function ValBinary(const S:String):longint;
  506. { Converts a binary string to longint }
  507. var
  508. vs,c : longint;
  509. Begin
  510. vs:=0;
  511. for c:=1 to length(s) do
  512. begin
  513. vs:=vs shl 1;
  514. if s[c] in ['0'..'1'] then
  515. inc(vs,ord(s[c])-ord('0'))
  516. else
  517. begin
  518. Message1(asmr_e_error_converting_binary,s);
  519. ValBinary:=0;
  520. exit;
  521. end;
  522. end;
  523. ValBinary:=vs;
  524. end;
  525. Function ValHexadecimal(const S:String):longint;
  526. { Converts a binary string to longint }
  527. var
  528. vs,c : longint;
  529. Begin
  530. vs:=0;
  531. for c:=1 to length(s) do
  532. begin
  533. vs:=vs shl 4;
  534. case s[c] of
  535. '0'..'9' :
  536. inc(vs,ord(s[c])-ord('0'));
  537. 'A'..'F' :
  538. inc(vs,ord(s[c])-ord('A')+10);
  539. 'a'..'f' :
  540. inc(vs,ord(s[c])-ord('a')+10);
  541. else
  542. begin
  543. Message1(asmr_e_error_converting_hexadecimal,s);
  544. ValHexadecimal:=0;
  545. exit;
  546. end;
  547. end;
  548. end;
  549. ValHexadecimal:=vs;
  550. end;
  551. Function PadZero(Var s: String; n: byte): Boolean;
  552. Begin
  553. PadZero:=TRUE;
  554. { Do some error checking first }
  555. if Length(s) = n then
  556. exit
  557. else
  558. if Length(s) > n then
  559. Begin
  560. PadZero:=FALSE;
  561. delete(s,n+1,length(s));
  562. exit;
  563. end
  564. else
  565. PadZero:=TRUE;
  566. { Fill it up with the specified character }
  567. fillchar(s[length(s)+1],n-1,#0);
  568. s[0]:=chr(n);
  569. end;
  570. {****************************************************************************
  571. TOperand
  572. ****************************************************************************}
  573. constructor TOperand.init;
  574. begin
  575. size:=S_NO;
  576. hasvar:=false;
  577. FillChar(Opr,sizeof(Opr),0);
  578. end;
  579. destructor TOperand.done;
  580. begin
  581. end;
  582. Procedure TOperand.SetSize(_size:longint);
  583. begin
  584. if (size = S_NO) and (_size<extended_size) then
  585. Begin
  586. case _size of
  587. 1 : size:=S_B;
  588. 2 : size:=S_W{ could be S_IS};
  589. 4 : size:=S_L{ could be S_IL or S_FS};
  590. 8 : size:=S_IQ{ could be S_D or S_FL};
  591. extended_size : size:=S_FX;
  592. end;
  593. end;
  594. end;
  595. Function TOperand.SetupResult:boolean;
  596. Begin
  597. SetupResult:=false;
  598. { replace by correct offset. }
  599. if assigned(procinfo^.returntype.def) and
  600. (procinfo^.returntype.def<>pdef(voiddef)) then
  601. begin
  602. opr.ref.offset:=procinfo^.return_offset;
  603. opr.ref.base:= procinfo^.framepointer;
  604. { always assume that the result is valid. }
  605. procinfo^.funcret_state:=vs_assigned;
  606. SetupResult:=true;
  607. end
  608. else
  609. Message(asmr_e_void_function);
  610. end;
  611. Function TOperand.SetupSelf:boolean;
  612. Begin
  613. SetupSelf:=false;
  614. if assigned(procinfo^._class) then
  615. Begin
  616. opr.typ:=OPR_REFERENCE;
  617. opr.ref.offset:=procinfo^.selfpointer_offset;
  618. opr.ref.base:=procinfo^.framepointer;
  619. opr.ref.options:=ref_selffixup;
  620. SetupSelf:=true;
  621. end
  622. else
  623. Message(asmr_e_cannot_use_SELF_outside_a_method);
  624. end;
  625. Function TOperand.SetupOldEBP:boolean;
  626. Begin
  627. SetupOldEBP:=false;
  628. if lexlevel>normal_function_level then
  629. Begin
  630. opr.typ:=OPR_REFERENCE;
  631. opr.ref.offset:=procinfo^.framepointer_offset;
  632. opr.ref.base:=procinfo^.framepointer;
  633. SetupOldEBP:=true;
  634. end
  635. else
  636. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  637. end;
  638. Function TOperand.SetupVar(const hs:string;GetOffset : boolean): Boolean;
  639. { search and sets up the correct fields in the Instr record }
  640. { for the NON-constant identifier passed to the routine. }
  641. { if not found returns FALSE. }
  642. var
  643. sym : psym;
  644. harrdef : parraydef;
  645. Begin
  646. SetupVar:=false;
  647. { are we in a routine ? }
  648. getsym(hs,false);
  649. sym:=srsym;
  650. if sym=nil then
  651. exit;
  652. case sym^.typ of
  653. varsym :
  654. begin
  655. { we always assume in asm statements that }
  656. { that the variable is valid. }
  657. pvarsym(sym)^.varstate:=vs_used;
  658. inc(pvarsym(sym)^.refs);
  659. case pvarsym(sym)^.owner^.symtabletype of
  660. objectsymtable :
  661. begin
  662. { this is not allowed, because we don't know if the self
  663. register is still free, and loading it first is also
  664. not possible, because this could break code }
  665. {opr.typ:=OPR_CONSTANT;
  666. opr.val:=pvarsym(sym)^.address;}
  667. { I do not agree here people using method vars should ensure
  668. that %esi is valid there }
  669. opr.ref.base:=self_pointer;
  670. opr.ref.offset:=pvarsym(sym)^.address;
  671. hasvar:=true;
  672. SetupVar:=true;
  673. Exit;
  674. end;
  675. unitsymtable,
  676. globalsymtable,
  677. staticsymtable :
  678. opr.ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname);
  679. parasymtable :
  680. begin
  681. { if we only want the offset we don't have to care
  682. the base will be zeroed after ! }
  683. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) or
  684. { this below is wrong because there are two parast
  685. for global functions one of interface the second of
  686. implementation
  687. if (pvarsym(sym)^.owner=procinfo^.def^.parast) or }
  688. GetOffset then
  689. begin
  690. opr.ref.base:=procinfo^.framepointer;
  691. end
  692. else
  693. begin
  694. if (procinfo^.framepointer=R_ESP) and
  695. assigned(procinfo^.parent) and
  696. (lexlevel=pvarsym(sym)^.owner^.symtablelevel+1) and
  697. { same problem as above !!
  698. (procinfo^.parent^.sym^.definition^.parast=pvarsym(sym)^.owner) and }
  699. (lexlevel>normal_function_level) then
  700. opr.ref.base:=procinfo^.parent^.framepointer
  701. else
  702. message1(asmr_e_local_para_unreachable,hs);
  703. end;
  704. opr.ref.offset:=pvarsym(sym)^.address;
  705. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) then
  706. begin
  707. opr.ref.offsetfixup:=aktprocsym^.definition^.parast^.address_fixup;
  708. opr.ref.options:=ref_parafixup;
  709. end
  710. else
  711. begin
  712. opr.ref.offsetfixup:=0;
  713. opr.ref.options:=ref_none;
  714. end;
  715. end;
  716. localsymtable :
  717. begin
  718. if (vo_is_external in pvarsym(sym)^.varoptions) then
  719. opr.ref.symbol:=newasmsymbol(pvarsym(sym)^.mangledname)
  720. else
  721. begin
  722. { if we only want the offset we don't have to care
  723. the base will be zeroed after ! }
  724. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) or
  725. {if (pvarsym(sym)^.owner=procinfo^.def^.localst) or}
  726. GetOffset then
  727. opr.ref.base:=procinfo^.framepointer
  728. else
  729. begin
  730. if (procinfo^.framepointer=R_ESP) and
  731. assigned(procinfo^.parent) and
  732. (lexlevel=pvarsym(sym)^.owner^.symtablelevel+1) and
  733. {(procinfo^.parent^.sym^.definition^.localst=pvarsym(sym)^.owner) and}
  734. (lexlevel>normal_function_level) then
  735. opr.ref.base:=procinfo^.parent^.framepointer
  736. else
  737. message1(asmr_e_local_para_unreachable,hs);
  738. end;
  739. opr.ref.offset:=-(pvarsym(sym)^.address);
  740. if (lexlevel=pvarsym(sym)^.owner^.symtablelevel) then
  741. begin
  742. opr.ref.offsetfixup:=aktprocsym^.definition^.localst^.address_fixup;
  743. opr.ref.options:=ref_localfixup;
  744. end
  745. else
  746. begin
  747. opr.ref.offsetfixup:=0;
  748. opr.ref.options:=ref_none;
  749. end;
  750. end;
  751. end;
  752. end;
  753. case pvarsym(sym)^.vartype.def^.deftype of
  754. orddef,
  755. enumdef,
  756. pointerdef,
  757. floatdef :
  758. SetSize(pvarsym(sym)^.getsize);
  759. arraydef :
  760. begin
  761. { for arrays try to get the element size, take care of
  762. multiple indexes }
  763. harrdef:=Parraydef(PVarsym(sym)^.vartype.def);
  764. while assigned(harrdef^.elementtype.def) and
  765. (harrdef^.elementtype.def^.deftype=arraydef) do
  766. harrdef:=parraydef(harrdef^.elementtype.def);
  767. SetSize(harrdef^.elesize);
  768. end;
  769. end;
  770. hasvar:=true;
  771. SetupVar:=true;
  772. Exit;
  773. end;
  774. typedconstsym :
  775. begin
  776. opr.ref.symbol:=newasmsymbol(ptypedconstsym(sym)^.mangledname);
  777. case ptypedconstsym(sym)^.typedconsttype.def^.deftype of
  778. orddef,
  779. enumdef,
  780. pointerdef,
  781. floatdef :
  782. SetSize(ptypedconstsym(sym)^.getsize);
  783. arraydef :
  784. begin
  785. { for arrays try to get the element size, take care of
  786. multiple indexes }
  787. harrdef:=Parraydef(PTypedConstSym(sym)^.typedconsttype.def);
  788. while assigned(harrdef^.elementtype.def) and
  789. (harrdef^.elementtype.def^.deftype=arraydef) do
  790. harrdef:=parraydef(harrdef^.elementtype.def);
  791. SetSize(harrdef^.elesize);
  792. end;
  793. end;
  794. hasvar:=true;
  795. SetupVar:=true;
  796. Exit;
  797. end;
  798. constsym :
  799. begin
  800. if pconstsym(sym)^.consttyp in [constint,constchar,constbool] then
  801. begin
  802. opr.typ:=OPR_CONSTANT;
  803. opr.val:=pconstsym(sym)^.value;
  804. hasvar:=true;
  805. SetupVar:=true;
  806. Exit;
  807. end;
  808. end;
  809. typesym :
  810. begin
  811. if ptypesym(sym)^.restype.def^.deftype in [recorddef,objectdef] then
  812. begin
  813. opr.typ:=OPR_CONSTANT;
  814. opr.val:=0;
  815. hasvar:=true;
  816. SetupVar:=TRUE;
  817. Exit;
  818. end;
  819. end;
  820. procsym :
  821. begin
  822. if assigned(pprocsym(sym)^.definition^.nextoverloaded) then
  823. Message(asmr_w_calling_overload_func);
  824. opr.typ:=OPR_SYMBOL;
  825. opr.symbol:=newasmsymbol(pprocsym(sym)^.definition^.mangledname);
  826. hasvar:=true;
  827. SetupVar:=TRUE;
  828. Exit;
  829. end;
  830. else
  831. begin
  832. Message(asmr_e_unsupported_symbol_type);
  833. exit;
  834. end;
  835. end;
  836. end;
  837. { looks for internal names of variables and routines }
  838. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  839. {$ifndef OLDDIRECTVAR}
  840. var
  841. p : pasmsymbol;
  842. begin
  843. SetupDirectVar:=false;
  844. p:=getasmsymbol(hs);
  845. if assigned(p) then
  846. begin
  847. opr.ref.symbol:=p;
  848. hasvar:=true;
  849. SetupDirectVar:=true;
  850. end;
  851. end;
  852. {$else}
  853. var
  854. p : pai_external;
  855. Begin
  856. SearchDirectVar:=false;
  857. { search in the list of internals }
  858. p:=search_assembler_symbol(internals,hs,EXT_ANY);
  859. if p=nil then
  860. p:=search_assembler_symbol(externals,hs,EXT_ANY);
  861. if p<>nil then
  862. begin
  863. instr.operands[operandnum].opr.ref.symbol:=p^.sym;
  864. case p^.exttyp of
  865. EXT_BYTE : instr.operands[operandnum].size:=S_B;
  866. EXT_WORD : instr.operands[operandnum].size:=S_W;
  867. EXT_NEAR,EXT_FAR,EXT_PROC,EXT_DWORD,EXT_CODEPTR,EXT_DATAPTR:
  868. instr.operands[operandnum].size:=S_L;
  869. EXT_QWORD : instr.operands[operandnum].size:=S_FL;
  870. EXT_TBYTE : instr.operands[operandnum].size:=S_FX;
  871. else
  872. { this is in the case where the instruction is LEA }
  873. { or something like that, in that case size is not }
  874. { important. }
  875. instr.operands[operandnum].size:=S_NO;
  876. end;
  877. instr.operands[operandnum].hasvar:=true;
  878. SearchDirectVar:=TRUE;
  879. Exit;
  880. end;
  881. end;
  882. {$endif}
  883. procedure TOperand.InitRef;
  884. {*********************************************************************}
  885. { Description: This routine first check if the opcode is of }
  886. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  887. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  888. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  889. { to point to the default segment. }
  890. {*********************************************************************}
  891. Begin
  892. case opr.typ of
  893. OPR_REFERENCE:
  894. exit;
  895. OPR_NONE: ;
  896. else
  897. Message(asmr_e_invalid_operand_type);
  898. end;
  899. opr.typ := OPR_REFERENCE;
  900. reset_reference(opr.ref);
  901. end;
  902. procedure TOperand.BuildOperand;
  903. begin
  904. abstract;
  905. end;
  906. {****************************************************************************
  907. TInstruction
  908. ****************************************************************************}
  909. constructor TInstruction.init;
  910. Begin
  911. Opcode:=A_NONE;
  912. Opsize:=S_NO;
  913. Condition:=C_NONE;
  914. Ops:=0;
  915. InitOperands;
  916. end;
  917. destructor TInstruction.done;
  918. var
  919. i : longint;
  920. Begin
  921. for i:=1 to 3 do
  922. Dispose(Operands[i],Done);
  923. end;
  924. procedure TInstruction.InitOperands;
  925. var
  926. i : longint;
  927. begin
  928. for i:=1 to 3 do
  929. New(Operands[i],init);
  930. end;
  931. Procedure TInstruction.SwapOperands;
  932. Var
  933. p : POperand;
  934. Begin
  935. case Ops of
  936. 2 :
  937. begin
  938. p:=Operands[1];
  939. Operands[1]:=Operands[2];
  940. Operands[2]:=p;
  941. end;
  942. 3 :
  943. begin
  944. p:=Operands[1];
  945. Operands[1]:=Operands[3];
  946. Operands[3]:=p;
  947. end;
  948. end;
  949. end;
  950. procedure TInstruction.ConcatInstruction(p:PAasmOutput);
  951. begin
  952. abstract;
  953. end;
  954. procedure TInstruction.BuildOpcode;
  955. begin
  956. abstract;
  957. end;
  958. {***************************************************************************
  959. TLocalLabel
  960. ***************************************************************************}
  961. constructor TLocalLabel.Init(const n:string);
  962. begin
  963. inherited InitName(n);
  964. lab:=nil;
  965. emitted:=false;
  966. end;
  967. function TLocalLabel.Getpasmlabel:pasmlabel;
  968. begin
  969. if not assigned(lab) then
  970. begin
  971. getlabel(lab);
  972. { this label is forced to be used so it's always written }
  973. inc(lab^.refs);
  974. end;
  975. Getpasmlabel:=lab;
  976. end;
  977. {***************************************************************************
  978. TLocalLabelList
  979. ***************************************************************************}
  980. procedure LocalLabelEmitted(p:PNamedIndexObject);{$ifndef FPC}far;{$endif}
  981. begin
  982. if not PLocalLabel(p)^.emitted then
  983. Message1(asmr_e_unknown_label_identifier,p^.name);
  984. end;
  985. procedure TLocalLabelList.CheckEmitted;
  986. begin
  987. ForEach({$ifndef TP}@{$endif}LocalLabelEmitted)
  988. end;
  989. function CreateLocalLabel(const s: string; var hl: pasmlabel; emit:boolean):boolean;
  990. var
  991. lab : PLocalLabel;
  992. Begin
  993. CreateLocalLabel:=true;
  994. { Check if it already is defined }
  995. lab:=PLocalLabel(LocalLabelList^.Search(s));
  996. if not assigned(lab) then
  997. begin
  998. new(lab,init(s));
  999. LocalLabelList^.Insert(lab);
  1000. end;
  1001. { set emitted flag and check for dup syms }
  1002. if emit then
  1003. begin
  1004. if lab^.Emitted then
  1005. begin
  1006. Message1(asmr_e_dup_local_sym,lab^.Name);
  1007. CreateLocalLabel:=false;
  1008. end;
  1009. lab^.Emitted:=true;
  1010. end;
  1011. hl:=lab^.Getpasmlabel;
  1012. end;
  1013. {****************************************************************************
  1014. Symbol table helper routines
  1015. ****************************************************************************}
  1016. Function SearchRecordType(const s:string): boolean;
  1017. Begin
  1018. SearchRecordType:=false;
  1019. { Check the constants in symtable }
  1020. getsym(s,false);
  1021. if srsym <> nil then
  1022. Begin
  1023. case srsym^.typ of
  1024. typesym :
  1025. begin
  1026. if ptypesym(srsym)^.restype.def^.deftype in [recorddef,objectdef] then
  1027. begin
  1028. SearchRecordType:=true;
  1029. exit;
  1030. end;
  1031. end;
  1032. end;
  1033. end;
  1034. end;
  1035. Function SearchIConstant(const s:string; var l:longint): boolean;
  1036. {**********************************************************************}
  1037. { Description: Searches for a CONSTANT of name s in either the local }
  1038. { symbol list, then in the global symbol list, and returns the value }
  1039. { of that constant in l. Returns TRUE if successfull, if not found, }
  1040. { or if the constant is not of correct type, then returns FALSE }
  1041. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1042. { respectively. }
  1043. {**********************************************************************}
  1044. Begin
  1045. SearchIConstant:=false;
  1046. { check for TRUE or FALSE reserved words first }
  1047. if s = 'TRUE' then
  1048. Begin
  1049. SearchIConstant:=TRUE;
  1050. l:=1;
  1051. exit;
  1052. end;
  1053. if s = 'FALSE' then
  1054. Begin
  1055. SearchIConstant:=TRUE;
  1056. l:=0;
  1057. exit;
  1058. end;
  1059. { Check the constants in symtable }
  1060. getsym(s,false);
  1061. if srsym <> nil then
  1062. Begin
  1063. case srsym^.typ of
  1064. constsym :
  1065. begin
  1066. if (pconstsym(srsym)^.consttyp in [constord,constint,constchar,constbool]) then
  1067. Begin
  1068. l:=pconstsym(srsym)^.value;
  1069. SearchIConstant:=TRUE;
  1070. exit;
  1071. end;
  1072. end;
  1073. enumsym:
  1074. Begin
  1075. l:=penumsym(srsym)^.value;
  1076. SearchIConstant:=TRUE;
  1077. exit;
  1078. end;
  1079. end;
  1080. end;
  1081. end;
  1082. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1083. { search and returns the offset and size of records/objects of the base }
  1084. { with field name setup in field. }
  1085. { returns FALSE if not found. }
  1086. { used when base is a variable or a typed constant name. }
  1087. var
  1088. st : psymtable;
  1089. harrdef : parraydef;
  1090. sym : psym;
  1091. i : longint;
  1092. base : string;
  1093. Begin
  1094. GetRecordOffsetSize:=FALSE;
  1095. Offset:=0;
  1096. Size:=0;
  1097. i:=pos('.',s);
  1098. if i=0 then
  1099. i:=255;
  1100. base:=Copy(s,1,i-1);
  1101. delete(s,1,i);
  1102. if base='SELF' then
  1103. st:=procinfo^._class^.symtable
  1104. else
  1105. begin
  1106. getsym(base,false);
  1107. sym:=srsym;
  1108. st:=nil;
  1109. { we can start with a var,type,typedconst }
  1110. case sym^.typ of
  1111. varsym :
  1112. begin
  1113. case pvarsym(sym)^.vartype.def^.deftype of
  1114. recorddef :
  1115. st:=precorddef(pvarsym(sym)^.vartype.def)^.symtable;
  1116. objectdef :
  1117. st:=pobjectdef(pvarsym(sym)^.vartype.def)^.symtable;
  1118. end;
  1119. end;
  1120. typesym :
  1121. begin
  1122. case ptypesym(sym)^.restype.def^.deftype of
  1123. recorddef :
  1124. st:=precorddef(ptypesym(sym)^.restype.def)^.symtable;
  1125. objectdef :
  1126. st:=pobjectdef(ptypesym(sym)^.restype.def)^.symtable;
  1127. end;
  1128. end;
  1129. typedconstsym :
  1130. begin
  1131. case ptypedconstsym(sym)^.typedconsttype.def^.deftype of
  1132. recorddef :
  1133. st:=precorddef(ptypedconstsym(sym)^.typedconsttype.def)^.symtable;
  1134. objectdef :
  1135. st:=pobjectdef(ptypedconstsym(sym)^.typedconsttype.def)^.symtable;
  1136. end;
  1137. end;
  1138. end;
  1139. end;
  1140. { now walk all recordsymtables }
  1141. while assigned(st) and (s<>'') do
  1142. begin
  1143. { load next field in base }
  1144. i:=pos('.',s);
  1145. if i=0 then
  1146. i:=255;
  1147. base:=Copy(s,1,i-1);
  1148. delete(s,1,i);
  1149. if st^.symtabletype=objectsymtable then
  1150. sym:=search_class_member(pobjectdef(st^.defowner),base)
  1151. else
  1152. sym:=st^.search(base);
  1153. if not assigned(sym) then
  1154. begin
  1155. GetRecordOffsetSize:=false;
  1156. exit;
  1157. end;
  1158. st:=nil;
  1159. case sym^.typ of
  1160. varsym :
  1161. begin
  1162. inc(Offset,pvarsym(sym)^.address);
  1163. Size:=PVarsym(sym)^.getsize;
  1164. case pvarsym(sym)^.vartype.def^.deftype of
  1165. arraydef :
  1166. begin
  1167. { for arrays try to get the element size, take care of
  1168. multiple indexes }
  1169. harrdef:=Parraydef(PVarsym(sym)^.vartype.def);
  1170. while assigned(harrdef^.elementtype.def) and
  1171. (harrdef^.elementtype.def^.deftype=arraydef) do
  1172. harrdef:=parraydef(harrdef^.elementtype.def);
  1173. size:=harrdef^.elesize;
  1174. end;
  1175. recorddef :
  1176. st:=precorddef(pvarsym(sym)^.vartype.def)^.symtable;
  1177. objectdef :
  1178. st:=pobjectdef(pvarsym(sym)^.vartype.def)^.symtable;
  1179. end;
  1180. end;
  1181. end;
  1182. end;
  1183. GetRecordOffsetSize:=(s='');
  1184. end;
  1185. Function SearchLabel(const s: string; var hl: pasmlabel;emit:boolean): boolean;
  1186. var
  1187. sym : psym;
  1188. hs : string;
  1189. Begin
  1190. hl:=nil;
  1191. SearchLabel:=false;
  1192. { Check for pascal labels, which are case insensetive }
  1193. hs:=upper(s);
  1194. getsym(hs,false);
  1195. sym:=srsym;
  1196. if sym=nil then
  1197. exit;
  1198. case sym^.typ of
  1199. labelsym :
  1200. begin
  1201. hl:=plabelsym(sym)^.lab;
  1202. if emit then
  1203. plabelsym(sym)^.defined:=true
  1204. else
  1205. plabelsym(sym)^.used:=true;
  1206. SearchLabel:=true;
  1207. exit;
  1208. end;
  1209. end;
  1210. end;
  1211. {*************************************************************************}
  1212. { Instruction Generation Utilities }
  1213. {*************************************************************************}
  1214. Procedure ConcatString(p : paasmoutput;s:string);
  1215. {*********************************************************************}
  1216. { PROCEDURE ConcatString(s:string); }
  1217. { Description: This routine adds the character chain pointed to in }
  1218. { s to the instruction linked list. }
  1219. {*********************************************************************}
  1220. Var
  1221. pc: PChar;
  1222. Begin
  1223. getmem(pc,length(s)+1);
  1224. p^.concat(new(pai_string,init_length_pchar(strpcopy(pc,s),length(s))));
  1225. end;
  1226. Procedure ConcatPasString(p : paasmoutput;s:string);
  1227. {*********************************************************************}
  1228. { PROCEDURE ConcatPasString(s:string); }
  1229. { Description: This routine adds the character chain pointed to in }
  1230. { s to the instruction linked list, contrary to ConcatString it }
  1231. { uses a pascal style string, so it conserves null characters. }
  1232. {*********************************************************************}
  1233. Begin
  1234. p^.concat(new(pai_string,init(s)));
  1235. end;
  1236. Procedure ConcatDirect(p : paasmoutput;s:string);
  1237. {*********************************************************************}
  1238. { PROCEDURE ConcatDirect(s:string) }
  1239. { Description: This routine output the string directly to the asm }
  1240. { output, it is only sed when writing special labels in AT&T mode, }
  1241. { and should not be used without due consideration, since it may }
  1242. { cause problems. }
  1243. {*********************************************************************}
  1244. Var
  1245. pc: PChar;
  1246. Begin
  1247. getmem(pc,length(s)+1);
  1248. p^.concat(new(pai_direct,init(strpcopy(pc,s))));
  1249. end;
  1250. Procedure ConcatConstant(p: paasmoutput; value: longint; maxvalue: longint);
  1251. {*********************************************************************}
  1252. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1253. { Description: This routine adds the value constant to the current }
  1254. { instruction linked list. }
  1255. { maxvalue -> indicates the size of the data to initialize: }
  1256. { $ff -> create a byte node. }
  1257. { $ffff -> create a word node. }
  1258. { $ffffffff -> create a dword node. }
  1259. {*********************************************************************}
  1260. Begin
  1261. if (maxvalue <> $ffffffff) and (value > maxvalue) then
  1262. Begin
  1263. Message(asmr_e_constant_out_of_bounds);
  1264. { assuming a value of maxvalue }
  1265. value:=maxvalue;
  1266. end;
  1267. if maxvalue = $ff then
  1268. p^.concat(new(pai_const,init_8bit(byte(value))))
  1269. else
  1270. if maxvalue = $ffff then
  1271. p^.concat(new(pai_const,init_16bit(word(value))))
  1272. else
  1273. if maxvalue = $ffffffff then
  1274. p^.concat(new(pai_const,init_32bit(longint(value))));
  1275. end;
  1276. Procedure ConcatConstSymbol(p : paasmoutput;const sym:string;l:longint);
  1277. begin
  1278. p^.concat(new(pai_const_symbol,initname_offset(sym,l)));
  1279. end;
  1280. Procedure ConcatRealConstant(p : paasmoutput;value: bestreal; real_typ : tfloattype);
  1281. {***********************************************************************}
  1282. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1283. { Description: This routine adds the value constant to the current }
  1284. { instruction linked list. }
  1285. { real_typ -> indicates the type of the real data to initialize: }
  1286. { s32real -> create a single node. }
  1287. { s64real -> create a double node. }
  1288. { s80real -> create an extended node. }
  1289. { s64bit -> create a comp node. }
  1290. { f32bit -> create a fixed node. (not used normally) }
  1291. {***********************************************************************}
  1292. Begin
  1293. case real_typ of
  1294. s32real : p^.concat(new(pai_real_32bit,init(value)));
  1295. s64real : p^.concat(new(pai_real_64bit,init(value)));
  1296. s80real : p^.concat(new(pai_real_80bit,init(value)));
  1297. s64comp : p^.concat(new(pai_comp_64bit,init(value)));
  1298. f32bit : p^.concat(new(pai_const,init_32bit(trunc(value*$10000))));
  1299. end;
  1300. end;
  1301. Procedure ConcatLabel(p: paasmoutput;var l : pasmlabel);
  1302. {*********************************************************************}
  1303. { PROCEDURE ConcatLabel }
  1304. { Description: This routine either emits a label or a labeled }
  1305. { instruction to the linked list of instructions. }
  1306. {*********************************************************************}
  1307. begin
  1308. p^.concat(new(pai_label,init(l)));
  1309. end;
  1310. procedure ConcatAlign(p:paasmoutput;l:longint);
  1311. {*********************************************************************}
  1312. { PROCEDURE ConcatPublic }
  1313. { Description: This routine emits an global definition to the }
  1314. { linked list of instructions.(used by AT&T styled asm) }
  1315. {*********************************************************************}
  1316. begin
  1317. p^.concat(new(pai_align,init(l)));
  1318. end;
  1319. procedure ConcatPublic(p:paasmoutput;const s : string);
  1320. {*********************************************************************}
  1321. { PROCEDURE ConcatPublic }
  1322. { Description: This routine emits an global definition to the }
  1323. { linked list of instructions.(used by AT&T styled asm) }
  1324. {*********************************************************************}
  1325. begin
  1326. p^.concat(new(pai_symbol,initname_global(s,0)));
  1327. end;
  1328. procedure ConcatLocal(p:paasmoutput;const s : string);
  1329. {*********************************************************************}
  1330. { PROCEDURE ConcatLocal }
  1331. { Description: This routine emits an local definition to the }
  1332. { linked list of instructions. }
  1333. {*********************************************************************}
  1334. begin
  1335. p^.concat(new(pai_symbol,initname(s,0)));
  1336. end;
  1337. Procedure ConcatGlobalBss(const s : string;size : longint);
  1338. {*********************************************************************}
  1339. { PROCEDURE ConcatGlobalBss }
  1340. { Description: This routine emits an global datablock to the }
  1341. { linked list of instructions. }
  1342. {*********************************************************************}
  1343. begin
  1344. bsssegment^.concat(new(pai_datablock,init_global(s,size)));
  1345. end;
  1346. Procedure ConcatLocalBss(const s : string;size : longint);
  1347. {*********************************************************************}
  1348. { PROCEDURE ConcatLocalBss }
  1349. { Description: This routine emits a local datablcok to the }
  1350. { linked list of instructions. }
  1351. {*********************************************************************}
  1352. begin
  1353. bsssegment^.concat(new(pai_datablock,init(s,size)));
  1354. end;
  1355. end.
  1356. {
  1357. $Log$
  1358. Revision 1.37 2000-03-16 15:10:25 pierre
  1359. * correct the fixups for inlined assembler code
  1360. Revision 1.36 2000/03/15 23:10:01 pierre
  1361. * fix for bug 848 (that still genrated wrong code)
  1362. + better testing for variables used in assembler
  1363. (gives an error if variable is not directly reachable !)
  1364. Revision 1.35 2000/02/09 13:23:03 peter
  1365. * log truncated
  1366. Revision 1.34 2000/01/07 01:14:37 peter
  1367. * updated copyright to 2000
  1368. Revision 1.33 1999/12/22 00:57:30 peter
  1369. * label are set to used so an error is given if used but not defined
  1370. Revision 1.32 1999/12/17 10:43:34 florian
  1371. * 761 fixed
  1372. Revision 1.31 1999/11/30 10:40:54 peter
  1373. + ttype, tsymlist
  1374. Revision 1.30 1999/11/17 17:05:04 pierre
  1375. * Notes/hints changes
  1376. Revision 1.29 1999/11/09 23:06:46 peter
  1377. * esi_offset -> selfpointer_offset to be newcg compatible
  1378. * hcogegen -> cgbase fixes for newcg
  1379. Revision 1.28 1999/11/06 14:34:26 peter
  1380. * truncated log to 20 revs
  1381. Revision 1.27 1999/09/27 23:44:58 peter
  1382. * procinfo is now a pointer
  1383. * support for result setting in sub procedure
  1384. Revision 1.26 1999/09/08 16:04:04 peter
  1385. * better support for object fields and more error checks for
  1386. field accesses which create buggy code
  1387. Revision 1.25 1999/09/04 20:29:11 florian
  1388. * bug 577 fixed
  1389. Revision 1.24 1999/08/27 14:37:50 peter
  1390. * fixed crash with typedconst array
  1391. Revision 1.23 1999/08/13 21:28:38 peter
  1392. * more reference types support
  1393. * arraydef size returns elementsize, also for multiple indexing array
  1394. Revision 1.22 1999/08/04 00:23:28 florian
  1395. * renamed i386asm and i386base to cpuasm and cpubase
  1396. Revision 1.21 1999/08/03 22:03:12 peter
  1397. * moved bitmask constants to sets
  1398. * some other type/const renamings
  1399. Revision 1.20 1999/07/29 20:54:06 peter
  1400. * write .size also
  1401. }