rautils.pas 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665
  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. {$i defines.inc}
  20. Interface
  21. Uses
  22. cutils,cclasses,
  23. globtype,aasm,cpubase,
  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. maxoperands = 3; { Maximum operands for assembler instructions }
  29. {---------------------------------------------------------------------
  30. Local Label Management
  31. ---------------------------------------------------------------------}
  32. Type
  33. { Each local label has this structure associated with it }
  34. TLocalLabel = class(TNamedIndexItem)
  35. Emitted : boolean;
  36. constructor Create(const n:string);
  37. function Gettasmlabel:tasmlabel;
  38. private
  39. lab : tasmlabel;
  40. end;
  41. TLocalLabelList = class(TDictionary)
  42. procedure CheckEmitted;
  43. end;
  44. var
  45. LocalLabelList : TLocalLabelList;
  46. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  47. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  48. {---------------------------------------------------------------------
  49. Instruction management
  50. ---------------------------------------------------------------------}
  51. type
  52. TOprType=(OPR_NONE,OPR_CONSTANT,OPR_SYMBOL,
  53. OPR_REFERENCE,OPR_REGISTER,OPR_REGLIST);
  54. TOprRec = record
  55. case typ:TOprType of
  56. OPR_NONE : ();
  57. OPR_CONSTANT : (val:longint);
  58. OPR_SYMBOL : (symbol:tasmsymbol;symofs:longint);
  59. OPR_REFERENCE : (ref:treference);
  60. OPR_REGISTER : (reg:tregister);
  61. {$ifdef m68k}
  62. OPR_REGLIST : (reglist:pregisterlist);
  63. {$else not m68k}
  64. OPR_REGLIST : ();
  65. {$endif m68k}
  66. end;
  67. TOperand = class
  68. size : topsize;
  69. hastype, { if the operand has typecasted variable }
  70. hasvar : boolean; { if the operand is loaded with a variable }
  71. opr : TOprRec;
  72. constructor create;
  73. destructor destroy;override;
  74. Procedure BuildOperand;virtual;
  75. Procedure SetSize(_size:longint;force:boolean);
  76. Procedure SetCorrectSize(opcode:tasmop);virtual;
  77. Function SetupResult:boolean;virtual;
  78. Function SetupSelf:boolean;
  79. Function SetupOldEBP:boolean;
  80. Function SetupVar(const s:string;GetOffset : boolean): Boolean;
  81. Function SetupDirectVar(const hs:string): Boolean;
  82. Procedure InitRef;
  83. end;
  84. TInstruction = class
  85. opcode : tasmop;
  86. opsize : topsize;
  87. condition : tasmcond;
  88. ops : byte;
  89. labeled : boolean;
  90. operands : array[1..maxoperands] of toperand;
  91. constructor create;
  92. destructor destroy;override;
  93. Procedure InitOperands;virtual;
  94. Procedure BuildOpcode;virtual;
  95. procedure ConcatInstruction(p:TAAsmoutput);virtual;
  96. Procedure Swatoperands;
  97. end;
  98. {---------------------------------------------------------------------}
  99. { Expression parser types }
  100. {---------------------------------------------------------------------}
  101. TExprOperator = record
  102. ch: char; { operator }
  103. is_prefix: boolean; { was it a prefix, possible prefixes are +,- and not }
  104. end;
  105. String15 = String[15];
  106. {**********************************************************************}
  107. { The following operators are supported: }
  108. { '+' : addition }
  109. { '-' : subtraction }
  110. { '*' : multiplication }
  111. { '/' : modulo division }
  112. { '^' : exclusive or }
  113. { '<' : shift left }
  114. { '>' : shift right }
  115. { '&' : bitwise and }
  116. { '|' : bitwise or }
  117. { '~' : bitwise complement }
  118. { '%' : modulo division }
  119. { nnn: longint numbers }
  120. { ( and ) parenthesis }
  121. {**********************************************************************}
  122. TExprParse = class
  123. public
  124. Constructor create;
  125. Destructor Destroy;override;
  126. Function Evaluate(Expr: String): longint;
  127. Function Priority(_Operator: Char): longint;
  128. private
  129. RPNStack : Array[1..RPNMax] of longint; { Stack For RPN calculator }
  130. RPNTop : longint;
  131. OpStack : Array[1..OpMax] of TExprOperator; { Operator stack For conversion }
  132. OpTop : longint;
  133. Procedure RPNPush(Num: Longint);
  134. Function RPNPop: Longint;
  135. Procedure RPNCalc(token: String15; prefix: boolean);
  136. Procedure OpPush(_Operator: char; prefix: boolean);
  137. { In reality returns TExprOperaotr }
  138. Procedure OpPop(var _Operator:TExprOperator);
  139. end;
  140. { Evaluate an expression string to a longint }
  141. Function CalculateExpression(const expression: string): longint;
  142. {---------------------------------------------------------------------}
  143. { String routines }
  144. {---------------------------------------------------------------------}
  145. Function ValDecimal(const S:String):longint;
  146. Function ValOctal(const S:String):longint;
  147. Function ValBinary(const S:String):longint;
  148. Function ValHexaDecimal(const S:String):longint;
  149. Function PadZero(Var s: String; n: byte): Boolean;
  150. Function EscapeToPascal(const s:string): string;
  151. {---------------------------------------------------------------------
  152. Symbol helper routines
  153. ---------------------------------------------------------------------}
  154. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  155. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  156. Function SearchType(const hs:string): 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 : TAAsmoutput;s:string);
  163. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  164. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  165. Procedure ConcatConstant(p : TAAsmoutput;value: longint; maxvalue: longint);
  166. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  167. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  168. Procedure ConcatString(p : TAAsmoutput;s:string);
  169. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  170. Procedure ConcatPublic(p:TAAsmoutput;const s : string);
  171. Procedure ConcatLocal(p:TAAsmoutput;const s : string);
  172. Procedure ConcatGlobalBss(const s : string;size : longint);
  173. Procedure ConcatLocalBss(const s : string;size : longint);
  174. Implementation
  175. uses
  176. {$ifdef delphi}
  177. sysutils,
  178. {$else}
  179. strings,
  180. {$endif}
  181. types,systems,verbose,globals,
  182. symsym,symtable,cpuasm,
  183. cgbase;
  184. {*************************************************************************
  185. TExprParse
  186. *************************************************************************}
  187. Constructor TExprParse.create;
  188. Begin
  189. end;
  190. Procedure TExprParse.RPNPush(Num : longint);
  191. { Add an operand to the top of the RPN stack }
  192. begin
  193. if RPNTop < RPNMax then
  194. begin
  195. Inc(RPNTop);
  196. RPNStack[RPNTop]:=Num;
  197. end
  198. else
  199. Message(asmr_e_expr_illegal);
  200. end;
  201. Function TExprParse.RPNPop : longint;
  202. { Get the operand at the top of the RPN stack }
  203. begin
  204. if RPNTop > 0 then
  205. begin
  206. RPNPop:=RPNStack[RPNTop];
  207. Dec(RPNTop);
  208. end
  209. else
  210. Message(asmr_e_expr_illegal);
  211. end;
  212. Procedure TExprParse.RPNCalc(Token : String15; prefix:boolean); { RPN Calculator }
  213. Var
  214. Temp : longint;
  215. n1,n2 : longint;
  216. LocalError : Integer;
  217. begin
  218. { Handle operators }
  219. if (Length(Token) = 1) and (Token[1] in ['+', '-', '*', '/','&','|','%','^','~','<','>']) then
  220. Case Token[1] of
  221. '+' :
  222. Begin
  223. if not prefix then
  224. RPNPush(RPNPop + RPNPop);
  225. end;
  226. '-' :
  227. Begin
  228. if prefix then
  229. RPNPush(-(RPNPop))
  230. else
  231. begin
  232. n1:=RPNPop;
  233. n2:=RPNPop;
  234. RPNPush(n2 - n1);
  235. end;
  236. end;
  237. '*' : RPNPush(RPNPop * RPNPop);
  238. '&' :
  239. begin
  240. n1:=RPNPop;
  241. n2:=RPNPop;
  242. RPNPush(n2 and n1);
  243. end;
  244. '|' :
  245. begin
  246. n1:=RPNPop;
  247. n2:=RPNPop;
  248. RPNPush(n2 or n1);
  249. end;
  250. '~' : RPNPush(NOT RPNPop);
  251. '<' :
  252. begin
  253. n1:=RPNPop;
  254. n2:=RPNPop;
  255. RPNPush(n2 SHL n1);
  256. end;
  257. '>' :
  258. begin
  259. n1:=RPNPop;
  260. n2:=RPNPop;
  261. RPNPush(n2 SHR n1);
  262. end;
  263. '%' :
  264. begin
  265. Temp:=RPNPop;
  266. if Temp <> 0 then
  267. RPNPush(RPNPop mod Temp)
  268. else
  269. begin
  270. Message(asmr_e_expr_zero_divide);
  271. { push 1 for error recovery }
  272. RPNPush(1);
  273. end;
  274. end;
  275. '^' : RPNPush(RPNPop XOR RPNPop);
  276. '/' :
  277. begin
  278. Temp:=RPNPop;
  279. if Temp <> 0 then
  280. RPNPush(RPNPop div Temp)
  281. else
  282. begin
  283. Message(asmr_e_expr_zero_divide);
  284. { push 1 for error recovery }
  285. RPNPush(1);
  286. end;
  287. end;
  288. end
  289. else
  290. begin
  291. { Convert String to number and add to stack }
  292. Val(Token, Temp, LocalError);
  293. if LocalError = 0 then
  294. RPNPush(Temp)
  295. else
  296. begin
  297. Message(asmr_e_expr_illegal);
  298. { push 1 for error recovery }
  299. RPNPush(1);
  300. end;
  301. end;
  302. end;
  303. Procedure TExprParse.OpPush(_Operator : char;prefix: boolean);
  304. { Add an operator onto top of the stack }
  305. begin
  306. if OpTop < OpMax then
  307. begin
  308. Inc(OpTop);
  309. OpStack[OpTop].ch:=_Operator;
  310. OpStack[OpTop].is_prefix:=prefix;
  311. end
  312. else
  313. Message(asmr_e_expr_illegal);
  314. end;
  315. Procedure TExprParse.OpPop(var _Operator:TExprOperator);
  316. { Get operator at the top of the stack }
  317. begin
  318. if OpTop > 0 then
  319. begin
  320. _Operator:=OpStack[OpTop];
  321. Dec(OpTop);
  322. end
  323. else
  324. Message(asmr_e_expr_illegal);
  325. end;
  326. Function TExprParse.Priority(_Operator : Char) : longint;
  327. { Return priority of operator }
  328. { The greater the priority, the higher the precedence }
  329. begin
  330. Case _Operator OF
  331. '(' :
  332. Priority:=0;
  333. '+', '-' :
  334. Priority:=1;
  335. '*', '/','%','<','>' :
  336. Priority:=2;
  337. '|','&','^','~' :
  338. Priority:=0;
  339. else
  340. Message(asmr_e_expr_illegal);
  341. end;
  342. end;
  343. Function TExprParse.Evaluate(Expr : String):longint;
  344. Var
  345. I : LongInt;
  346. Token : String15;
  347. opr : TExprOperator;
  348. begin
  349. Evaluate:=0;
  350. { Reset stacks }
  351. OpTop :=0;
  352. RPNTop:=0;
  353. Token :='';
  354. { nothing to do ? }
  355. if Expr='' then
  356. exit;
  357. For I:=1 to Length(Expr) DO
  358. begin
  359. if Expr[I] in ['0'..'9'] then
  360. begin { Build multi-digit numbers }
  361. Token:=Token + Expr[I];
  362. if I = Length(Expr) then { Send last one to calculator }
  363. RPNCalc(Token,false);
  364. end
  365. else
  366. if Expr[I] in ['+', '-', '*', '/', '(', ')','^','&','|','%','~','<','>'] then
  367. begin
  368. if Token <> '' then
  369. begin { Send last built number to calc. }
  370. RPNCalc(Token,false);
  371. Token:='';
  372. end;
  373. Case Expr[I] OF
  374. '(' : OpPush('(',false);
  375. ')' : begin
  376. While OpStack[OpTop].ch <> '(' DO
  377. Begin
  378. OpPop(opr);
  379. RPNCalc(opr.ch,opr.is_prefix);
  380. end;
  381. OpPop(opr); { Pop off and ignore the '(' }
  382. end;
  383. '+','-','~' : Begin
  384. { workaround for -2147483648 }
  385. if (expr[I]='-') and (expr[i+1] in ['0'..'9']) then
  386. begin
  387. token:='-';
  388. expr[i]:='+';
  389. end;
  390. { if start of expression then surely a prefix }
  391. { or if previous char was also an operator }
  392. if (I = 1) or (not (Expr[I-1] in ['0'..'9','(',')'])) then
  393. OpPush(Expr[I],true)
  394. else
  395. Begin
  396. { Evaluate all higher priority operators }
  397. While (OpTop > 0) AND (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  398. Begin
  399. OpPop(opr);
  400. RPNCalc(opr.ch,opr.is_prefix);
  401. end;
  402. OpPush(Expr[I],false);
  403. End;
  404. end;
  405. '*', '/',
  406. '^','|','&',
  407. '%','<','>' : begin
  408. While (OpTop > 0) and (Priority(Expr[I]) <= Priority(OpStack[OpTop].ch)) DO
  409. Begin
  410. OpPop(opr);
  411. RPNCalc(opr.ch,opr.is_prefix);
  412. end;
  413. OpPush(Expr[I],false);
  414. end;
  415. end; { Case }
  416. end
  417. else
  418. Message(asmr_e_expr_illegal); { Handle bad input error }
  419. end;
  420. { Pop off the remaining operators }
  421. While OpTop > 0 do
  422. Begin
  423. OpPop(opr);
  424. RPNCalc(opr.ch,opr.is_prefix);
  425. end;
  426. { The result is stored on the top of the stack }
  427. Evaluate:=RPNPop;
  428. end;
  429. Destructor TExprParse.Destroy;
  430. Begin
  431. end;
  432. Function CalculateExpression(const expression: string): longint;
  433. var
  434. expr: TExprParse;
  435. Begin
  436. expr:=TExprParse.create;
  437. CalculateExpression:=expr.Evaluate(expression);
  438. expr.Free;
  439. end;
  440. {*************************************************************************}
  441. { String conversions/utils }
  442. {*************************************************************************}
  443. Function EscapeToPascal(const s:string): string;
  444. { converts a C styled string - which contains escape }
  445. { characters to a pascal style string. }
  446. var
  447. i,len : longint;
  448. hs : string;
  449. temp : string;
  450. c : char;
  451. Begin
  452. hs:='';
  453. len:=0;
  454. i:=0;
  455. while (i<length(s)) and (len<255) do
  456. begin
  457. Inc(i);
  458. if (s[i]='\') and (i<length(s)) then
  459. Begin
  460. inc(i);
  461. case s[i] of
  462. '\' :
  463. c:='\';
  464. 'b':
  465. c:=#8;
  466. 'f':
  467. c:=#12;
  468. 'n':
  469. c:=#10;
  470. 'r':
  471. c:=#13;
  472. 't':
  473. c:=#9;
  474. '"':
  475. c:='"';
  476. '0'..'7':
  477. Begin
  478. temp:=s[i];
  479. temp:=temp+s[i+1];
  480. temp:=temp+s[i+2];
  481. inc(i,2);
  482. c:=chr(ValOctal(temp));
  483. end;
  484. 'x':
  485. Begin
  486. temp:=s[i+1];
  487. temp:=temp+s[i+2];
  488. inc(i,2);
  489. c:=chr(ValHexaDecimal(temp));
  490. end;
  491. else
  492. Begin
  493. Message1(asmr_e_escape_seq_ignored,s[i]);
  494. c:=s[i];
  495. end;
  496. end;
  497. end
  498. else
  499. c:=s[i];
  500. inc(len);
  501. hs[len]:=c;
  502. end;
  503. hs[0]:=chr(len);
  504. EscapeToPascal:=hs;
  505. end;
  506. Function ValDecimal(const S:String):longint;
  507. { Converts a decimal 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*10;
  515. if s[c] in ['0'..'9'] then
  516. inc(vs,ord(s[c])-ord('0'))
  517. else
  518. begin
  519. Message1(asmr_e_error_converting_decimal,s);
  520. ValDecimal:=0;
  521. exit;
  522. end;
  523. end;
  524. ValDecimal:=vs;
  525. end;
  526. Function ValOctal(const S:String):longint;
  527. { Converts an octal 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 3;
  535. if s[c] in ['0'..'7'] then
  536. inc(vs,ord(s[c])-ord('0'))
  537. else
  538. begin
  539. Message1(asmr_e_error_converting_octal,s);
  540. ValOctal:=0;
  541. exit;
  542. end;
  543. end;
  544. ValOctal:=vs;
  545. end;
  546. Function ValBinary(const S:String):longint;
  547. { Converts a binary string to longint }
  548. var
  549. vs,c : longint;
  550. Begin
  551. vs:=0;
  552. for c:=1 to length(s) do
  553. begin
  554. vs:=vs shl 1;
  555. if s[c] in ['0'..'1'] then
  556. inc(vs,ord(s[c])-ord('0'))
  557. else
  558. begin
  559. Message1(asmr_e_error_converting_binary,s);
  560. ValBinary:=0;
  561. exit;
  562. end;
  563. end;
  564. ValBinary:=vs;
  565. end;
  566. Function ValHexadecimal(const S:String):longint;
  567. { Converts a binary string to longint }
  568. var
  569. vs,c : longint;
  570. Begin
  571. vs:=0;
  572. for c:=1 to length(s) do
  573. begin
  574. vs:=vs shl 4;
  575. case s[c] of
  576. '0'..'9' :
  577. inc(vs,ord(s[c])-ord('0'));
  578. 'A'..'F' :
  579. inc(vs,ord(s[c])-ord('A')+10);
  580. 'a'..'f' :
  581. inc(vs,ord(s[c])-ord('a')+10);
  582. else
  583. begin
  584. Message1(asmr_e_error_converting_hexadecimal,s);
  585. ValHexadecimal:=0;
  586. exit;
  587. end;
  588. end;
  589. end;
  590. ValHexadecimal:=vs;
  591. end;
  592. Function PadZero(Var s: String; n: byte): Boolean;
  593. Begin
  594. PadZero:=TRUE;
  595. { Do some error checking first }
  596. if Length(s) = n then
  597. exit
  598. else
  599. if Length(s) > n then
  600. Begin
  601. PadZero:=FALSE;
  602. delete(s,n+1,length(s));
  603. exit;
  604. end
  605. else
  606. PadZero:=TRUE;
  607. { Fill it up with the specified character }
  608. fillchar(s[length(s)+1],n-1,#0);
  609. s[0]:=chr(n);
  610. end;
  611. {****************************************************************************
  612. TOperand
  613. ****************************************************************************}
  614. constructor TOperand.Create;
  615. begin
  616. size:=S_NO;
  617. hastype:=false;
  618. hasvar:=false;
  619. FillChar(Opr,sizeof(Opr),0);
  620. end;
  621. destructor TOperand.destroy;
  622. begin
  623. end;
  624. Procedure TOperand.SetCorrectSize(opcode:tasmop);
  625. begin
  626. end;
  627. Procedure TOperand.SetSize(_size:longint;force:boolean);
  628. begin
  629. if force or
  630. ((size = S_NO) and (_size<=extended_size)) then
  631. Begin
  632. case _size of
  633. 1 : size:=S_B;
  634. 2 : size:=S_W{ could be S_IS};
  635. 4 : size:=S_L{ could be S_IL or S_FS};
  636. 8 : size:=S_IQ{ could be S_D or S_FL};
  637. extended_size : size:=S_FX;
  638. end;
  639. end;
  640. end;
  641. Function TOperand.SetupResult:boolean;
  642. Begin
  643. SetupResult:=false;
  644. { replace by correct offset. }
  645. if (not is_void(aktprocsym.definition.rettype.def)) then
  646. begin
  647. if (procinfo^.return_offset=0) and ((m_tp in aktmodeswitches) or
  648. (m_delphi in aktmodeswitches)) then
  649. begin
  650. Message(asmr_e_cannot_use_RESULT_here);
  651. exit;
  652. end;
  653. opr.ref.offset:=procinfo^.return_offset;
  654. opr.ref.base:= procinfo^.framepointer;
  655. opr.ref.options:=ref_parafixup;
  656. { always assume that the result is valid. }
  657. tfuncretsym(aktprocsym.definition.funcretsym).funcretstate:=vs_assigned;
  658. SetupResult:=true;
  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(procinfo^._class) then
  667. Begin
  668. opr.typ:=OPR_REFERENCE;
  669. opr.ref.offset:=procinfo^.selfpointer_offset;
  670. opr.ref.base:=procinfo^.framepointer;
  671. opr.ref.options:=ref_selffixup;
  672. SetupSelf:=true;
  673. end
  674. else
  675. Message(asmr_e_cannot_use_SELF_outside_a_method);
  676. end;
  677. Function TOperand.SetupOldEBP:boolean;
  678. Begin
  679. SetupOldEBP:=false;
  680. if lexlevel>normal_function_level then
  681. Begin
  682. opr.typ:=OPR_REFERENCE;
  683. opr.ref.offset:=procinfo^.framepointer_offset;
  684. opr.ref.base:=procinfo^.framepointer;
  685. opr.ref.options:=ref_parafixup;
  686. SetupOldEBP:=true;
  687. end
  688. else
  689. Message(asmr_e_cannot_use_OLDEBP_outside_nested_procedure);
  690. end;
  691. Function TOperand.SetupVar(const s:string;GetOffset : boolean): Boolean;
  692. { search and sets up the correct fields in the Instr record }
  693. { for the NON-constant identifier passed to the routine. }
  694. { if not found returns FALSE. }
  695. var
  696. sym : tsym;
  697. srsymtable : tsymtable;
  698. harrdef : tarraydef;
  699. l : longint;
  700. Begin
  701. SetupVar:=false;
  702. asmsearchsym(s,sym,srsymtable);
  703. if sym = nil then
  704. exit;
  705. case sym.typ of
  706. varsym :
  707. begin
  708. { we always assume in asm statements that }
  709. { that the variable is valid. }
  710. tvarsym(sym).varstate:=vs_used;
  711. inc(tvarsym(sym).refs);
  712. case tvarsym(sym).owner.symtabletype of
  713. objectsymtable :
  714. begin
  715. { this is not allowed, because we don't know if the self
  716. register is still free, and loading it first is also
  717. not possible, because this could break code }
  718. { Be TP/Delphi compatible in Delphi or TP modes }
  719. if (m_tp in aktmodeswitches) then
  720. begin
  721. opr.typ:=OPR_CONSTANT;
  722. opr.val:=tvarsym(sym).address;
  723. end
  724. { I do not agree here people using method vars should ensure
  725. that %esi is valid there }
  726. else
  727. begin
  728. opr.ref.base:=self_pointer;
  729. opr.ref.offset:=tvarsym(sym).address;
  730. end;
  731. hasvar:=true;
  732. SetupVar:=true;
  733. Exit;
  734. end;
  735. globalsymtable,
  736. staticsymtable :
  737. opr.ref.symbol:=newasmsymbol(tvarsym(sym).mangledname);
  738. parasymtable :
  739. begin
  740. { if we only want the offset we don't have to care
  741. the base will be zeroed after ! }
  742. if (lexlevel=tvarsym(sym).owner.symtablelevel) or
  743. { this below is wrong because there are two parast
  744. for global functions one of interface the second of
  745. implementation
  746. if (tvarsym(sym).owner=procinfo^.def.parast) or }
  747. GetOffset then
  748. begin
  749. opr.ref.base:=procinfo^.framepointer;
  750. end
  751. else
  752. begin
  753. if (procinfo^.framepointer=stack_pointer) and
  754. assigned(procinfo^.parent) and
  755. (lexlevel=tvarsym(sym).owner.symtablelevel+1) and
  756. { same problem as above !!
  757. (procinfo^.parent^.sym.definition.parast=tvarsym(sym).owner) and }
  758. (lexlevel>normal_function_level) then
  759. opr.ref.base:=procinfo^.parent^.framepointer
  760. else
  761. message1(asmr_e_local_para_unreachable,s);
  762. end;
  763. opr.ref.offset:=tvarsym(sym).address;
  764. if (lexlevel=tvarsym(sym).owner.symtablelevel) then
  765. begin
  766. opr.ref.offsetfixup:=aktprocsym.definition.parast.address_fixup;
  767. opr.ref.options:=ref_parafixup;
  768. end
  769. else
  770. begin
  771. opr.ref.offsetfixup:=0;
  772. opr.ref.options:=ref_none;
  773. end;
  774. if (tvarsym(sym).varspez=vs_var) or
  775. ((tvarsym(sym).varspez=vs_const) and
  776. push_addr_param(tvarsym(sym).vartype.def)) then
  777. SetSize(target_info.size_of_pointer,false);
  778. end;
  779. localsymtable :
  780. begin
  781. if (vo_is_external in tvarsym(sym).varoptions) then
  782. opr.ref.symbol:=newasmsymbol(tvarsym(sym).mangledname)
  783. else
  784. begin
  785. { if we only want the offset we don't have to care
  786. the base will be zeroed after ! }
  787. if (lexlevel=tvarsym(sym).owner.symtablelevel) or
  788. {if (tvarsym(sym).owner=procinfo^.def.localst) or}
  789. GetOffset then
  790. opr.ref.base:=procinfo^.framepointer
  791. else
  792. begin
  793. if (procinfo^.framepointer=stack_pointer) and
  794. assigned(procinfo^.parent) and
  795. (lexlevel=tvarsym(sym).owner.symtablelevel+1) and
  796. {(procinfo^.parent^.sym.definition.localst=tvarsym(sym).owner) and}
  797. (lexlevel>normal_function_level) then
  798. opr.ref.base:=procinfo^.parent^.framepointer
  799. else
  800. message1(asmr_e_local_para_unreachable,s);
  801. end;
  802. opr.ref.offset:=-(tvarsym(sym).address);
  803. if (lexlevel=tvarsym(sym).owner.symtablelevel) then
  804. begin
  805. opr.ref.offsetfixup:=aktprocsym.definition.localst.address_fixup;
  806. opr.ref.options:=ref_localfixup;
  807. end
  808. else
  809. begin
  810. opr.ref.offsetfixup:=0;
  811. opr.ref.options:=ref_none;
  812. end;
  813. end;
  814. if (tvarsym(sym).varspez in [vs_var,vs_out]) or
  815. ((tvarsym(sym).varspez=vs_const) and
  816. push_addr_param(tvarsym(sym).vartype.def)) then
  817. SetSize(target_info.size_of_pointer,false);
  818. end;
  819. end;
  820. case tvarsym(sym).vartype.def.deftype of
  821. orddef,
  822. enumdef,
  823. pointerdef,
  824. floatdef :
  825. SetSize(tvarsym(sym).getsize,false);
  826. arraydef :
  827. begin
  828. { for arrays try to get the element size, take care of
  829. multiple indexes }
  830. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  831. while assigned(harrdef.elementtype.def) and
  832. (harrdef.elementtype.def.deftype=arraydef) do
  833. harrdef:=tarraydef(harrdef.elementtype.def);
  834. SetSize(harrdef.elesize,false);
  835. end;
  836. end;
  837. hasvar:=true;
  838. SetupVar:=true;
  839. Exit;
  840. end;
  841. typedconstsym :
  842. begin
  843. opr.ref.symbol:=newasmsymbol(ttypedconstsym(sym).mangledname);
  844. case ttypedconstsym(sym).typedconsttype.def.deftype of
  845. orddef,
  846. enumdef,
  847. pointerdef,
  848. floatdef :
  849. SetSize(ttypedconstsym(sym).getsize,false);
  850. arraydef :
  851. begin
  852. { for arrays try to get the element size, take care of
  853. multiple indexes }
  854. harrdef:=tarraydef(ttypedconstsym(sym).typedconsttype.def);
  855. while assigned(harrdef.elementtype.def) and
  856. (harrdef.elementtype.def.deftype=arraydef) do
  857. harrdef:=tarraydef(harrdef.elementtype.def);
  858. SetSize(harrdef.elesize,false);
  859. end;
  860. end;
  861. hasvar:=true;
  862. SetupVar:=true;
  863. Exit;
  864. end;
  865. constsym :
  866. begin
  867. if tconstsym(sym).consttyp in [constint,constchar,constbool] then
  868. begin
  869. opr.typ:=OPR_CONSTANT;
  870. opr.val:=tconstsym(sym).value;
  871. SetupVar:=true;
  872. Exit;
  873. end;
  874. end;
  875. typesym :
  876. begin
  877. if ttypesym(sym).restype.def.deftype in [recorddef,objectdef] then
  878. begin
  879. opr.typ:=OPR_CONSTANT;
  880. opr.val:=0;
  881. SetupVar:=TRUE;
  882. Exit;
  883. end;
  884. end;
  885. procsym :
  886. begin
  887. if assigned(tprocsym(sym).definition.nextoverloaded) then
  888. Message(asmr_w_calling_overload_func);
  889. l:=opr.ref.offset;
  890. opr.typ:=OPR_SYMBOL;
  891. opr.symbol:=newasmsymbol(tprocsym(sym).definition.mangledname);
  892. opr.symofs:=l;
  893. hasvar:=true;
  894. SetupVar:=TRUE;
  895. Exit;
  896. end;
  897. else
  898. begin
  899. Message(asmr_e_unsupported_symbol_type);
  900. exit;
  901. end;
  902. end;
  903. end;
  904. { looks for internal names of variables and routines }
  905. Function TOperand.SetupDirectVar(const hs:string): Boolean;
  906. var
  907. p : tasmsymbol;
  908. begin
  909. SetupDirectVar:=false;
  910. p:=getasmsymbol(hs);
  911. if assigned(p) then
  912. begin
  913. opr.ref.symbol:=p;
  914. hasvar:=true;
  915. SetupDirectVar:=true;
  916. end;
  917. end;
  918. procedure TOperand.InitRef;
  919. {*********************************************************************}
  920. { Description: This routine first check if the opcode is of }
  921. { type OPR_NONE, or OPR_REFERENCE , if not it gives out an error. }
  922. { If the operandtype = OPR_NONE or <> OPR_REFERENCE then it sets up }
  923. { the operand type to OPR_REFERENCE, as well as setting up the ref }
  924. { to point to the default segment. }
  925. {*********************************************************************}
  926. Begin
  927. case opr.typ of
  928. OPR_REFERENCE:
  929. exit;
  930. OPR_NONE: ;
  931. else
  932. Message(asmr_e_invalid_operand_type);
  933. end;
  934. opr.typ := OPR_REFERENCE;
  935. reset_reference(opr.ref);
  936. end;
  937. procedure TOperand.BuildOperand;
  938. begin
  939. abstract;
  940. end;
  941. {****************************************************************************
  942. TInstruction
  943. ****************************************************************************}
  944. constructor TInstruction.create;
  945. Begin
  946. Opcode:=A_NONE;
  947. Opsize:=S_NO;
  948. Condition:=C_NONE;
  949. Ops:=0;
  950. InitOperands;
  951. Labeled:=false;
  952. end;
  953. destructor TInstruction.destroy;
  954. var
  955. i : longint;
  956. Begin
  957. for i:=1 to 3 do
  958. Operands[i].free;
  959. end;
  960. procedure TInstruction.InitOperands;
  961. var
  962. i : longint;
  963. begin
  964. for i:=1 to 3 do
  965. Operands[i].create;
  966. end;
  967. Procedure TInstruction.Swatoperands;
  968. Var
  969. p : toperand;
  970. Begin
  971. case Ops of
  972. 2 :
  973. begin
  974. p:=Operands[1];
  975. Operands[1]:=Operands[2];
  976. Operands[2]:=p;
  977. end;
  978. 3 :
  979. begin
  980. p:=Operands[1];
  981. Operands[1]:=Operands[3];
  982. Operands[3]:=p;
  983. end;
  984. end;
  985. end;
  986. procedure TInstruction.ConcatInstruction(p:TAAsmOutput);
  987. begin
  988. abstract;
  989. end;
  990. procedure TInstruction.BuildOpcode;
  991. begin
  992. abstract;
  993. end;
  994. {***************************************************************************
  995. TLocalLabel
  996. ***************************************************************************}
  997. constructor TLocalLabel.create(const n:string);
  998. begin
  999. inherited CreateName(n);
  1000. lab:=nil;
  1001. emitted:=false;
  1002. end;
  1003. function TLocalLabel.Gettasmlabel:tasmlabel;
  1004. begin
  1005. if not assigned(lab) then
  1006. begin
  1007. getlabel(lab);
  1008. { this label is forced to be used so it's always written }
  1009. inc(lab.refs);
  1010. end;
  1011. Gettasmlabel:=lab;
  1012. end;
  1013. {***************************************************************************
  1014. TLocalLabelList
  1015. ***************************************************************************}
  1016. procedure LocalLabelEmitted(p:tnamedindexitem);
  1017. begin
  1018. if not TLocalLabel(p).emitted then
  1019. Message1(asmr_e_unknown_label_identifier,p.name);
  1020. end;
  1021. procedure TLocalLabelList.CheckEmitted;
  1022. begin
  1023. ForEach_Static({$ifdef FPCPROCVAR}@{$endif}LocalLabelEmitted)
  1024. end;
  1025. function CreateLocalLabel(const s: string; var hl: tasmlabel; emit:boolean):boolean;
  1026. var
  1027. lab : TLocalLabel;
  1028. Begin
  1029. CreateLocalLabel:=true;
  1030. { Check if it already is defined }
  1031. lab:=TLocalLabel(LocalLabellist.Search(s));
  1032. if not assigned(lab) then
  1033. begin
  1034. lab:=TLocalLabel.Create(s);
  1035. LocalLabellist.Insert(lab);
  1036. end;
  1037. { set emitted flag and check for dup syms }
  1038. if emit then
  1039. begin
  1040. if lab.Emitted then
  1041. begin
  1042. Message1(asmr_e_dup_local_sym,lab.Name);
  1043. CreateLocalLabel:=false;
  1044. end;
  1045. lab.Emitted:=true;
  1046. end;
  1047. hl:=lab.Gettasmlabel;
  1048. end;
  1049. {****************************************************************************
  1050. Symbol table helper routines
  1051. ****************************************************************************}
  1052. procedure AsmSearchSym(const s:string;var srsym:tsym;var srsymtable:tsymtable);
  1053. var
  1054. i : integer;
  1055. begin
  1056. i:=pos('.',s);
  1057. { allow unit.identifier }
  1058. if i>0 then
  1059. begin
  1060. searchsym(Copy(s,1,i-1),srsym,srsymtable);
  1061. if assigned(srsym) then
  1062. begin
  1063. if (srsym.typ=unitsym) and
  1064. (srsym.owner.unitid=0) then
  1065. srsym:=searchsymonlyin(tunitsym(srsym).unitsymtable,Copy(s,i+1,255))
  1066. else
  1067. srsym:=nil;
  1068. end;
  1069. end
  1070. else
  1071. searchsym(s,srsym,srsymtable);
  1072. end;
  1073. Function SearchType(const hs:string): Boolean;
  1074. var
  1075. srsym : tsym;
  1076. srsymtable : tsymtable;
  1077. begin
  1078. asmsearchsym(hs,srsym,srsymtable);
  1079. SearchType:=assigned(srsym) and
  1080. (srsym.typ=typesym);
  1081. end;
  1082. Function SearchRecordType(const s:string): boolean;
  1083. var
  1084. srsym : tsym;
  1085. srsymtable : tsymtable;
  1086. Begin
  1087. SearchRecordType:=false;
  1088. { Check the constants in symtable }
  1089. asmsearchsym(s,srsym,srsymtable);
  1090. if srsym <> nil then
  1091. Begin
  1092. case srsym.typ of
  1093. typesym :
  1094. begin
  1095. if ttypesym(srsym).restype.def.deftype in [recorddef,objectdef] then
  1096. begin
  1097. SearchRecordType:=true;
  1098. exit;
  1099. end;
  1100. end;
  1101. end;
  1102. end;
  1103. end;
  1104. Function SearchIConstant(const s:string; var l:longint): boolean;
  1105. {**********************************************************************}
  1106. { Description: Searches for a CONSTANT of name s in either the local }
  1107. { symbol list, then in the global symbol list, and returns the value }
  1108. { of that constant in l. Returns TRUE if successfull, if not found, }
  1109. { or if the constant is not of correct type, then returns FALSE }
  1110. { Remarks: Also handle TRUE and FALSE returning in those cases 1 and 0 }
  1111. { respectively. }
  1112. {**********************************************************************}
  1113. var
  1114. srsym : tsym;
  1115. srsymtable : tsymtable;
  1116. Begin
  1117. SearchIConstant:=false;
  1118. { check for TRUE or FALSE reserved words first }
  1119. if s = 'TRUE' then
  1120. Begin
  1121. SearchIConstant:=TRUE;
  1122. l:=1;
  1123. exit;
  1124. end;
  1125. if s = 'FALSE' then
  1126. Begin
  1127. SearchIConstant:=TRUE;
  1128. l:=0;
  1129. exit;
  1130. end;
  1131. { Check the constants in symtable }
  1132. asmsearchsym(s,srsym,srsymtable);
  1133. if srsym <> nil then
  1134. Begin
  1135. case srsym.typ of
  1136. constsym :
  1137. begin
  1138. if (tconstsym(srsym).consttyp in [constord,constint,constchar,constbool]) then
  1139. Begin
  1140. l:=tconstsym(srsym).value;
  1141. SearchIConstant:=TRUE;
  1142. exit;
  1143. end;
  1144. end;
  1145. enumsym:
  1146. Begin
  1147. l:=tenumsym(srsym).value;
  1148. SearchIConstant:=TRUE;
  1149. exit;
  1150. end;
  1151. end;
  1152. end;
  1153. end;
  1154. Function GetRecordOffsetSize(s:string;Var Offset: longint;var Size:longint):boolean;
  1155. { search and returns the offset and size of records/objects of the base }
  1156. { with field name setup in field. }
  1157. { returns FALSE if not found. }
  1158. { used when base is a variable or a typed constant name. }
  1159. var
  1160. st : tsymtable;
  1161. harrdef : tarraydef;
  1162. sym : tsym;
  1163. srsymtable : tsymtable;
  1164. i : longint;
  1165. base : string;
  1166. Begin
  1167. GetRecordOffsetSize:=FALSE;
  1168. Offset:=0;
  1169. Size:=0;
  1170. i:=pos('.',s);
  1171. if i=0 then
  1172. i:=255;
  1173. base:=Copy(s,1,i-1);
  1174. delete(s,1,i);
  1175. if base='SELF' then
  1176. st:=procinfo^._class.symtable
  1177. else
  1178. begin
  1179. asmsearchsym(base,sym,srsymtable);
  1180. st:=nil;
  1181. { we can start with a var,type,typedconst }
  1182. case sym.typ of
  1183. varsym :
  1184. begin
  1185. case tvarsym(sym).vartype.def.deftype of
  1186. recorddef :
  1187. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1188. objectdef :
  1189. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1190. end;
  1191. end;
  1192. typesym :
  1193. begin
  1194. case ttypesym(sym).restype.def.deftype of
  1195. recorddef :
  1196. st:=trecorddef(ttypesym(sym).restype.def).symtable;
  1197. objectdef :
  1198. st:=tobjectdef(ttypesym(sym).restype.def).symtable;
  1199. end;
  1200. end;
  1201. typedconstsym :
  1202. begin
  1203. case ttypedconstsym(sym).typedconsttype.def.deftype of
  1204. recorddef :
  1205. st:=trecorddef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1206. objectdef :
  1207. st:=tobjectdef(ttypedconstsym(sym).typedconsttype.def).symtable;
  1208. end;
  1209. end;
  1210. end;
  1211. end;
  1212. { now walk all recordsymtables }
  1213. while assigned(st) and (s<>'') do
  1214. begin
  1215. { load next field in base }
  1216. i:=pos('.',s);
  1217. if i=0 then
  1218. i:=255;
  1219. base:=Copy(s,1,i-1);
  1220. delete(s,1,i);
  1221. if st.symtabletype=objectsymtable then
  1222. sym:=search_class_member(tobjectdef(st.defowner),base)
  1223. else
  1224. sym:=tsym(st.search(base));
  1225. if not assigned(sym) then
  1226. begin
  1227. GetRecordOffsetSize:=false;
  1228. exit;
  1229. end;
  1230. st:=nil;
  1231. case sym.typ of
  1232. varsym :
  1233. begin
  1234. inc(Offset,tvarsym(sym).address);
  1235. Size:=tvarsym(sym).getsize;
  1236. case tvarsym(sym).vartype.def.deftype of
  1237. arraydef :
  1238. begin
  1239. { for arrays try to get the element size, take care of
  1240. multiple indexes }
  1241. harrdef:=tarraydef(tvarsym(sym).vartype.def);
  1242. while assigned(harrdef.elementtype.def) and
  1243. (harrdef.elementtype.def.deftype=arraydef) do
  1244. harrdef:=tarraydef(harrdef.elementtype.def);
  1245. size:=harrdef.elesize;
  1246. end;
  1247. recorddef :
  1248. st:=trecorddef(tvarsym(sym).vartype.def).symtable;
  1249. objectdef :
  1250. st:=tobjectdef(tvarsym(sym).vartype.def).symtable;
  1251. end;
  1252. end;
  1253. end;
  1254. end;
  1255. GetRecordOffsetSize:=(s='');
  1256. end;
  1257. Function SearchLabel(const s: string; var hl: tasmlabel;emit:boolean): boolean;
  1258. var
  1259. sym : tsym;
  1260. srsymtable : tsymtable;
  1261. hs : string;
  1262. Begin
  1263. hl:=nil;
  1264. SearchLabel:=false;
  1265. { Check for pascal labels, which are case insensetive }
  1266. hs:=upper(s);
  1267. asmsearchsym(hs,sym,srsymtable);
  1268. if sym=nil then
  1269. exit;
  1270. case sym.typ of
  1271. labelsym :
  1272. begin
  1273. hl:=tlabelsym(sym).lab;
  1274. if emit then
  1275. tlabelsym(sym).defined:=true
  1276. else
  1277. tlabelsym(sym).used:=true;
  1278. SearchLabel:=true;
  1279. exit;
  1280. end;
  1281. end;
  1282. end;
  1283. {*************************************************************************}
  1284. { Instruction Generation Utilities }
  1285. {*************************************************************************}
  1286. Procedure ConcatString(p : TAAsmoutput;s:string);
  1287. {*********************************************************************}
  1288. { PROCEDURE ConcatString(s:string); }
  1289. { Description: This routine adds the character chain pointed to in }
  1290. { s to the instruction linked list. }
  1291. {*********************************************************************}
  1292. Var
  1293. pc: PChar;
  1294. Begin
  1295. getmem(pc,length(s)+1);
  1296. p.concat(Tai_string.Create_length_pchar(strpcopy(pc,s),length(s)));
  1297. end;
  1298. Procedure ConcatPasString(p : TAAsmoutput;s:string);
  1299. {*********************************************************************}
  1300. { PROCEDURE ConcatPasString(s:string); }
  1301. { Description: This routine adds the character chain pointed to in }
  1302. { s to the instruction linked list, contrary to ConcatString it }
  1303. { uses a pascal style string, so it conserves null characters. }
  1304. {*********************************************************************}
  1305. Begin
  1306. p.concat(Tai_string.Create(s));
  1307. end;
  1308. Procedure ConcatDirect(p : TAAsmoutput;s:string);
  1309. {*********************************************************************}
  1310. { PROCEDURE ConcatDirect(s:string) }
  1311. { Description: This routine output the string directly to the asm }
  1312. { output, it is only sed when writing special labels in AT&T mode, }
  1313. { and should not be used without due consideration, since it may }
  1314. { cause problems. }
  1315. {*********************************************************************}
  1316. Var
  1317. pc: PChar;
  1318. Begin
  1319. getmem(pc,length(s)+1);
  1320. p.concat(Tai_direct.Create(strpcopy(pc,s)));
  1321. end;
  1322. Procedure ConcatConstant(p: TAAsmoutput; value: longint; maxvalue: longint);
  1323. {*********************************************************************}
  1324. { PROCEDURE ConcatConstant(value: longint; maxvalue: longint); }
  1325. { Description: This routine adds the value constant to the current }
  1326. { instruction linked list. }
  1327. { maxvalue -> indicates the size of the data to initialize: }
  1328. { $ff -> create a byte node. }
  1329. { $ffff -> create a word node. }
  1330. { $ffffffff -> create a dword node. }
  1331. {*********************************************************************}
  1332. Begin
  1333. if (maxvalue <> longint($ffffffff)) and (value > maxvalue) then
  1334. Begin
  1335. Message(asmr_e_constant_out_of_bounds);
  1336. { assuming a value of maxvalue }
  1337. value:=maxvalue;
  1338. end;
  1339. if maxvalue = $ff then
  1340. p.concat(Tai_const.Create_8bit(byte(value)))
  1341. else
  1342. if maxvalue = $ffff then
  1343. p.concat(Tai_const.Create_16bit(word(value)))
  1344. else
  1345. if maxvalue = longint($ffffffff) then
  1346. p.concat(Tai_const.Create_32bit(longint(value)));
  1347. end;
  1348. Procedure ConcatConstSymbol(p : TAAsmoutput;const sym:string;l:longint);
  1349. begin
  1350. p.concat(Tai_const_symbol.Createname_offset(sym,l));
  1351. end;
  1352. Procedure ConcatRealConstant(p : TAAsmoutput;value: bestreal; real_typ : tfloattype);
  1353. {***********************************************************************}
  1354. { PROCEDURE ConcatRealConstant(value: bestreal; real_typ : tfloattype); }
  1355. { Description: This routine adds the value constant to the current }
  1356. { instruction linked list. }
  1357. { real_typ -> indicates the type of the real data to initialize: }
  1358. { s32real -> create a single node. }
  1359. { s64real -> create a double node. }
  1360. { s80real -> create an extended node. }
  1361. { s64bit -> create a comp node. }
  1362. { f32bit -> create a fixed node. (not used normally) }
  1363. {***********************************************************************}
  1364. Begin
  1365. case real_typ of
  1366. s32real : p.concat(Tai_real_32bit.Create(value));
  1367. s64real : p.concat(Tai_real_64bit.Create(value));
  1368. s80real : p.concat(Tai_real_80bit.Create(value));
  1369. s64comp : p.concat(Tai_comp_64bit.Create(value));
  1370. end;
  1371. end;
  1372. Procedure ConcatLabel(p: TAAsmoutput;var l : tasmlabel);
  1373. {*********************************************************************}
  1374. { PROCEDURE ConcatLabel }
  1375. { Description: This routine either emits a label or a labeled }
  1376. { instruction to the linked list of instructions. }
  1377. {*********************************************************************}
  1378. begin
  1379. p.concat(Tai_label.Create(l));
  1380. end;
  1381. procedure ConcatAlign(p:TAAsmoutput;l:longint);
  1382. {*********************************************************************}
  1383. { PROCEDURE ConcatPublic }
  1384. { Description: This routine emits an global definition to the }
  1385. { linked list of instructions.(used by AT&T styled asm) }
  1386. {*********************************************************************}
  1387. begin
  1388. p.concat(Tai_align.Create(l));
  1389. end;
  1390. procedure ConcatPublic(p:TAAsmoutput;const s : string);
  1391. {*********************************************************************}
  1392. { PROCEDURE ConcatPublic }
  1393. { Description: This routine emits an global definition to the }
  1394. { linked list of instructions.(used by AT&T styled asm) }
  1395. {*********************************************************************}
  1396. begin
  1397. p.concat(Tai_symbol.Createname_global(s,0));
  1398. end;
  1399. procedure ConcatLocal(p:TAAsmoutput;const s : string);
  1400. {*********************************************************************}
  1401. { PROCEDURE ConcatLocal }
  1402. { Description: This routine emits an local definition to the }
  1403. { linked list of instructions. }
  1404. {*********************************************************************}
  1405. begin
  1406. p.concat(Tai_symbol.Createname(s,0));
  1407. end;
  1408. Procedure ConcatGlobalBss(const s : string;size : longint);
  1409. {*********************************************************************}
  1410. { PROCEDURE ConcatGlobalBss }
  1411. { Description: This routine emits an global datablock to the }
  1412. { linked list of instructions. }
  1413. {*********************************************************************}
  1414. begin
  1415. bssSegment.concat(Tai_datablock.Create_global(s,size));
  1416. end;
  1417. Procedure ConcatLocalBss(const s : string;size : longint);
  1418. {*********************************************************************}
  1419. { PROCEDURE ConcatLocalBss }
  1420. { Description: This routine emits a local datablcok to the }
  1421. { linked list of instructions. }
  1422. {*********************************************************************}
  1423. begin
  1424. bssSegment.concat(Tai_datablock.Create(s,size));
  1425. end;
  1426. end.
  1427. {
  1428. $Log$
  1429. Revision 1.23 2001-08-26 13:36:48 florian
  1430. * some cg reorganisation
  1431. * some PPC updates
  1432. Revision 1.22 2001/08/12 17:57:07 peter
  1433. * under development flag for targets
  1434. Revision 1.21 2001/08/06 21:40:48 peter
  1435. * funcret moved from tprocinfo to tprocdef
  1436. Revision 1.20 2001/04/18 22:01:58 peter
  1437. * registration of targets and assemblers
  1438. Revision 1.19 2001/04/13 20:06:05 peter
  1439. * allow unit.identifier in asm readers
  1440. Revision 1.18 2001/04/13 01:22:13 peter
  1441. * symtable change to classes
  1442. * range check generation and errors fixed, make cycle DEBUG=1 works
  1443. * memory leaks fixed
  1444. Revision 1.17 2001/04/02 21:20:34 peter
  1445. * resulttype rewrite
  1446. Revision 1.16 2001/03/11 22:58:50 peter
  1447. * getsym redesign, removed the globals srsym,srsymtable
  1448. Revision 1.15 2001/02/26 19:44:54 peter
  1449. * merged generic m68k updates from fixes branch
  1450. Revision 1.14 2000/12/25 00:07:28 peter
  1451. + new tlinkedlist class (merge of old tstringqueue,tcontainer and
  1452. tlinkedlist objects)
  1453. Revision 1.13 2000/12/07 17:19:43 jonas
  1454. * new constant handling: from now on, hex constants >$7fffffff are
  1455. parsed as unsigned constants (otherwise, $80000000 got sign extended
  1456. and became $ffffffff80000000), all constants in the longint range
  1457. become longints, all constants >$7fffffff and <=cardinal($ffffffff)
  1458. are cardinals and the rest are int64's.
  1459. * added lots of longint typecast to prevent range check errors in the
  1460. compiler and rtl
  1461. * type casts of symbolic ordinal constants are now preserved
  1462. * fixed bug where the original resulttype wasn't restored correctly
  1463. after doing a 64bit rangecheck
  1464. Revision 1.12 2000/11/29 00:30:38 florian
  1465. * unused units removed from uses clause
  1466. * some changes for widestrings
  1467. Revision 1.11 2000/11/06 22:30:30 peter
  1468. * more fixes
  1469. Revision 1.10 2000/11/04 14:25:21 florian
  1470. + merged Attila's changes for interfaces, not tested yet
  1471. Revision 1.9 2000/10/31 22:30:13 peter
  1472. * merged asm result patch part 2
  1473. Revision 1.8 2000/10/31 22:02:51 peter
  1474. * symtable splitted, no real code changes
  1475. Revision 1.7 2000/10/08 10:26:33 peter
  1476. * merged @result fix from Pierre
  1477. Revision 1.6 2000/09/24 21:19:51 peter
  1478. * delphi compile fixes
  1479. Revision 1.5 2000/09/24 15:06:26 peter
  1480. * use defines.inc
  1481. Revision 1.4 2000/08/27 16:11:52 peter
  1482. * moved some util functions from globals,cobjects to cutils
  1483. * splitted files into finput,fmodule
  1484. Revision 1.3 2000/08/06 10:42:29 peter
  1485. * merged patches name generation in lib and asm constant eval
  1486. Revision 1.2 2000/07/13 11:32:48 michael
  1487. + removed logs
  1488. }