rautils.pas 45 KB

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