home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 26 / AACD 26.iso / AACD / Programming / ace_gpl_release / src / ace / c / memory.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-04  |  6.0 KB  |  264 lines

  1. /* << ACE >>
  2.  
  3.    -- Amiga BASIC Compiler --
  4.  
  5.    ** Parser: memory functions **
  6.    ** Copyright (C) 1998 David Benn
  7.    ** 
  8.    ** This program is free software; you can redistribute it and/or
  9.    ** modify it under the terms of the GNU General Public License
  10.    ** as published by the Free Software Foundation; either version 2
  11.    ** of the License, or (at your option) any later version.
  12.    **
  13.    ** This program is distributed in the hope that it will be useful,
  14.    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.    ** GNU General Public License for more details.
  17.    **
  18.    ** You should have received a copy of the GNU General Public License
  19.    ** along with this program; if not, write to the Free Software
  20.    ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  21.  
  22.    Author: David J Benn
  23.      Date: 13th,14th June 1993,
  24.        3rd July 1993
  25. */
  26.  
  27. #include "acedef.h"
  28.  
  29. /* locals */
  30. int    struct_member_type;  /* for SWAP -> filled by address_of_object() */
  31.  
  32. /* externals */
  33. extern    int    sym;
  34. extern    int    lev;
  35. extern     SYM    *curr_item;
  36. extern    char    id[MAXIDSIZE];
  37. extern    char    tempstrname[80];
  38.  
  39. /* -------------- */
  40. /* POKE functions */
  41. /* -------------- */
  42.  
  43. void poke()
  44. {
  45. int addrtype;
  46.  
  47.  /* get address */
  48.  insymbol();
  49.  addrtype=make_integer(expr());
  50.  if ((addrtype == longtype) || (addrtype == shorttype))
  51.  {
  52.   /* make sure address is long */
  53.   if (addrtype == shorttype)
  54.   {
  55.    gen("move.w","(sp)+","d0");
  56.    gen("ext.l","d0","  ");
  57.    gen("move.l","d0","-(sp)");
  58.   }
  59.   if (sym == comma)
  60.   { 
  61.    /* get value */
  62.    insymbol();
  63.    make_sure_short(expr());
  64.    gen("move.w","(sp)+","d0");   /* data to be poked */
  65.    gen("move.l","(sp)+","a0");   /* address */
  66.    gen("move.b","d0","(a0)");    /* poke (a0),d0 */
  67.   }
  68.  }
  69.  else _error(4);
  70. }
  71.  
  72. void pokew()
  73. {
  74. int addrtype;
  75.  
  76.  /* get address */
  77.  insymbol();
  78.  addrtype=make_integer(expr());
  79.  if ((addrtype == longtype) || (addrtype == shorttype))
  80.  {
  81.   /* make sure address is long */
  82.   if (addrtype == shorttype)
  83.   {
  84.    gen("move.w","(sp)+","d0");
  85.    gen("ext.l","d0","  ");
  86.    gen("move.l","d0","-(sp)");
  87.   }
  88.   if (sym == comma)
  89.   { 
  90.    /* get value */
  91.    insymbol();
  92.    make_sure_short(expr());
  93.    gen("move.w","(sp)+","d0");   /* data to be poked */
  94.    gen("move.l","(sp)+","a0");   /* address */
  95.    gen("move.w","d0","(a0)");    /* pokew (a0),d0 */
  96.   }
  97.  }
  98.  else _error(4);
  99. }
  100.  
  101. void pokel()
  102. {
  103. int addrtype,datatype;
  104.  
  105.  /* get address */
  106.  insymbol();
  107.  addrtype=make_integer(expr());
  108.  if ((addrtype == longtype) || (addrtype == shorttype))
  109.  {
  110.   /* make sure address is long */
  111.   if (addrtype == shorttype)
  112.   {
  113.    gen("move.w","(sp)+","d0");
  114.    gen("ext.l","d0","  ");
  115.    gen("move.l","d0","-(sp)");
  116.   }
  117.   if (sym == comma)
  118.   { 
  119.    /* get value */
  120.    insymbol();
  121.    datatype=make_integer(expr());
  122.    if ((datatype == shorttype) || (datatype == longtype))
  123.    {
  124.     /* coerce data to long */
  125.     if (datatype == shorttype)
  126.     {
  127.      gen("move.w","(sp)+","d0");
  128.      gen("ext.l","d0","  ");
  129.     }
  130.     else
  131.         gen("move.l","(sp)+","d0");   /* data to be poked */
  132.     gen("move.l","(sp)+","a0");       /* address */
  133.     gen("move.l","d0","(a0)");           /* pokel (a0),d0 */
  134.    }
  135.    else _error(4);
  136.   }
  137.  }
  138.  else _error(4);
  139. }
  140.  
  141. /* ---- */
  142. /* SWAP */
  143. /* ---- */
  144. void get_obj_info(objname,object,objtype)
  145. char *objname;
  146. int  *object;
  147. int  *objtype;
  148. {
  149. /* get info about a variable, extvar, array or structure for SWAP */
  150.  
  151. BOOL found=FALSE;
  152.  
  153.  if (exist(objname,variable))
  154.     found = TRUE;
  155.  else
  156.  if (exist(objname,extvar))
  157.     found = TRUE;
  158.  else
  159.  if (exist(objname,array))
  160.     found = TRUE;
  161.  else
  162.  if (exist(objname,structure))
  163.     found = TRUE;
  164.  
  165.  if (found)
  166.  {
  167.   *object = curr_item->object;
  168.   *objtype = curr_item->type;
  169.  }
  170.  else
  171.  {
  172.   *object = undefined;
  173.   *objtype = undefined;
  174.  }
  175. }
  176.  
  177. void swap()
  178. {
  179. /* SWAP <object>,<object> */
  180.  
  181. char first[MAXIDSIZE],second[MAXIDSIZE],bssbuf[80];
  182. int  typ1,typ2,dataobj1,dataobj2;
  183.  
  184.  insymbol();
  185.  
  186.  if (sym != ident) _error(7);
  187.  else
  188.  {
  189.   strcpy(first,id);
  190.   address_of_object();
  191.   get_obj_info(first,&dataobj1,&typ1);
  192.  
  193.   /* get_obj_info() won't tell us about structure member type */
  194.   if (dataobj1 == structure) typ1 = struct_member_type;
  195.  
  196.   if (dataobj1 != structure && dataobj1 != array) insymbol();
  197.  
  198.   if (sym != comma) _error(16);
  199.   else
  200.   {
  201.    insymbol();
  202.    if (sym != ident) _error(7);
  203.    else
  204.    {
  205.     strcpy(second,id);
  206.     address_of_object();
  207.     get_obj_info(second,&dataobj2,&typ2);
  208.  
  209.     /* get_obj_info() won't tell us about structure member type */
  210.     if (dataobj2 == structure) typ2 = struct_member_type;
  211.  
  212.     if (dataobj2 != structure && dataobj2 != array) insymbol();
  213.  
  214.     /* if two objects are of same data type -> swap them */
  215.     if (typ1 != typ2) _error(4);
  216.     else
  217.     {
  218.      gen("movea.l","(sp)+","a2"); /* second address */
  219.      gen("movea.l","(sp)+","a1"); /* first address */
  220.  
  221.      if (typ1 == stringtype)
  222.      {
  223.       /* make copies of two addresses */
  224.       gen("move.l","a1","d1"); /* first address */
  225.       gen("move.l","a2","d2"); /* second address */
  226.  
  227.       make_temp_string();
  228.  
  229.       /* copy first to temp */
  230.       gen("movea.l","d1","a1");    /* source */ 
  231.       gen("lea",tempstrname,"a0"); /* dest */ 
  232.       gen("jsr","_strcpy","  ");   /* temp = first */
  233.  
  234.       /* copy second to first */
  235.       gen("movea.l","d2","a1");    /* source */ 
  236.       gen("movea.l","d1","a0");    /* dest */ 
  237.       gen("jsr","_strcpy","  ");   /* first = second */ 
  238.  
  239.       /* copy temp to second */
  240.       gen("lea",tempstrname,"a1"); /* source */ 
  241.       gen("movea.l","d2","a0");    /* dest */
  242.       gen("jsr","_strcpy","  ");   /* second = temp */
  243.  
  244.       enter_XREF("_strcpy");
  245.      }
  246.      else
  247.      if (typ1 == shorttype)
  248.      {
  249.       gen("move.w","(a1)","d0");   /* temp = [first] */
  250.       gen("move.w","(a2)","(a1)"); /* [first] = [second] */
  251.       gen("move.w","d0","(a2)");   /* [second] =temp */     
  252.      }
  253.      else
  254.      {
  255.       gen("move.l","(a1)","d0");   /* temp = [first] */
  256.       gen("move.l","(a2)","(a1)"); /* [first] = [second] */
  257.       gen("move.l","d0","(a2)");   /* [second] =temp */     
  258.      }
  259.     }
  260.    }
  261.   }
  262.  } 
  263. }
  264.