package org.jruby.ir.persistence;
import org.jruby.RubyInstanceConfig;
import org.jruby.RubySymbol;
import org.jruby.ir.IRClosure;
import org.jruby.ir.IRMethod;
import org.jruby.ir.IRScope;
import org.jruby.ir.IRScopeType;
import org.jruby.ir.IRScriptBody;
import org.jruby.ir.instructions.Instr;
import org.jruby.ir.operands.LocalVariable;
import org.jruby.parser.StaticScope;
import org.jruby.util.ByteList;
import java.io.IOException;
import java.util.Map;
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.encode(scope.getName());
file.encode(scope.getLexicalParent());
}
persistStaticScope(file, scope.getStaticScope());
persistLocalVariables(scope, file);
file.endEncodingScopeHeader(scope);
}
private static void persistLocalVariables(IRScope scope, IRWriterEncoder file) {
Map<RubySymbol, LocalVariable> localVariables = scope.getLocalVariables();
if (RubyInstanceConfig.IR_WRITING_DEBUG) System.out.println("PERSISTING LVARS (" + localVariables.size() + ")");
file.encode(localVariables.size());
for (RubySymbol name: localVariables.keySet()) {
file.encode(name);
int offset = localVariables.get(name).getOffset();
if (RubyInstanceConfig.IR_WRITING_DEBUG) System.out.println(" NAME: " + name + "(0:" + offset + ")");
file.encode(offset);
}
}
private static void persistStaticScope(IRWriterEncoder file, StaticScope staticScope) {
file.encode(staticScope.getType());
file.encode(staticScope.getVariables());
file.encode(staticScope.getFirstKeywordIndex());
file.encode(staticScope.getSignature());
}
}