rautils.pas 47 KB

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