rautils.pas 47 KB

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