rautils.pas 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752
  1. {
  2. $Id$
  3. Copyright (c) 1998-2002 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. {$i fpcdefs.inc}
  20. Interface
  21. Uses
  22. cutils,cclasses,
  23. globtype,aasmbase,aasmtai,cpubase,cpuinfo,cginfo,
  24. symconst,symbase,symtype,symdef;
  25. Const
  26. RPNMax = 10; { I think you only need 4, but just to be safe }
  27. OpMax = 25;
  28. {---------------------------------------------------------------------
  29. Local Label Management
  30. ---------------------------------------------------------------------}
  31. Type
  32. { Each local label has this structure associated with it }
  33. TLocalLabel = class(TNamedIndexItem)
  34. Emitted : boolean;
  35. constructor Create(const n:string);
  36. function Gettasmlabel:tasmlabel;
  37. private
  38. lab : tasmlabel;
  39. end;
  40. TLocalLabelList = class(TDictionary)
  41. procedure CheckEmitted;
  42. end;
  43. var
  44. LocalLabelList : TLocalLabelList;
  45. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  46. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  47. {---------------------------------------------------------------------
  48. Instruction management
  49. ---------------------------------------------------------------------}
  50. type
  51. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,
  52. OPR_REFERENCE,OPR_REGISTER,OPR_REGLIST);
  53. TOprRec = record
  54. case typ:TOprType of
  55. OPR_NONE : ();
  56. OPR_CONSTANT : (val:longint);
  57. OPR_SYMBOL : (symbol:tasmsymbol;symofs:longint);
  58. OPR_REFERENCE : (ref:treference);
  59. OPR_REGISTER : (reg:tregister);
  60. {$ifdef m68k}
  61. OPR_REGLIST : (reglist:Tsupregset);
  62. {$else not m68k}
  63. OPR_REGLIST : ();
  64. {$endif m68k}
  65. end;
  66. TOperand = class
  67. hastype, { if the operand has typecasted variable }
  68. hasvar : boolean; { if the operand is loaded with a variable }
  69. size : TCGSize;
  70. opr : TOprRec;
  71. constructor create;
  72. destructor destroy;override;
  73. Procedure BuildOperand;virtual;
  74. Procedure SetSize(_size:longint;force:boolean);virtual;
  75. Procedure SetCorrectSize(opcode:tasmop);virtual;
  76. Function SetupResult:boolean;virtual;
  77. Function SetupSelf:boolean;
  78. Function SetupOldEBP:boolean;
  79. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  80. Function SetupDirectVar(const hs:string): Boolean;
  81. Procedure InitRef;
  82. end;
  83. TInstruction = class
  84. opcode : tasmop;
  85. condition : tasmcond;
  86. ops : byte;
  87. labeled : boolean;
  88. operands : array[1..max_operands] of toperand;
  89. constructor create;
  90. destructor destroy;override;
  91. Procedure InitOperands;virtual;
  92. Procedure BuildOpcode;virtual;
  93. procedure ConcatInstruction(p:TAAsmoutput);virtual;
  94. Procedure Swapoperands;
  95. end;
  96. {---------------------------------------------------------------------}
  97. { Expression parser types }
  98. {---------------------------------------------------------------------}
  99. TExprOperator = record
  100. ch: char; { operator }
  101. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  102. end;
  103. String15 = String[15];
  104. {**********************************************************************}
  105. { The following operators are supported: }
  106. { '+' : addition }
  107. { '-' : subtraction }
  108. { '*' : multiplication }
  109. { '/' : modulo division }
  110. { '^' : exclusive or }
  111. { '<' : shift left }
  112. { '>' : shift right }
  113. { '&' : bitwise and }
  114. { '|' : bitwise or }
  115. { '~' : bitwise complement }
  116. { '%' : modulo division }
  117. { nnn: longint numbers }
  118. { ( and ) parenthesis }
  119. {**********************************************************************}
  120. TExprParse = class
  121. public
  122. Constructor create;
  123. Destructor Destroy;override;
  124. Function Evaluate(Expr: String): longint;
  125. Function Priority(_Operator: Char): longint;
  126. private
  127. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  128. RPNTop : longint;
  129. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  130. OpTop : longint;
  131. Procedure RPNPush(Num: Longint);
  132. Function RPNPop: Longint;
  133. Procedure RPNCalc(token: String15; prefix: boolean);
  134. Procedure OpPush(_Operator: char; prefix: boolean);
  135. { In reality returns TExprOperaotr }
  136. Procedure OpPop(var _Operator:TExprOperator);
  137. end;
  138. { Evaluate an expression string to a longint }
  139. Function CalculateExpression(const expression: string): longint;
  140. {---------------------------------------------------------------------}
  141. { String routines }
  142. {---------------------------------------------------------------------}
  143. Function ValDecimal(const S:String):longint;
  144. Function ValOctal(const S:String):longint;
  145. Function ValBinary(const S:String):longint;
  146. Function ValHexaDecimal(const S:String):longint;
  147. Function PadZero(Var s: String; n: byte): Boolean;
  148. Function EscapeToPascal(const s:string): string;
  149. {---------------------------------------------------------------------
  150. Symbol helper routines
  151. ---------------------------------------------------------------------}
  152. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  153. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  154. Function SearchType(const hs:string): Boolean;
  155. Function SearchRecordType(const s:string): boolean;
  156. Function SearchIConstant(const s:string; var l:longint): boolean;
  157. {---------------------------------------------------------------------
  158. Instruction generation routines
  159. ---------------------------------------------------------------------}
  160. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  161. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  162. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  163. Procedure ConcatConstant(p : TAAsmoutput;value: longint; maxvalue: longint);
  164. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  165. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  166. Procedure ConcatString(p : TAAsmoutput;s:string);
  167. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  168. Procedure ConcatPublic(p:TAAsmoutput;const s : string);
  169. Procedure ConcatLocal(p:TAAsmoutput;const s : string);
  170. Procedure ConcatGlobalBss(const s : string;size : longint);
  171. Procedure ConcatLocalBss(const s : string;size : longint);
  172. Implementation
  173. uses
  174. {$ifdef delphi}
  175. sysutils,
  176. {$else}
  177. strings,
  178. {$endif}
  179. defutil,systems,verbose,globals,
  180. symsym,symtable,paramgr,
  181. aasmcpu,
  182. cgbase,tgobj;
  183. {*************************************************************************
  184. TExprParse
  185. *************************************************************************}
  186. Constructor TExprParse.create;
  187. Begin
  188. end;
  189. Procedure TExprParse.RPNPush(Num : longint);
  190. { Add an operand to the top of the RPN stack }
  191. begin
  192. if RPNTop < RPNMax then
  193. begin
  194. Inc(RPNTop);
  195. RPNStack[RPNTop]:=Num;
  196. end
  197. else
  198. Message(asmr_e_expr_illegal);
  199. end;
  200. Function TExprParse.RPNPop : longint;
  201. { Get the operand at the top of the RPN stack }
  202. begin
  203. if RPNTop > 0 then
  204. begin
  205. RPNPop:=RPNStack[RPNTop];
  206. Dec(RPNTop);
  207. end
  208. else
  209. Message(asmr_e_expr_illegal);
  210. end;
  211. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  212. Var
  213. Temp : longint;
  214. n1,n2 : longint;
  215. LocalError : Integer;
  216. begin
  217. { Handle operators }
  218. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  219. Case Token[1] of
  220. '+' :
  221. Begin
  222. if not prefix then
  223. RPNPush(RPNPop + RPNPop);
  224. end;
  225. '-' :
  226. Begin
  227. if prefix then
  228. RPNPush(-(RPNPop))
  229. else
  230. begin
  231. n1:=RPNPop;
  232. n2:=RPNPop;
  233. RPNPush(n2 - n1);
  234. end;
  235. end;
  236. '*' : RPNPush(RPNPop * RPNPop);
  237. '&' :
  238. begin
  239. n1:=RPNPop;
  240. n2:=RPNPop;
  241. RPNPush(n2 and n1);
  242. end;
  243. '|' :
  244. begin
  245. n1:=RPNPop;
  246. n2:=RPNPop;
  247. RPNPush(n2 or n1);
  248. end;
  249. '~' : RPNPush(NOT RPNPop);
  250. '<' :
  251. begin
  252. n1:=RPNPop;
  253. n2:=RPNPop;
  254. RPNPush(n2 SHL n1);
  255. end;
  256. '>' :
  257. begin
  258. n1:=RPNPop;
  259. n2:=RPNPop;
  260. RPNPush(n2 SHR n1);
  261. end;
  262. '%' :
  263. begin
  264. Temp:=RPNPop;
  265. if Temp <> 0 then
  266. RPNPush(RPNPop mod Temp)
  267. else
  268. begin
  269. Message(asmr_e_expr_zero_divide);
  270. { push 1 for error recovery }
  271. RPNPush(1);
  272. end;
  273. end;
  274. '^' : RPNPush(RPNPop XOR RPNPop);
  275. '/' :
  276. begin
  277. Temp:=RPNPop;
  278. if Temp <> 0 then
  279. RPNPush(RPNPop div Temp)
  280. else
  281. begin
  282. Message(asmr_e_expr_zero_divide);
  283. { push 1 for error recovery }
  284. RPNPush(1);
  285. end;
  286. end;
  287. end
  288. else
  289. begin
  290. { Convert String to number and add to stack }
  291. Val(Token, Temp, LocalError);
  292. if LocalError = 0 then
  293. RPNPush(Temp)
  294. else
  295. begin
  296. Message(asmr_e_expr_illegal);
  297. { push 1 for error recovery }
  298. RPNPush(1);
  299. end;
  300. end;
  301. end;
  302. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  303. { Add an operator onto top of the stack }
  304. begin
  305. if OpTop < OpMax then
  306. begin
  307. Inc(OpTop);
  308. OpStack[OpTop].ch:=_Operator;
  309. OpStack[OpTop].is_prefix:=prefix;
  310. end
  311. else
  312. Message(asmr_e_expr_illegal);
  313. end;
  314. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  315. { Get operator at the top of the stack }
  316. begin
  317. if OpTop > 0 then
  318. begin
  319. _Operator:=OpStack[OpTop];
  320. Dec(OpTop);
  321. end
  322. else
  323. Message(asmr_e_expr_illegal);
  324. end;
  325. Function TExprParse.Priority(_Operator : Char) : longint;
  326. { Return priority of operator }
  327. { The greater the priority, the higher the precedence }
  328. begin
  329. Case _Operator OF
  330. '(' :
  331. Priority:=0;
  332. '+', '-' :
  333. Priority:=1;
  334. '*', '/','%','<','>' :
  335. Priority:=2;
  336. '|','&','^','~' :
  337. Priority:=0;
  338. else
  339. Message(asmr_e_expr_illegal);
  340. end;
  341. end;
  342. Function TExprParse.Evaluate(Expr : String):longint;
  343. Var
  344. I : LongInt;
  345. Token : String15;
  346. opr : TExprOperator;
  347. begin
  348. Evaluate:=0;
  349. { Reset stacks }
  350. OpTop :=0;
  351. RPNTop:=0;
  352. Token :='';
  353. { nothing to do ? }
  354. if Expr='' then
  355. exit;
  356. For I:=1 to Length(Expr) DO
  357. begin
  358. if Expr[I] in ['0'..'9'] then
  359. begin { Build multi-digit numbers }
  360. Token:=Token + Expr[I];
  361. if I = Length(Expr) then { Send last one to calculator }
  362. RPNCalc(Token,false);
  363. end
  364. else
  365. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  366. begin
  367. if Token <> '' then
  368. begin { Send last built number to calc. }
  369. RPNCalc(Token,false);
  370. Token:='';
  371. end;
  372. Case Expr[I] OF
  373. '(' : OpPush('(',false);
  374. ')' : begin
  375. While OpStack[OpTop].ch <> '(' DO
  376. Begin
  377. OpPop(opr);
  378. RPNCalc(opr.ch,opr.is_prefix);
  379. end;
  380. OpPop(opr); { Pop off and ignore the '(' }
  381. end;
  382. '+','-','~' : Begin
  383. { workaround for -2147483648 }
  384. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  385. begin
  386. token:='-';
  387. expr[i]:='+';
  388. end;
  389. { if start of expression then surely a prefix }
  390. { or if previous char was also an operator }
  391. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  392. OpPush(Expr[I],true)
  393. else
  394. Begin
  395. { Evaluate all higher priority operators }
  396. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  397. Begin
  398. OpPop(opr);
  399. RPNCalc(opr.ch,opr.is_prefix);
  400. end;
  401. OpPush(Expr[I],false);
  402. End;
  403. end;
  404. '*', '/',
  405. '^','|','&',
  406. '%','<','>' : begin
  407. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  408. Begin
  409. OpPop(opr);
  410. RPNCalc(opr.ch,opr.is_prefix);
  411. end;
  412. OpPush(Expr[I],false);
  413. end;
  414. end; { Case }
  415. end
  416. else
  417. Message(asmr_e_expr_illegal); { Handle bad input error }
  418. end;
  419. { Pop off the remaining operators }
  420. While OpTop > 0 do
  421. Begin
  422. OpPop(opr);
  423. RPNCalc(opr.ch,opr.is_prefix);
  424. end;
  425. { The result is stored on the top of the stack }
  426. Evaluate:=RPNPop;
  427. end;
  428. Destructor TExprParse.Destroy;
  429. Begin
  430. end;
  431. Function CalculateExpression(const expression: string): longint;
  432. var
  433. expr: TExprParse;
  434. Begin
  435. expr:=TExprParse.create;
  436. CalculateExpression:=expr.Evaluate(expression);
  437. expr.Free;
  438. end;
  439. {*************************************************************************}
  440. { String conversions/utils }
  441. {*************************************************************************}
  442. Function EscapeToPascal(const s:string): string;
  443. { converts a C styled string - which contains escape }
  444. { characters to a pascal style string. }
  445. var
  446. i,len : longint;
  447. hs : string;
  448. temp : string;
  449. c : char;
  450. Begin
  451. hs:='';
  452. len:=0;
  453. i:=0;
  454. while (i<length(s)) and (len<255) do
  455. begin
  456. Inc(i);
  457. if (s[i]='\') and (i<length(s)) then
  458. Begin
  459. inc(i);
  460. case s[i] of
  461. '\' :
  462. c:='\';
  463. 'b':
  464. c:=#8;
  465. 'f':
  466. c:=#12;
  467. 'n':
  468. c:=#10;
  469. 'r':
  470. c:=#13;
  471. 't':
  472. c:=#9;
  473. '"':
  474. c:='"';
  475. '0'..'7':
  476. Begin
  477. temp:=s[i];
  478. temp:=temp+s[i+1];
  479. temp:=temp+s[i+2];
  480. inc(i,2);
  481. c:=chr(ValOctal(temp));
  482. end;
  483. 'x':
  484. Begin
  485. temp:=s[i+1];
  486. temp:=temp+s[i+2];
  487. inc(i,2);
  488. c:=chr(ValHexaDecimal(temp));
  489. end;
  490. else
  491. Begin
  492. Message1(asmr_e_escape_seq_ignored,s[i]);
  493. c:=s[i];
  494. end;
  495. end;
  496. end
  497. else
  498. c:=s[i];
  499. inc(len);
  500. hs[len]:=c;
  501. end;
  502. hs[0]:=chr(len);
  503. EscapeToPascal:=hs;
  504. end;
  505. Function ValDecimal(const S:String):longint;
  506. { Converts a decimal 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*10;
  514. if s[c] in ['0'..'9'] then
  515. inc(vs,ord(s[c])-ord('0'))
  516. else
  517. begin
  518. Message1(asmr_e_error_converting_decimal,s);
  519. ValDecimal:=0;
  520. exit;
  521. end;
  522. end;
  523. ValDecimal:=vs;
  524. end;
  525. Function ValOctal(const S:String):longint;
  526. { Converts an octal 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 3;
  534. if s[c] in ['0'..'7'] then
  535. inc(vs,ord(s[c])-ord('0'))
  536. else
  537. begin
  538. Message1(asmr_e_error_converting_octal,s);
  539. ValOctal:=0;
  540. exit;
  541. end;
  542. end;
  543. ValOctal:=vs;
  544. end;
  545. Function ValBinary(const S:String):longint;
  546. { Converts a binary string to longint }
  547. var
  548. vs,c : longint;
  549. Begin
  550. vs:=0;
  551. for c:=1 to length(s) do
  552. begin
  553. vs:=vs shl 1;
  554. if s[c] in ['0'..'1'] then
  555. inc(vs,ord(s[c])-ord('0'))
  556. else
  557. begin
  558. Message1(asmr_e_error_converting_binary,s);
  559. ValBinary:=0;
  560. exit;
  561. end;
  562. end;
  563. ValBinary:=vs;
  564. end;
  565. Function ValHexadecimal(const S:String):longint;
  566. { Converts a binary string to longint }
  567. var
  568. vs,c : longint;
  569. Begin
  570. vs:=0;
  571. for c:=1 to length(s) do
  572. begin
  573. vs:=vs shl 4;
  574. case s[c] of
  575. '0'..'9' :
  576. inc(vs,ord(s[c])-ord('0'));
  577. 'A'..'F' :
  578. inc(vs,ord(s[c])-ord('A')+10);
  579. 'a'..'f' :
  580. inc(vs,ord(s[c])-ord('a')+10);
  581. else
  582. begin
  583. Message1(asmr_e_error_converting_hexadecimal,s);
  584. ValHexadecimal:=0;
  585. exit;
  586. end;
  587. end;
  588. end;
  589. ValHexadecimal:=vs;
  590. end;
  591. Function PadZero(Var s: String; n: byte): Boolean;
  592. Begin
  593. PadZero:=TRUE;
  594. { Do some error checking first }
  595. if Length(s) = n then
  596. exit
  597. else
  598. if Length(s) > n then
  599. Begin
  600. PadZero:=FALSE;
  601. delete(s,n+1,length(s));
  602. exit;
  603. end
  604. else
  605. PadZero:=TRUE;
  606. { Fill it up with the specified character }
  607. fillchar(s[length(s)+1],n-1,#0);
  608. s[0]:=chr(n);
  609. end;
  610. {****************************************************************************
  611. TOperand
  612. ****************************************************************************}
  613. constructor TOperand.Create;
  614. begin
  615. size:=OS_NO;
  616. hastype:=false;
  617. hasvar:=false;
  618. FillChar(Opr,sizeof(Opr),0);
  619. end;
  620. destructor TOperand.destroy;
  621. begin
  622. end;
  623. Procedure TOperand.SetSize(_size:longint;force:boolean);
  624. begin
  625. if force or
  626. ((size = OS_NO) and (_size<=extended_size)) then
  627. Begin
  628. case _size of
  629. 1 : size:=OS_8;
  630. 2 : size:=OS_16{ could be S_IS};
  631. 4 : size:=OS_32{ could be S_IL or S_FS};
  632. 8 : size:=OS_64{ could be S_D or S_FL};
  633. else
  634. begin
  635. { extended_size can also be 8, resulting in a
  636. duplicate label }
  637. if _size=extended_size then
  638. size:=OS_F80;
  639. end;
  640. end;
  641. end;
  642. end;
  643. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  644. begin
  645. end;
  646. Function TOperand.SetupResult:boolean;
  647. Begin
  648. SetupResult:=false;
  649. { replace by correct offset. }
  650. if (not is_void(current_procdef.rettype.def)) then
  651. begin
  652. if (m_tp7 in aktmodeswitches) and
  653. (not paramanager.ret_in_param(current_procdef.rettype.def,current_procdef.proccalloption)) then
  654. begin
  655. Message(asmr_e_cannot_use_RESULT_here);
  656. exit;
  657. end;
  658. SetupResult:=setupvar('result',false)
  659. end
  660. else
  661. Message(asmr_e_void_function);
  662. end;
  663. Function TOperand.SetupSelf:boolean;
  664. Begin
  665. SetupSelf:=false;
  666. if assigned(current_procdef._class) then
  667. SetupSelf:=setupvar('self',false)
  668. else
  669. Message(asmr_e_cannot_use_SELF_outside_a_method);
  670. end;
  671. Function TOperand.SetupOldEBP:boolean;
  672. Begin
  673. SetupOldEBP:=false;
  674. if current_procdef.parast.symtablelevel>normal_function_level then
  675. SetupOldEBP:=setupvar('parentframe',false)
  676. else
  677. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  678. end;
  679. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  680. { search and sets up the correct fields in the Instr record }
  681. { for the NON-constant identifier passed to the routine. }
  682. { if not found returns FALSE. }
  683. var
  684. sym : tsym;
  685. srsymtable : tsymtable;
  686. harrdef : tarraydef;
  687. l : longint;
  688. Begin
  689. SetupVar:=false;
  690. asmsearchsym(s,sym,srsymtable);
  691. if sym = nil then
  692. exit;
  693. case sym.typ of
  694. varsym :
  695. begin
  696. { we always assume in asm statements that }
  697. { that the variable is valid. }
  698. tvarsym(sym).varstate:=vs_used;
  699. inc(tvarsym(sym).refs);
  700. case tvarsym(sym).owner.symtabletype of
  701. objectsymtable :
  702. begin
  703. { We return the address of the field, just like Delphi/TP }
  704. opr.typ:=OPR_CONSTANT;
  705. opr.val:=tvarsym(sym).address;
  706. hasvar:=true;
  707. SetupVar:=true;
  708. Exit;
  709. end;
  710. globalsymtable,
  711. staticsymtable :
  712. opr.ref.symbol:=objectlibrary.newasmsymboldata(tvarsym(sym).mangledname);
  713. parasymtable :
  714. begin
  715. { if we only want the offset we don't have to care
  716. the base will be zeroed after ! }
  717. if (tvarsym(sym).owner=current_procdef.parast) or
  718. GetOffset then
  719. begin
  720. opr.ref.base:=current_procinfo.framepointer;
  721. end
  722. else
  723. begin
  724. if (current_procdef.localst.datasize=0) and
  725. assigned(current_procinfo.parent) and
  726. (tvarsym(sym).owner=current_procdef.parast) and
  727. (current_procdef.parast.symtablelevel>normal_function_level) then
  728. opr.ref.base:=current_procinfo.parent.framepointer
  729. else
  730. message1(asmr_e_local_para_unreachable,s);
  731. end;
  732. opr.ref.offset:=tvarsym(sym).address;
  733. if (current_procdef.parast.symtablelevel=tvarsym(sym).owner.symtablelevel) then
  734. begin
  735. opr.ref.offsetfixup:=current_procdef.parast.address_fixup;
  736. opr.ref.options:=ref_parafixup;
  737. end
  738. else
  739. begin
  740. opr.ref.offsetfixup:=0;
  741. opr.ref.options:=ref_none;
  742. end;
  743. if (tvarsym(sym).varspez=vs_var) or
  744. ((tvarsym(sym).varspez=vs_const) and
  745. paramanager.push_addr_param(tvarsym(sym).vartype.def,current_procdef.proccalloption)) then
  746. SetSize(pointer_size,false);
  747. end;
  748. localsymtable :
  749. begin
  750. if (vo_is_external in tvarsym(sym).varoptions) then
  751. opr.ref.symbol:=objectlibrary.newasmsymboldata(tvarsym(sym).mangledname)
  752. else
  753. begin
  754. { if we only want the offset we don't have to care
  755. the base will be zeroed after ! }
  756. if (tvarsym(sym).owner=current_procdef.localst) or
  757. GetOffset then
  758. opr.ref.base:=current_procinfo.framepointer
  759. else
  760. begin
  761. if (current_procdef.localst.datasize=0) and
  762. assigned(current_procinfo.parent) and
  763. (tvarsym(sym).owner=current_procinfo.parent.procdef.localst) and
  764. (current_procdef.parast.symtablelevel>normal_function_level) then
  765. opr.ref.base:=current_procinfo.parent.framepointer
  766. else
  767. message1(asmr_e_local_para_unreachable,s);
  768. end;
  769. opr.ref.offset:=tvarsym(sym).address;
  770. if (current_procdef.localst.symtablelevel=tvarsym(sym).owner.symtablelevel) then
  771. begin
  772. opr.ref.offsetfixup:=current_procdef.localst.address_fixup;
  773. opr.ref.options:=ref_localfixup;
  774. end
  775. else
  776. begin
  777. opr.ref.offsetfixup:=0;
  778. opr.ref.options:=ref_none;
  779. end;
  780. end;
  781. if (tvarsym(sym).varspez in [vs_var,vs_out]) or
  782. ((tvarsym(sym).varspez=vs_const) and
  783. paramanager.push_addr_param(tvarsym(sym).vartype.def,current_procdef.proccalloption)) then
  784. SetSize(pointer_size,false);
  785. end;
  786. end;
  787. case tvarsym(sym).vartype.def.deftype of
  788. orddef,
  789. enumdef,
  790. pointerdef,
  791. floatdef :
  792. SetSize(tvarsym(sym).getsize,false);
  793. arraydef :
  794. begin
  795. { for arrays try to get the element size, take care of
  796. multiple indexes }
  797. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  798. while assigned(harrdef.elementtype.def) and
  799. (harrdef.elementtype.def.deftype=arraydef) do
  800. harrdef:=tarraydef(harrdef.elementtype.def);
  801. SetSize(harrdef.elesize,false);
  802. end;
  803. end;
  804. hasvar:=true;
  805. SetupVar:=true;
  806. Exit;
  807. end;
  808. typedconstsym :
  809. begin
  810. opr.ref.symbol:=objectlibrary.newasmsymboldata(ttypedconstsym(sym).mangledname);
  811. case ttypedconstsym(sym).typedconsttype.def.deftype of
  812. orddef,
  813. enumdef,
  814. pointerdef,
  815. floatdef :
  816. SetSize(ttypedconstsym(sym).getsize,false);
  817. arraydef :
  818. begin
  819. { for arrays try to get the element size, take care of
  820. multiple indexes }
  821. harrdef:=tarraydef(ttypedconstsym(sym).typedconsttype.def);
  822. while assigned(harrdef.elementtype.def) and
  823. (harrdef.elementtype.def.deftype=arraydef) do
  824. harrdef:=tarraydef(harrdef.elementtype.def);
  825. SetSize(harrdef.elesize,false);
  826. end;
  827. end;
  828. hasvar:=true;
  829. SetupVar:=true;
  830. Exit;
  831. end;
  832. constsym :
  833. begin
  834. if tconstsym(sym).consttyp in [constint,constchar,constbool] then
  835. begin
  836. opr.typ:=OPR_CONSTANT;
  837. opr.val:=tconstsym(sym).value.valueord;
  838. SetupVar:=true;
  839. Exit;
  840. end;
  841. end;
  842. typesym :
  843. begin
  844. if ttypesym(sym).restype.def.deftype in [recorddef,objectdef] then
  845. begin
  846. opr.typ:=OPR_CONSTANT;
  847. opr.val:=0;
  848. SetupVar:=TRUE;
  849. Exit;
  850. end;
  851. end;
  852. procsym :
  853. begin
  854. if Tprocsym(sym).procdef_count>1 then
  855. Message(asmr_w_calling_overload_func);
  856. l:=opr.ref.offset;
  857. opr.typ:=OPR_SYMBOL;
  858. opr.symbol:=objectlibrary.newasmsymbol(tprocsym(sym).first_procdef.mangledname);
  859. opr.symofs:=l;
  860. hasvar:=true;
  861. SetupVar:=TRUE;
  862. Exit;
  863. end;
  864. else
  865. begin
  866. Message(asmr_e_unsupported_symbol_type);
  867. exit;
  868. end;
  869. end;
  870. end;
  871. { looks for internal names of variables and routines }
  872. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  873. var
  874. p : tasmsymbol;
  875. begin
  876. SetupDirectVar:=false;
  877. p:=objectlibrary.getasmsymbol(hs);
  878. if assigned(p) then
  879. begin
  880. opr.ref.symbol:=p;
  881. hasvar:=true;
  882. SetupDirectVar:=true;
  883. end;
  884. end;
  885. procedure TOperand.InitRef;
  886. {*********************************************************************}
  887. { Description: This routine first check if the opcode is of }
  888. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  889. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  890. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  891. { to point to the default segment. }
  892. {*********************************************************************}
  893. Begin
  894. case opr.typ of
  895. OPR_REFERENCE:
  896. exit;
  897. OPR_NONE: ;
  898. else
  899. Message(asmr_e_invalid_operand_type);
  900. end;
  901. opr.typ := OPR_REFERENCE;
  902. Fillchar(opr.ref,sizeof(treference),0);
  903. {$ifdef i386}
  904. opr.ref.segment.enum:=R_INTREGISTER;
  905. {$endif}
  906. opr.ref.index.enum:=R_INTREGISTER;
  907. opr.ref.base.enum:=R_INTREGISTER;
  908. end;
  909. procedure TOperand.BuildOperand;
  910. begin
  911. abstract;
  912. end;
  913. {****************************************************************************
  914. TInstruction
  915. ****************************************************************************}
  916. constructor TInstruction.create;
  917. Begin
  918. Opcode:=A_NONE;
  919. Condition:=C_NONE;
  920. Ops:=0;
  921. InitOperands;
  922. Labeled:=false;
  923. end;
  924. destructor TInstruction.destroy;
  925. var
  926. i : longint;
  927. Begin
  928. for i:=1 to 3 do
  929. Operands[i].free;
  930. end;
  931. procedure TInstruction.InitOperands;
  932. var
  933. i : longint;
  934. begin
  935. for i:=1 to 3 do
  936. Operands[i].create;
  937. end;
  938. Procedure TInstruction.Swapoperands;
  939. Var
  940. p : toperand;
  941. Begin
  942. case Ops of
  943. 2 :
  944. begin
  945. p:=Operands[1];
  946. Operands[1]:=Operands[2];
  947. Operands[2]:=p;
  948. end;
  949. 3 :
  950. begin
  951. p:=Operands[1];
  952. Operands[1]:=Operands[3];
  953. Operands[3]:=p;
  954. end;
  955. end;
  956. end;
  957. procedure TInstruction.ConcatInstruction(p:TAAsmOutput);
  958. begin
  959. abstract;
  960. end;
  961. procedure TInstruction.BuildOpcode;
  962. begin
  963. abstract;
  964. end;
  965. {***************************************************************************
  966. TLocalLabel
  967. ***************************************************************************}
  968. constructor TLocalLabel.create(const n:string);
  969. begin
  970. inherited CreateName(n);
  971. lab:=nil;
  972. emitted:=false;
  973. end;
  974. function TLocalLabel.Gettasmlabel:tasmlabel;
  975. begin
  976. if not assigned(lab) then
  977. begin
  978. objectlibrary.getlabel(lab);
  979. { this label is forced to be used so it's always written }
  980. lab.increfs;
  981. end;
  982. Gettasmlabel:=lab;
  983. end;
  984. {***************************************************************************
  985. TLocalLabelList
  986. ***************************************************************************}
  987. procedure LocalLabelEmitted(p:tnamedindexitem;arg:pointer);
  988. begin
  989. if not TLocalLabel(p).emitted then
  990. Message1(asmr_e_unknown_label_identifier,p.name);
  991. end;
  992. procedure TLocalLabelList.CheckEmitted;
  993. begin
  994. ForEach_Static({$ifdef FPCPROCVAR}@{$endif}LocalLabelEmitted,nil)
  995. end;
  996. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  997. var
  998. lab : TLocalLabel;
  999. Begin
  1000. CreateLocalLabel:=true;
  1001. { Check if it already is defined }
  1002. lab:=TLocalLabel(LocalLabellist.Search(s));
  1003. if not assigned(lab) then
  1004. begin
  1005. lab:=TLocalLabel.Create(s);
  1006. LocalLabellist.Insert(lab);
  1007. end;
  1008. { set emitted flag and check for dup syms }
  1009. if emit then
  1010. begin
  1011. if lab.Emitted then
  1012. begin
  1013. Message1(asmr_e_dup_local_sym,lab.Name);
  1014. CreateLocalLabel:=false;
  1015. end;
  1016. lab.Emitted:=true;
  1017. end;
  1018. hl:=lab.Gettasmlabel;
  1019. end;
  1020. {****************************************************************************
  1021. Symbol table helper routines
  1022. ****************************************************************************}
  1023. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1024. var
  1025. i : integer;
  1026. begin
  1027. i:=pos('.',s);
  1028. { allow unit.identifier }
  1029. if i>0 then
  1030. begin
  1031. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1032. if assigned(srsym) then
  1033. begin
  1034. if (srsym.typ=unitsym) and
  1035. (srsym.owner.unitid=0) then
  1036. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1037. else
  1038. srsym:=nil;
  1039. end;
  1040. end
  1041. else
  1042. searchsym(s,srsym,srsymtable);
  1043. end;
  1044. Function SearchType(const hs:string): Boolean;
  1045. var
  1046. srsym : tsym;
  1047. srsymtable : tsymtable;
  1048. begin
  1049. asmsearchsym(hs,srsym,srsymtable);
  1050. SearchType:=assigned(srsym) and
  1051. (srsym.typ=typesym);
  1052. end;
  1053. Function SearchRecordType(const s:string): boolean;
  1054. var
  1055. srsym : tsym;
  1056. srsymtable : tsymtable;
  1057. Begin
  1058. SearchRecordType:=false;
  1059. { Check the constants in symtable }
  1060. asmsearchsym(s,srsym,srsymtable);
  1061. if srsym <> nil then
  1062. Begin
  1063. case srsym.typ of
  1064. typesym :
  1065. begin
  1066. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1067. begin
  1068. SearchRecordType:=true;
  1069. exit;
  1070. end;
  1071. end;
  1072. end;
  1073. end;
  1074. end;
  1075. Function SearchIConstant(const s:string; var l:longint): boolean;
  1076. {**********************************************************************}
  1077. { Description: Searches for a CONSTANT of name s in either the local }
  1078. { symbol list, then in the global symbol list, and returns the value }
  1079. { of that constant in l. Returns TRUE if successfull, if not found, }
  1080. { or if the constant is not of correct type, then returns FALSE }
  1081. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1082. { respectively. }
  1083. {**********************************************************************}
  1084. var
  1085. srsym : tsym;
  1086. srsymtable : tsymtable;
  1087. Begin
  1088. SearchIConstant:=false;
  1089. { check for TRUE or FALSE reserved words first }
  1090. if s = 'TRUE' then
  1091. Begin
  1092. SearchIConstant:=TRUE;
  1093. l:=1;
  1094. exit;
  1095. end;
  1096. if s = 'FALSE' then
  1097. Begin
  1098. SearchIConstant:=TRUE;
  1099. l:=0;
  1100. exit;
  1101. end;
  1102. { Check the constants in symtable }
  1103. asmsearchsym(s,srsym,srsymtable);
  1104. if srsym <> nil then
  1105. Begin
  1106. case srsym.typ of
  1107. constsym :
  1108. begin
  1109. if (tconstsym(srsym).consttyp in [constord,constint,constchar,constbool]) then
  1110. Begin
  1111. l:=tconstsym(srsym).value.valueord;
  1112. SearchIConstant:=TRUE;
  1113. exit;
  1114. end;
  1115. end;
  1116. enumsym:
  1117. Begin
  1118. l:=tenumsym(srsym).value;
  1119. SearchIConstant:=TRUE;
  1120. exit;
  1121. end;
  1122. end;
  1123. end;
  1124. end;
  1125. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1126. { search and returns the offset and size of records/objects of the base }
  1127. { with field name setup in field. }
  1128. { returns FALSE if not found. }
  1129. { used when base is a variable or a typed constant name. }
  1130. var
  1131. st : tsymtable;
  1132. harrdef : tarraydef;
  1133. sym : tsym;
  1134. srsymtable : tsymtable;
  1135. i : longint;
  1136. base : string;
  1137. Begin
  1138. GetRecordOffsetSize:=FALSE;
  1139. Offset:=0;
  1140. Size:=0;
  1141. i:=pos('.',s);
  1142. if i=0 then
  1143. i:=255;
  1144. base:=Copy(s,1,i-1);
  1145. delete(s,1,i);
  1146. if base='SELF' then
  1147. st:=current_procdef._class.symtable
  1148. else
  1149. begin
  1150. asmsearchsym(base,sym,srsymtable);
  1151. st:=nil;
  1152. { we can start with a var,type,typedconst }
  1153. case sym.typ of
  1154. varsym :
  1155. begin
  1156. case tvarsym(sym).vartype.def.deftype of
  1157. recorddef :
  1158. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1159. objectdef :
  1160. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1161. end;
  1162. end;
  1163. typesym :
  1164. begin
  1165. case ttypesym(sym).restype.def.deftype of
  1166. recorddef :
  1167. st:=trecorddef(ttypesym(sym).restype.def).symtable;
  1168. objectdef :
  1169. st:=tobjectdef(ttypesym(sym).restype.def).symtable;
  1170. end;
  1171. end;
  1172. typedconstsym :
  1173. begin
  1174. case ttypedconstsym(sym).typedconsttype.def.deftype of
  1175. recorddef :
  1176. st:=trecorddef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1177. objectdef :
  1178. st:=tobjectdef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1179. end;
  1180. end;
  1181. end;
  1182. end;
  1183. { now walk all recordsymtables }
  1184. while assigned(st) and (s<>'') do
  1185. begin
  1186. { load next field in base }
  1187. i:=pos('.',s);
  1188. if i=0 then
  1189. i:=255;
  1190. base:=Copy(s,1,i-1);
  1191. delete(s,1,i);
  1192. if st.symtabletype=objectsymtable then
  1193. sym:=search_class_member(tobjectdef(st.defowner),base)
  1194. else
  1195. sym:=tsym(st.search(base));
  1196. if not assigned(sym) then
  1197. begin
  1198. GetRecordOffsetSize:=false;
  1199. exit;
  1200. end;
  1201. st:=nil;
  1202. case sym.typ of
  1203. varsym :
  1204. begin
  1205. inc(Offset,tvarsym(sym).address);
  1206. Size:=tvarsym(sym).getsize;
  1207. case tvarsym(sym).vartype.def.deftype of
  1208. arraydef :
  1209. begin
  1210. { for arrays try to get the element size, take care of
  1211. multiple indexes }
  1212. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  1213. while assigned(harrdef.elementtype.def) and
  1214. (harrdef.elementtype.def.deftype=arraydef) do
  1215. harrdef:=tarraydef(harrdef.elementtype.def);
  1216. size:=harrdef.elesize;
  1217. end;
  1218. recorddef :
  1219. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1220. objectdef :
  1221. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1222. end;
  1223. end;
  1224. end;
  1225. end;
  1226. GetRecordOffsetSize:=(s='');
  1227. end;
  1228. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1229. var
  1230. sym : tsym;
  1231. srsymtable : tsymtable;
  1232. hs : string;
  1233. Begin
  1234. hl:=nil;
  1235. SearchLabel:=false;
  1236. { Check for pascal labels, which are case insensetive }
  1237. hs:=upper(s);
  1238. asmsearchsym(hs,sym,srsymtable);
  1239. if sym=nil then
  1240. exit;
  1241. case sym.typ of
  1242. labelsym :
  1243. begin
  1244. hl:=tlabelsym(sym).lab;
  1245. if emit then
  1246. tlabelsym(sym).defined:=true
  1247. else
  1248. tlabelsym(sym).used:=true;
  1249. SearchLabel:=true;
  1250. exit;
  1251. end;
  1252. end;
  1253. end;
  1254. {*************************************************************************}
  1255. { Instruction Generation Utilities }
  1256. {*************************************************************************}
  1257. Procedure ConcatString(p : TAAsmoutput;s:string);
  1258. {*********************************************************************}
  1259. { PROCEDURE ConcatString(s:string); }
  1260. { Description: This routine adds the character chain pointed to in }
  1261. { s to the instruction linked list. }
  1262. {*********************************************************************}
  1263. Var
  1264. pc: PChar;
  1265. Begin
  1266. getmem(pc,length(s)+1);
  1267. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1268. end;
  1269. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1270. {*********************************************************************}
  1271. { PROCEDURE ConcatPasString(s:string); }
  1272. { Description: This routine adds the character chain pointed to in }
  1273. { s to the instruction linked list, contrary to ConcatString it }
  1274. { uses a pascal style string, so it conserves null characters. }
  1275. {*********************************************************************}
  1276. Begin
  1277. p.concat(Tai_string.Create(s));
  1278. end;
  1279. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1280. {*********************************************************************}
  1281. { PROCEDURE ConcatDirect(s:string) }
  1282. { Description: This routine output the string directly to the asm }
  1283. { output, it is only sed when writing special labels in AT&T mode, }
  1284. { and should not be used without due consideration, since it may }
  1285. { cause problems. }
  1286. {*********************************************************************}
  1287. Var
  1288. pc: PChar;
  1289. Begin
  1290. getmem(pc,length(s)+1);
  1291. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1292. end;
  1293. Procedure ConcatConstant(p: TAAsmoutput; value: longint; maxvalue: longint);
  1294. {*********************************************************************}
  1295. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1296. { Description: This routine adds the value constant to the current }
  1297. { instruction linked list. }
  1298. { maxvalue -> indicates the size of the data to initialize: }
  1299. { $ff -> create a byte node. }
  1300. { $ffff -> create a word node. }
  1301. { $ffffffff -> create a dword node. }
  1302. {*********************************************************************}
  1303. Begin
  1304. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1305. Begin
  1306. Message(asmr_e_constant_out_of_bounds);
  1307. { assuming a value of maxvalue }
  1308. value:=maxvalue;
  1309. end;
  1310. if maxvalue = $ff then
  1311. p.concat(Tai_const.Create_8bit(byte(value)))
  1312. else
  1313. if maxvalue = $ffff then
  1314. p.concat(Tai_const.Create_16bit(word(value)))
  1315. else
  1316. if maxvalue = longint($ffffffff) then
  1317. p.concat(Tai_const.Create_32bit(longint(value)));
  1318. end;
  1319. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  1320. begin
  1321. p.concat(Tai_const_symbol.Createname_offset(sym,l));
  1322. end;
  1323. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1324. {***********************************************************************}
  1325. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1326. { Description: This routine adds the value constant to the current }
  1327. { instruction linked list. }
  1328. { real_typ -> indicates the type of the real data to initialize: }
  1329. { s32real -> create a single node. }
  1330. { s64real -> create a double node. }
  1331. { s80real -> create an extended node. }
  1332. { s64bit -> create a comp node. }
  1333. { f32bit -> create a fixed node. (not used normally) }
  1334. {***********************************************************************}
  1335. Begin
  1336. case real_typ of
  1337. s32real : p.concat(Tai_real_32bit.Create(value));
  1338. s64real : p.concat(Tai_real_64bit.Create(value));
  1339. s80real : p.concat(Tai_real_80bit.Create(value));
  1340. s64comp : p.concat(Tai_comp_64bit.Create(value));
  1341. end;
  1342. end;
  1343. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1344. {*********************************************************************}
  1345. { PROCEDURE ConcatLabel }
  1346. { Description: This routine either emits a label or a labeled }
  1347. { instruction to the linked list of instructions. }
  1348. {*********************************************************************}
  1349. begin
  1350. p.concat(Tai_label.Create(l));
  1351. end;
  1352. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  1353. {*********************************************************************}
  1354. { PROCEDURE ConcatPublic }
  1355. { Description: This routine emits an global definition to the }
  1356. { linked list of instructions.(used by AT&T styled asm) }
  1357. {*********************************************************************}
  1358. begin
  1359. p.concat(Tai_align.Create(l));
  1360. end;
  1361. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1362. {*********************************************************************}
  1363. { PROCEDURE ConcatPublic }
  1364. { Description: This routine emits an global definition to the }
  1365. { linked list of instructions.(used by AT&T styled asm) }
  1366. {*********************************************************************}
  1367. begin
  1368. p.concat(Tai_symbol.Createname_global(s,0));
  1369. end;
  1370. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1371. {*********************************************************************}
  1372. { PROCEDURE ConcatLocal }
  1373. { Description: This routine emits an local definition to the }
  1374. { linked list of instructions. }
  1375. {*********************************************************************}
  1376. begin
  1377. p.concat(Tai_symbol.Createname(s,0));
  1378. end;
  1379. Procedure ConcatGlobalBss(const s : string;size : longint);
  1380. {*********************************************************************}
  1381. { PROCEDURE ConcatGlobalBss }
  1382. { Description: This routine emits an global datablock to the }
  1383. { linked list of instructions. }
  1384. {*********************************************************************}
  1385. begin
  1386. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1387. end;
  1388. Procedure ConcatLocalBss(const s : string;size : longint);
  1389. {*********************************************************************}
  1390. { PROCEDURE ConcatLocalBss }
  1391. { Description: This routine emits a local datablcok to the }
  1392. { linked list of instructions. }
  1393. {*********************************************************************}
  1394. begin
  1395. bssSegment.concat(Tai_datablock.Create(s,size));
  1396. end;
  1397. end.
  1398. {
  1399. $Log$
  1400. Revision 1.62 2003-05-30 23:57:08 peter
  1401. * more sparc cleanup
  1402. * accumulator removed, splitted in function_return_reg (called) and
  1403. function_result_reg (caller)
  1404. Revision 1.61 2003/05/25 08:55:49 peter
  1405. * load result using hidden parameter
  1406. Revision 1.60 2003/05/15 18:58:53 peter
  1407. * removed selfpointer_offset, vmtpointer_offset
  1408. * tvarsym.adjusted_address
  1409. * address in localsymtable is now in the real direction
  1410. * removed some obsolete globals
  1411. Revision 1.59 2003/05/12 17:22:00 jonas
  1412. * fixed (last?) remaining -tvarsym(X).address to
  1413. tg.direction*tvarsym(X).address...
  1414. Revision 1.58 2003/04/27 11:21:34 peter
  1415. * aktprocdef renamed to current_procdef
  1416. * procinfo renamed to current_procinfo
  1417. * procinfo will now be stored in current_module so it can be
  1418. cleaned up properly
  1419. * gen_main_procsym changed to create_main_proc and release_main_proc
  1420. to also generate a tprocinfo structure
  1421. * fixed unit implicit initfinal
  1422. Revision 1.57 2003/04/27 07:29:51 peter
  1423. * current_procdef cleanup, current_procdef is now always nil when parsing
  1424. a new procdef declaration
  1425. * aktprocsym removed
  1426. * lexlevel removed, use symtable.symtablelevel instead
  1427. * implicit init/final code uses the normal genentry/genexit
  1428. * funcret state checking updated for new funcret handling
  1429. Revision 1.56 2003/04/25 20:59:34 peter
  1430. * removed funcretn,funcretsym, function result is now in varsym
  1431. and aliases for result and function name are added using absolutesym
  1432. * vs_hidden parameter for funcret passed in parameter
  1433. * vs_hidden fixes
  1434. * writenode changed to printnode and released from extdebug
  1435. * -vp option added to generate a tree.log with the nodetree
  1436. * nicer printnode for statements, callnode
  1437. Revision 1.55 2003/04/06 21:11:23 olle
  1438. * changed newasmsymbol to newasmsymboldata for data symbols
  1439. Revision 1.54 2003/03/28 19:16:57 peter
  1440. * generic constructor working for i386
  1441. * remove fixed self register
  1442. * esi added as address register for i386
  1443. Revision 1.53 2003/02/19 22:00:14 daniel
  1444. * Code generator converted to new register notation
  1445. - Horribily outdated todo.txt removed
  1446. Revision 1.52 2003/01/08 18:43:56 daniel
  1447. * Tregister changed into a record
  1448. Revision 1.51 2002/12/14 15:02:03 carl
  1449. * maxoperands -> max_operands (for portability in rautils.pas)
  1450. * fix some range-check errors with loadconst
  1451. + add ncgadd unit to m68k
  1452. * some bugfix of a_param_reg with LOC_CREFERENCE
  1453. Revision 1.50 2002/11/25 17:43:23 peter
  1454. * splitted defbase in defutil,symutil,defcmp
  1455. * merged isconvertable and is_equal into compare_defs(_ext)
  1456. * made operator search faster by walking the list only once
  1457. Revision 1.49 2002/11/22 22:48:10 carl
  1458. * memory optimization with tconstsym (1.5%)
  1459. Revision 1.48 2002/11/18 17:31:59 peter
  1460. * pass proccalloption to ret_in_xxx and push_xxx functions
  1461. Revision 1.47 2002/11/15 16:29:31 peter
  1462. * made tasmsymbol.refs private (merged)
  1463. Revision 1.46 2002/09/03 16:26:27 daniel
  1464. * Make Tprocdef.defs protected
  1465. Revision 1.45 2002/08/25 19:25:20 peter
  1466. * sym.insert_in_data removed
  1467. * symtable.insertvardata/insertconstdata added
  1468. * removed insert_in_data call from symtable.insert, it needs to be
  1469. called separatly. This allows to deref the address calculation
  1470. * procedures now calculate the parast addresses after the procedure
  1471. directives are parsed. This fixes the cdecl parast problem
  1472. * push_addr_param has an extra argument that specifies if cdecl is used
  1473. or not
  1474. Revision 1.44 2002/08/17 09:23:41 florian
  1475. * first part of procinfo rewrite
  1476. Revision 1.43 2002/08/16 14:24:59 carl
  1477. * issameref() to test if two references are the same (then emit no opcodes)
  1478. + ret_in_reg to replace ret_in_acc
  1479. (fix some register allocation bugs at the same time)
  1480. + save_std_register now has an extra parameter which is the
  1481. usedinproc registers
  1482. Revision 1.42 2002/08/13 18:01:52 carl
  1483. * rename swatoperands to swapoperands
  1484. + m68k first compilable version (still needs a lot of testing):
  1485. assembler generator, system information , inline
  1486. assembler reader.
  1487. Revision 1.41 2002/08/12 15:08:40 carl
  1488. + stab register indexes for powerpc (moved from gdb to cpubase)
  1489. + tprocessor enumeration moved to cpuinfo
  1490. + linker in target_info is now a class
  1491. * many many updates for m68k (will soon start to compile)
  1492. - removed some ifdef or correct them for correct cpu
  1493. Revision 1.40 2002/08/11 14:32:27 peter
  1494. * renamed current_library to objectlibrary
  1495. Revision 1.39 2002/08/11 13:24:13 peter
  1496. * saving of asmsymbols in ppu supported
  1497. * asmsymbollist global is removed and moved into a new class
  1498. tasmlibrarydata that will hold the info of a .a file which
  1499. corresponds with a single module. Added librarydata to tmodule
  1500. to keep the library info stored for the module. In the future the
  1501. objectfiles will also be stored to the tasmlibrarydata class
  1502. * all getlabel/newasmsymbol and friends are moved to the new class
  1503. Revision 1.38 2002/07/20 11:57:57 florian
  1504. * types.pas renamed to defbase.pas because D6 contains a types
  1505. unit so this would conflicts if D6 programms are compiled
  1506. + Willamette/SSE2 instructions to assembler added
  1507. Revision 1.37 2002/07/11 14:41:28 florian
  1508. * start of the new generic parameter handling
  1509. Revision 1.36 2002/07/01 18:46:25 peter
  1510. * internal linker
  1511. * reorganized aasm layer
  1512. Revision 1.35 2002/05/18 13:34:17 peter
  1513. * readded missing revisions
  1514. Revision 1.34 2002/05/16 19:46:44 carl
  1515. + defines.inc -> fpcdefs.inc to avoid conflicts if compiling by hand
  1516. + try to fix temp allocation (still in ifdef)
  1517. + generic constructor calls
  1518. + start of tassembler / tmodulebase class cleanup
  1519. Revision 1.31 2002/05/12 16:53:10 peter
  1520. * moved entry and exitcode to ncgutil and cgobj
  1521. * foreach gets extra argument for passing local data to the
  1522. iterator function
  1523. * -CR checks also class typecasts at runtime by changing them
  1524. into as
  1525. * fixed compiler to cycle with the -CR option
  1526. * fixed stabs with elf writer, finally the global variables can
  1527. be watched
  1528. * removed a lot of routines from cga unit and replaced them by
  1529. calls to cgobj
  1530. * u32bit-s32bit updates for and,or,xor nodes. When one element is
  1531. u32bit then the other is typecasted also to u32bit without giving
  1532. a rangecheck warning/error.
  1533. * fixed pascal calling method with reversing also the high tree in
  1534. the parast, detected by tcalcst3 test
  1535. Revision 1.30 2002/04/20 21:32:24 carl
  1536. + generic FPC_CHECKPOINTER
  1537. + first parameter offset in stack now portable
  1538. * rename some constants
  1539. + move some cpu stuff to other units
  1540. - remove unused constents
  1541. * fix stacksize for some targets
  1542. * fix generic size problems which depend now on EXTEND_SIZE constant
  1543. Revision 1.29 2002/04/15 19:02:35 carl
  1544. + target_info.size_of_pointer -> pointer_Size
  1545. Revision 1.28 2002/04/02 17:11:29 peter
  1546. * tlocation,treference update
  1547. * LOC_CONSTANT added for better constant handling
  1548. * secondadd splitted in multiple routines
  1549. * location_force_reg added for loading a location to a register
  1550. of a specified size
  1551. * secondassignment parses now first the right and then the left node
  1552. (this is compatible with Kylix). This saves a lot of push/pop especially
  1553. with string operations
  1554. * adapted some routines to use the new cg methods
  1555. Revision 1.27 2002/01/29 21:32:03 peter
  1556. * allow accessing locals in other lexlevel when the current assembler
  1557. routine doesn't have locals.
  1558. Revision 1.26 2002/01/24 18:25:50 peter
  1559. * implicit result variable generation for assembler routines
  1560. * removed m_tp modeswitch, use m_tp7 or not(m_fpc) instead
  1561. }