MCPcopy Create free account
hub / github.com/SeaOfNodes/Simple / alloc

Method alloc

chapter16/src/main/java/com/seaofnodes/simple/Parser.java:769–813  ·  view source on GitHub ↗

Parse an allocation

()

Source from the content-addressed store, hash-verified

767 if( !(t instanceof TypeMemPtr tmp) )
768 throw error("Cannot allocate a "+t.str());
769
770 // Parse new struct { default_initialization }
771 StructNode s = INITS.get(tmp._obj._name);
772 if( s==null ) throw error("Unknown struct type '" + tmp._obj._name + "'");
773
774 Field[] fs = s._ts._fields;
775 // if the object is fully initialized, we can skip a block here.
776 // Check for constructor block:
777 boolean hasConstructor = match("{");
778 Ary<Node> init=s._inputs; int idx=0;
779 if( hasConstructor ) {
780 idx = _scope.nIns();
781 // Push a scope, and pre-assign all struct fields.
782 _scope.push();
783 for( int i=0; i<fs.length; i++ )
784 _scope.define(fs[i]._fname, fs[i]._type, fs[i]._final, s.in(i));
785 // Parse the constructor body
786 parseBlock();
787 require("}");
788 init = _scope._inputs;
789 }
790 // Check that all fields are initialized
791 for( int i=idx; i<init.size(); i++ )
792 if( init.get(i)._type == Type.TOP )
793 throw error("'"+tmp._obj._name+"' is not fully initialized, field '" + fs[i-idx]._fname + "' needs to be set in a constructor");
794 Node ptr = newStruct(tmp._obj, con(tmp._obj.offset(fs.length)), idx, init );
795 if( hasConstructor )
796 _scope.pop();
797 return ptr;
798 }
799
800
801 /**
802 * Return a NewNode initialized memory.
803 * @param obj is the declared type, with GLB fields
804 * @param init is a collection of initialized fields
805 */
806 private Node newStruct( TypeStruct obj, Node size, int idx, Ary<Node> init ) {
807 Field[] fs = obj._fields;
808 if( fs==null )
809 throw error("Unknown struct type '" + obj._name + "'");
810 Node prior = mem().keep();
811 MemMergeNode input = new MemMergeNode(null);
812 for( Field f : fs ) input.alias(f._alias,prior);
813 // Constructor values follow control, memory, and allocation size.
814 Node[] ns = new Node[3+fs.length];
815 ns[0] = ctrl();
816 ns[1] = input.peephole();

Callers 1

parsePrimaryMethod · 0.95

Calls 15

typeMethod · 0.95
errorMethod · 0.95
matchMethod · 0.95
parseExpressionMethod · 0.95
requireMethod · 0.95
typeAryMethod · 0.95
newArrayMethod · 0.95
strMethod · 0.95
parseBlockMethod · 0.95
newStructMethod · 0.95
conMethod · 0.95
keepMethod · 0.45

Tested by

no test coverage detected