The Delphi --sharpen switch

published: Fri, 1-Apr-2005   |   updated: Thu, 27-Oct-2005

Good grief. I was spelunking through the exe files in the Delphi 2005 bin directory this morning using my trusty hex viewer when I noticed a compiler switch in DCCIL that I'd never seen mentioned before.

The switch was --sharpen. It doesn't appear in the main help info you get with dccil --help, so I tried dccil --sharpen --help instead. The following text was shown:

C:\Borland\Delphi2005\Bin>dccil --sharpen --help
Borland Delphi for .NET compiler version 17.0
Copyright (c) 1983,2004 Borland Software Corporation
.NET Framework v1.1.4322 loaded

--sharpen = (internal use) compile Delphi code to C# 

Wow. So I tried it. I typed up the standard "hello, world" application in hello.dpr:

program Hello;
{$apptype console}
begin
  writeln('hello, world');
end.

and then compiled it with DCCIL and this switch. I got a .cs file like this:

using System;
using System.IO;

namespace Sharpen.Hello;
  class Hello {
    public void main() {
      Console.WriteLine("hello, world");
    }
  }
}

Not only that but there was a .csproj and a .sln file too.

OK, pretty good, and maybe that's all it's geared to do: simple programs. Let's throw in a try finally with a call to Free an object to see how it deals with deterministic memory deallocation:

program Hello;
{$apptype console}

type
  TFoo = class
    private
      FObj : TObject;
    protected
    public
      constructor Create;
      destructor Destroy; override;
      procedure Hello;
  end;

constructor Create;
begin
  inherited Create;
  FObj := TObject.Create;
end;

destructor Destroy; override;
begin
  FObj.Free; 
  inherited Destroy;
end;

procedure Hello;
begin
  writeln('hello, world');
end;

var
  foo : TFoo;
begin
  foo := TFoo.Create;
  try
    foo.Hello;
  finally
    foo.Free;
  end;
end.

The result was very interesting, showing that Danny and the boys are analyzing the code at a really deep level and not just designing a simple conversion tool.

using System;
using System.IO;

namespace Sharpen.Hello;
  class Foo {
    // SHARPEN NOTES
    // TFoo.Destroy not converted - no finalizer, not IDisposable 
    private object obj;
    public Foo() {
      obj = new object();
    }
    public void Hello() {
      Console.WriteLine("hello, world");
    }
  }

  class Hello {
    public void main() {
      Foo foo = new Foo();
      foo.Hello();
    }
  }
}

Notice several things: they've got code to strip the initial T off type names, ditto for the standard F prefix on fields. They're also analyzing TFoo's destructor and noticing that there is no finalizer to the code, nor is there any reference to IDisposable, and so determining that Destroy is not even needed. Bloody 'ell.

Is this the new direction for the Delphi compiler? A translator to C# and then invoking csc.exe to compile it? It would certainly make the the job the Delphi compiler team does radically different. Also we could write code in Delphi and then check in the C# code (good for those shops that require C# and sneer at Delphi). For that we'd have to have an --unsharpen switch as well, but I haven't seen it so far.

I'm going to be investigating this --sharpen switch even more, but I've got to get back to work now.