logo
class

compiler::Fanp

sys::Obj
  compiler::Fanp
   1  //
   2  // Copyright (c) 2006, Brian Frank and Andy Frank
   3  // Licensed under the Academic Free License version 3.0
   4  //
   5  // History:
   6  //   9 Jan 06  Brian Frank  Creation
   7  //
   8  
   9  **
  10  ** Fan Disassembler
  11  **
  12  class Fanp
  13  {
  14  
  15  //////////////////////////////////////////////////////////////////////////
  16  // Execute
  17  //////////////////////////////////////////////////////////////////////////
  18  
  19    Void execute(Str target)
  20    {
  21      colon := target.index(":")
  22      dot   := target.index(".")
  23      if (colon  == null) printPod(Pod.find(target))
  24      else if (dot < 0) printType(Type.find(target))
  25      else printSlot(Slot.find(target))
  26    }
  27  
  28    Void printPod(Pod pod)
  29    {
  30      p := printer(pod)
  31      if (showTables) { p.tables; return }
  32      p.ftypes
  33    }
  34  
  35    Void printType(Type t)
  36    {
  37      p := printer(t.pod)
  38      if (showTables) { p.tables; return }
  39      ftype := ftype(p.pod, t.name)
  40      p.ftype(ftype)
  41    }
  42  
  43    Void printSlot(Slot slot)
  44    {
  45      p := printer(slot.parent.pod)
  46      if (showTables) { p.tables; return }
  47      fslot := fslot(ftype(p.pod, slot.parent.name), slot.name)
  48      p.slot(fslot)
  49    }
  50  
  51  //////////////////////////////////////////////////////////////////////////
  52  // Utils
  53  //////////////////////////////////////////////////////////////////////////
  54  
  55    FPrinter printer(Pod pod)
  56    {
  57      printer := FPrinter.make(fpod(pod.name))
  58      printer.showCode  = showCode
  59      printer.showLines = showLines
  60      printer.showIndex = showIndex
  61      return printer
  62    }
  63  
  64    FPod fpod(Str podName)
  65    {
  66      ns := FPodNamespace.make(Sys.homeDir + `lib/fan/`)
  67      fpod := ns.resolvePod(podName, true)
  68      fpod.readFully
  69      return fpod
  70    }
  71  
  72    FType ftype(FPod pod, Str typeName)
  73    {
  74      ftype := pod.ftypes.find |FType ft->Bool|
  75      {
  76        r := pod.typeRef(ft.self)
  77        return typeName == pod.n(r.typeName)
  78      }
  79      if (ftype == null) throw UnknownTypeErr.make(pod.name + "::" + typeName)
  80      return ftype
  81    }
  82  
  83    FSlot fslot(FType ftype, Str slotName)
  84    {
  85      FSlot slot := null
  86  
  87      slot = ftype.ffields.find |FSlot s->Bool|
  88      {
  89        return slotName == ftype.fpod.n(s.nameIndex)
  90      }
  91      if (slot != null) return slot
  92  
  93      slot = ftype.fmethods.find |FSlot s->Bool|
  94      {
  95        return slotName == ftype.fpod.n(s.nameIndex)
  96      }
  97      if (slot != null) return slot
  98  
  99      throw UnknownSlotErr.make(slotName)
 100    }
 101  
 102  //////////////////////////////////////////////////////////////////////////
 103  // Run
 104  //////////////////////////////////////////////////////////////////////////
 105  
 106    Void run(Str[] args)
 107    {
 108      if (args.isEmpty) { help; return }
 109  
 110      Str target := null
 111  
 112      // process args
 113      args.each |Str a|
 114      {
 115        if (a.isEmpty) return
 116        if (a == "-help" || a == "-h" || a == "-?")
 117        {
 118          help
 119          return
 120        }
 121        else if (a == "-t") { showTables  = true }
 122        else if (a == "-c") { showCode    = true }
 123        else if (a == "-l") { showLines   = true }
 124        else if (a == "-i") { showIndex   = true }
 125        else if (a[0] == '-')
 126        {
 127          echo("WARNING: Unknown option $a")
 128        }
 129        else
 130        {
 131          target = a
 132        }
 133      }
 134  
 135      if (target == null) { help; return }
 136      execute(target)
 137    }
 138  
 139    Void help()
 140    {
 141      echo("Fan Disassembler");
 142      echo("Usage:");
 143      echo("  fanp [options] <pod>");
 144      echo("  fanp [options] <pod>::<type>");
 145      echo("  fanp [options] <pod>::<type>.<method>");
 146      echo("Options:");
 147      echo("  -help, -h, -?  print usage help");
 148      echo("  -t             print constant pool tables");
 149      echo("  -c             print code buffers");
 150      echo("  -l             print line number table");
 151      echo("  -i             print table indexes in code");
 152    }
 153  
 154  //////////////////////////////////////////////////////////////////////////
 155  // Main
 156  //////////////////////////////////////////////////////////////////////////
 157  
 158    static Void main()
 159    {
 160      make.run(Sys.args)
 161    }
 162  
 163  //////////////////////////////////////////////////////////////////////////
 164  // Fields
 165  //////////////////////////////////////////////////////////////////////////
 166  
 167    Bool showTables := false
 168    Bool showCode   := false
 169    Bool showLines  := false
 170    Bool showIndex  := false
 171  
 172  }