package org.jruby.ir.persistence;
import org.jruby.RubyInstanceConfig;
import org.jruby.ir.IRMethod;
import org.jruby.ir.IRScope;
import org.jruby.ir.IRScriptBody;
import org.jruby.ir.instructions.Instr;
import org.jruby.parser.StaticScope;
import java.io.IOException;
public class IRWriter {
private IRWriter() { }
public static void persist(IRWriterEncoder file, IRScope script) throws IOException {
file.startEncoding(script);
persistScopeInstructions(file, script);
file.startEncodingScopeHeaders(script);
persistScopeHeaders(file, script);
file.endEncodingScopeHeaders(script);
file.endEncoding(script);
}
private static void persistScopeInstructions(IRWriterEncoder file, IRScope parent) {
persistScopeInstrs(file, parent);
for (IRScope scope: parent.getLexicalScopes()) {
persistScopeInstructions(file, scope);
}
}
private static void persistScopeInstrs(IRWriterEncoder file, IRScope scope) {
file.startEncodingScopeInstrs(scope);
if (scope instanceof IRMethod && !scope.hasBeenBuilt()) {
((IRMethod) scope).lazilyAcquireInterpreterContext();
}
for (Instr instr: scope.getInterpreterContext().getInstructions()) {
file.encode(instr);
}
file.endEncodingScopeInstrs(scope);
}
private static void (IRWriterEncoder file, IRScope parent) {
persistScopeHeader(file, parent);
for (IRScope scope: parent.getLexicalScopes()) {
persistScopeHeaders(file, scope);
}
}
private static void (IRWriterEncoder file, IRScope scope) {
if (RubyInstanceConfig.IR_WRITING_DEBUG) System.out.println("Writing Scope Header");
file.startEncodingScopeHeader(scope);
scope.persistScopeHeader(file);
if (RubyInstanceConfig.IR_WRITING_DEBUG) System.out.println("NAME = " + scope.getId());
if (scope instanceof IRScriptBody) {
file.encode(scope.getId());
} else {
file.encodeRaw(scope.getName());
file.encode(scope.getLexicalParent());
}
persistStaticScope(file, scope.getStaticScope());
scope.persistScopeFlags(file);
file.endEncodingScopeHeader(scope);
}
private static void persistStaticScope(IRWriterEncoder file, StaticScope staticScope) {
file.encode(staticScope.getType());
file.encode(staticScope.getVariables());
file.encode(staticScope.getFirstKeywordIndex());
file.encode(staticScope.getSignature());
}
}