2121from api .errors import InvalidOperatorError
2222from api .errors import InvalidBuiltinFunctionError
2323from api .errors import InternalError
24+ from zxbpp import zxbpp
2425
2526from . import backend
2627from .backend .__float import _float
3637 'FunctionTranslator' ]
3738
3839JumpTable = namedtuple ('JumpTable' , ('label' , 'addresses' ))
40+ LabelledData = namedtuple ('LabelledData' , ('label' , 'data' ))
3941
4042
4143class Translator (TranslatorVisitor ):
@@ -1305,7 +1307,9 @@ def visit_LBOUND(self, node):
13051307 elif entry .scope == SCOPE .parameter :
13061308 self .ic_fparam (entry .t , entry .offset )
13071309 elif entry .scope == SCOPE .local :
1308- self .ic_fparam (entry .t , - entry .offset )
1310+ self .ic_paddr (- entry .offset , entry .t )
1311+ t1 = optemps .new_t ()
1312+ self .ic_fparam (gl .PTR_TYPE , t1 )
13091313 self .ic_call ('__LBOUND' , self .TYPE (gl .BOUND_TYPE ).size )
13101314 backend .REQUIRES .add ('bound.asm' )
13111315
@@ -1318,7 +1322,9 @@ def visit_UBOUND(self, node):
13181322 elif entry .scope == SCOPE .parameter :
13191323 self .ic_fparam (entry .t , entry .offset )
13201324 elif entry .scope == SCOPE .local :
1321- self .ic_fparam (entry .t , - entry .offset )
1325+ self .ic_paddr (- entry .offset , entry .t )
1326+ t1 = optemps .new_t ()
1327+ self .ic_fparam (gl .PTR_TYPE , t1 )
13221328 self .ic_call ('__UBOUND' , self .TYPE (gl .BOUND_TYPE ).size )
13231329 backend .REQUIRES .add ('bound.asm' )
13241330
@@ -1364,6 +1370,8 @@ def start(self):
13641370 self .visit (f )
13651371
13661372 def visit_FUNCTION (self , node ):
1373+ bound_tables = []
1374+
13671375 self .ic_label (node .mangled )
13681376 if node .convention == CONVENTION .fastcall :
13691377 self .ic_enter ('__fastcall__' )
@@ -1378,6 +1386,28 @@ def visit_FUNCTION(self, node):
13781386 # return
13791387
13801388 if local_var .class_ == CLASS .array and local_var .scope == SCOPE .local :
1389+ bound_ptrs = [] # Bound tables pointers (empty if not used)
1390+ lbound_label = local_var .mangled + '.__LBOUND__'
1391+ ubound_label = local_var .mangled + '.__UBOUND__'
1392+
1393+ if local_var .lbound_used or local_var .ubound_used :
1394+ bound_ptrs = ['0' , '0' ] # NULL by default
1395+ if local_var .lbound_used :
1396+ bound_ptrs [0 ] = lbound_label
1397+ if local_var .ubound_used :
1398+ bound_ptrs [1 ] = ubound_label
1399+
1400+ if bound_ptrs :
1401+ zxbpp .ID_TABLE .define ('__ZXB_USE_LOCAL_ARRAY_WITH_BOUNDS__' , lineno = 0 )
1402+
1403+ if local_var .lbound_used :
1404+ l = ['%04X' % bound .lower for bound in local_var .bounds ]
1405+ bound_tables .append (LabelledData (lbound_label , l ))
1406+
1407+ if local_var .ubound_used :
1408+ l = ['%04X' % bound .upper for bound in local_var .bounds ]
1409+ bound_tables .append (LabelledData (ubound_label , l ))
1410+
13811411 l = [len (local_var .bounds ) - 1 ] + [x .count for x in local_var .bounds [1 :]] # TODO Check this
13821412 q = []
13831413 for x in l :
@@ -1388,7 +1418,7 @@ def visit_FUNCTION(self, node):
13881418 r = []
13891419 if local_var .default_value is not None :
13901420 r .extend (self .array_default_value (local_var .type_ , local_var .default_value ))
1391- self .ic_larrd (local_var .offset , q , local_var .size , r ) # Initializes array bounds
1421+ self .ic_larrd (local_var .offset , q , local_var .size , r , bound_ptrs ) # Initializes array bounds
13921422 elif local_var .class_ == CLASS .const :
13931423 continue
13941424 else : # Local vars always defaults to 0, so if 0 we do nothing
@@ -1451,6 +1481,9 @@ def visit_FUNCTION(self, node):
14511481 else :
14521482 self .ic_leave (node .params .size )
14531483
1484+ for bound_table in bound_tables :
1485+ self .ic_vard (bound_table .label , bound_table .data )
1486+
14541487 def visit_FUNCDECL (self , node ):
14551488 """ Nested scope functions
14561489 """
0 commit comments