[m-rev.] diff: flatten init_struct in .NET backend.

Tyson Dowd trd at miscrit.be
Wed Sep 19 00:37:20 AEST 2001


Hi,

It took a while to figure out why I traversing the RTTI data structures
didn't quite work, despite the code being equivalent to the C backend...

===================================================================


Estimated hours taken: 1
Branches: main

compiler/mlds_to_il.m:
	Flatten the contents of init_struct(...) before generating the
	element initialziers.
	This makes them match the layout of structures in the C backend,
	as nesting { .... } initializers in C flatten in this way.


Index: compiler/mlds_to_il.m
===================================================================
RCS file: /home/mercury1/repository/mercury/compiler/mlds_to_il.m,v
retrieving revision 1.81
diff -u -r1.81 mlds_to_il.m
--- compiler/mlds_to_il.m	27 Aug 2001 12:15:34 -0000	1.81
+++ compiler/mlds_to_il.m	18 Sep 2001 14:24:51 -0000
@@ -1333,9 +1333,19 @@
 data_initializer_to_instrs(init_obj(Rval), _Type, node([]), InitInstrs) --> 
 	load(Rval, InitInstrs).
 
-	% Currently, structs are the same as arrays.
-data_initializer_to_instrs(init_struct(InitList), Type,
+	% MLDS structures initializers are assumed to be initialized like
+	% structures in C, which means nested elements are actually laid out
+	% flat in the structure.
+	%
+	% So we flatten structures, and then process them as arrays
+	% (this may have to be re-visited if used to initialise high-level
+	% data).
+
+data_initializer_to_instrs(init_struct(InitList0), Type,
 		AllocInstrs, InitInstrs) --> 
+
+	
+	{ InitList = flatten_inits(InitList0) },
 	data_initializer_to_instrs(init_array(InitList), Type,
 		AllocInstrs, InitInstrs).
 
@@ -1343,8 +1353,9 @@
 	% For sub-initializations, we don't worry about keeping AllocInstrs
 	% and InitInstrs apart, since we are only interested in top level
 	% allocations.
-data_initializer_to_instrs(init_array(InitList), Type,
+data_initializer_to_instrs(init_array(InitList0), Type,
 		AllocInstrs, InitInstrs) -->
+
 		%
 		% figure out the array element type
 		%
@@ -1415,6 +1426,24 @@
 maybe_box_initializer(init_obj(Rval), init_obj(NewRval)) -->
 	{ rval_to_type(Rval, BoxType) },
 	{ NewRval = unop(box(BoxType), Rval) }.
+
+
+	% Code to flatten nested intializers.
+
+:- func flatten_inits(list(mlds__initializer)) = list(mlds__initializer).
+flatten_inits(Inits0) = list__condense(list__map(flatten_init(Inits))).
+
+:- func flatten_init(mlds__initializer) = list(mlds__initializer).
+flatten_init(I) = Inits :-
+	( I = init_struct(Inits0) ->
+		Inits = flatten_inits(Inits0)
+	; I = init_array(Inits0) ->
+		Inits = flatten_inits(Inits0)
+	;
+		Inits = [X]
+	).
+	
+
 
 %-----------------------------------------------------------------------------%
 %

--------------------------------------------------------------------------
mercury-reviews mailing list
post:  mercury-reviews at cs.mu.oz.au
administrative address: owner-mercury-reviews at cs.mu.oz.au
unsubscribe: Address: mercury-reviews-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-reviews-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the reviews mailing list