Author Topic: [SOLVED] Oxygen's Equivalents to C, Pls?  (Read 12385 times)

0 Members and 2 Guests are viewing this topic.

JRS

  • Guest
Re: [SOLVED] Oxygen's Equivalents to C, Pls?
« Reply #15 on: October 04, 2014, 11:58:40 AM »
Then I'll emit nanoscheme as a 32-bit application with 64-bit calc and then you'll port it to RTL64.inc yourself. It will then be the best, beautifullest, and fastest Scheme interpreter on the net, guaranteed. :D

The definition of commitment!

Mike Lobanovsky

  • Guest
Re: [SOLVED] Oxygen's Equivalents to C, Pls?
« Reply #16 on: October 04, 2014, 12:04:58 PM »
The definition of commitment!

Let's wait and see Charles' commitment to jump on the bandwagon first. :D

Charles Pegge

  • Guest
Re: [SOLVED] Oxygen's Equivalents to C, Pls?
« Reply #17 on: October 04, 2014, 12:28:15 PM »

Then I will emit guidlines :)

always use sys integers to hold pointers, not int or long !

If you are going ahead with indexed functions, I will need to formulate a safer protocol for compatibility.


rax/eax update:

http://www.oxygenbasic.org/o2zips/Oxygen.zip

Mike Lobanovsky

  • Guest
Re: [SOLVED] Oxygen's Equivalents to C, Pls?
« Reply #18 on: October 04, 2014, 12:45:02 PM »
Quote
always use sys integers to hold pointers, not int or long !

Will do.

Quote
If you are going ahead with indexed functions, I will need to formulate a safer protocol for compatibility.

If you're talking about

function foo(sys bar) as sys, label

then I'm all ears.

Charles Pegge

  • Guest
Re: [SOLVED] Oxygen's Equivalents to C, Pls?
« Reply #19 on: October 04, 2014, 02:37:11 PM »
The ears attached to your avatar are truly enormous. You can probably hear me muttering two thousan miles away :)

It is safe to work with a fully protyped function:

Indexed Functions
Code: [Select]
function foo1(quad a) as quad, label
return 10
end function

function foo2(quad a) as quad, label
return 20
end function

function foo3(quad a) as quad, label
return 30
end function

sys fooTable={@foo1,@foo2,@foo3}

!* foo(quad a) as quad 'prototype used for every foo

'TEST

indexbase 1
for i=1 to 3
  @foo=fooTable[i] 'assign address
  print foo(42) 'make call
next

Mike Lobanovsky

  • Guest
Re: [SOLVED] Oxygen's Equivalents to C, Pls?
« Reply #20 on: October 04, 2014, 02:52:57 PM »
Great!

All of nanoscheme's indexed functions conform to a common proto

! function opexe_N (DWORD operator) as pointer

where DWORD stands for unsigned long, and pointer, for cell* as defined in my C code at the top of this page.

Will that do for our purposes?

Charles Pegge

  • Guest
Re: [SOLVED] Oxygen's Equivalents to C, Pls?
« Reply #21 on: October 04, 2014, 03:30:16 PM »

Sure, I just checked - operator and pointer are not reserved words yet :)

Need a few hours astral. o__/\_

Mike Lobanovsky

  • Guest

JRS

  • Guest
Re: [SOLVED] Oxygen's Equivalents to C, Pls?
« Reply #23 on: October 04, 2014, 09:44:01 PM »

View from Afar, Lake District, Cumbria, England

Charles Pegge

  • Guest
Re: [SOLVED] Oxygen's Equivalents to C, Pls?
« Reply #24 on: October 04, 2014, 11:46:35 PM »


Charles Pegge

  • Guest
Re: [SOLVED] Oxygen's Equivalents to C, Pls?
« Reply #25 on: October 05, 2014, 02:47:42 AM »
Hi Mike,

My further thought on C equivalents:

Code: [Select]
struct cell{
    unsigned long _flag;
    union {
      struct {
        char* _svalue;
        long  _keynum;
      } _string;
      struct {
        union {
          __int64 _ivalue;
          double  _rvalue;
        } _value;
      } _number;
      struct {
        struct cell* _car;
        struct cell* _cdr;
      } _cons;
    } _object;
  } cell;

  typedef struct cell* pointer;

  #define type(p)           ((p)->_flag)

  #define isfixnum(p)       ((type(p) & T_FLONUM) == T_NUMBER)
  #define isflonum(p)       ((type(p) & T_FLONUM) == T_FLONUM)

  #define ivalue(p)         ((p)->_object._number._value._ivalue)
  #define rvalue(p)         ((p)->_object._number._value._rvalue)

  #define num_ivalue(p)     (isfixnum(p) ? ivalue(p) : (__int64)rvalue(p))
  #define num_rvalue(p)     (isflonum(p) ? rvalue(p) : (double)ivalue(p))

Code: [Select]
'CLARIFYING CELL STRUCTURE
==========================

type cell
  sys    flag
  char*  svalue
  sys    keynum
  =
  sys    flag
  quad   ivalue
  =
  sys    flag
  double rvalue
  =
  sys    flag
  cell*  car
  cell*  cdr
end type

'#recordof cell

typedef cell *pointer

enum types { T_NULL, T_NUMBER, T_FLONUM }

'print T_FLONUM

macro isfixnum(p)     (p##.flag == T_NUMBER)
macro isflonum(p)     (p##.flag == T_FLONUM)
macro ivalue(p)       p##.ivalue   
macro rvalue(p)       p##.rvalue   


'C STYLE FUNCTIONS
==================

quad   num_ivalue(cell*p) {if isfixnum(p) {return p.ivalue} return p.rvalue }
double num_rvalue(cell*p) {if isfixnum(p) {return p.ivalue} return p.rvalue }


'OPTIMISED ASM FUNCTIONS: 32BIT MODE STDCALL
============================================

quad   num_ivalue(cell*p) at num_ivalues_asm
double num_rvalue(cell*p) at num_rvalues_asm


'TEST
=====

double d
quad   q
sys    n=3
#show cell p at getmemory 100 * sizeof cell
#show q = num_ivalue p[n]
free memory @cell


'ASSEMBLY CODE
==============

jmp fwd over

.num_ivalues_asm

mov eax,[esp+4] 'first param
(
cmp  [eax],T_NUMBER
jnz exit
fild qword [eax+8]
ret 4
)
fld qword  [eax+8]
frndint 'ROUNDING
ret 4

.num_rvalues_asm

mov eax,[esp+4] 'first param
(
cmp  [eax],T_NUMBER
jnz exit
fild qword [eax+8]
ret 4
)
fld qword  [eax+8]
ret 4

.over


'USING THE IFF MACRO (DUAL 32/64)
=================================

deff iff
and al,1
(
jnz exit
fxch st1
)
fstp st0
end deff

macro num_ivalue(p) iff(isfixnum(p), p##.ivalue, p##.rvalue)
macro num_rvalue(p) iff(isfixnum(p), p##.ivalue, p##.rvalue)


'ROUNDING?
==========
'it may be desirable to truncate or round when returning num_ivalues

macro num_ivalue(p) iff(isfixnum(p), p##.ivalue, round(p##.rvalue) )

I think the optimised asm functions  will give the shortest code stream and best performance:

quad   num_ivalue(pointer p) at num_ivalues_asm

...

.num_ivalues_asm

mov eax,[esp+4] 'first param 'pointer'
(
cmp  [eax],T_NUMBER
jnz exit
fild qword [eax+8]
ret 4
)
fld qword  [eax+8]
frndint 'ROUNDING
ret 4
« Last Edit: October 05, 2014, 05:24:51 AM by Charles Pegge »

Mike Lobanovsky

  • Guest
Re: [OFFTOPIC] Oxygen's Equivalents to C, Pls?
« Reply #26 on: October 05, 2014, 04:51:53 AM »
@John:

Thanks for this beautiful picture! I didn't have much chance to enjoy Great Britain's countryside but I remember I really liked what I actually happened to see. And this picture is a perfect match to my recollections. :)


@Charles:

Do you still fly in your dreams?? :o I remember I stopped doing so somewhere at the age of 27 or 28. They say one stops to when one's organism stops to grow and physical maturity sets in. But I still remember that wonderful sense of flight and I would give away much to be able to feel it again. :)

Mike Lobanovsky

  • Guest
Re: [SOLVED] Oxygen's Equivalents to C, Pls?
« Reply #27 on: October 05, 2014, 05:33:18 AM »
Charles,

1. I agree with your vision of cell structure in O2. The C version is an almost exact replica of original author's version with my amendments as to its alignment which used to be really horrible in the original.

Since none of cell's fields are accessed directly throughout the code but rather through the #defines only, your simplifications will not meddle with actual porting. And they also simplify the compiler's part of work since it has fewer offsets to resolve.

2. Your macros isfixnum() and isflonum() won't do because what I presented is only a fraction of possible data types and respective flag combinatins which may be composite. So, bit masking must stay intact. It is used in data type transformations and also in GC where flag overlays are used to mark the cells for recovery and as vacant entities.

3. Your "C style functions" may stay with their returns uncast only if you swear in your mamma's name that Oxygen will do that automatically and won't faulter at that. (but still it leaves me somewhat uneasy...)

4. I'd rather we'd better (hehe...) stick with the asm functions as you suggest.

5. There is absolutely no any transparent rounding of floating-point values in LISP. Everything is always done through explicit (round), (floor), (ceiling), and (truncate) commands. Even if a data type transformation is under way, e.g. (inexact->exact), and the value to be transformed is not a flint, and there is not an explicit command of the four aforementioned ones as to how to round the value in question, an error must be flagged.

And of course there shouldn't be any rounding in case of arithmetic comparisons either, by definition.

Charles Pegge

  • Guest
Re: [SOLVED] Oxygen's Equivalents to C, Pls?
« Reply #28 on: October 05, 2014, 05:53:35 AM »

Dream Flying:

Occasionally. I have a few problems with navigation and maintaining altitude :). A few nights ago, I found myself hanging onto the side of a 6 storey building, - forgot to fly.

It may be helpful to cultivate daydreaming, so you can habitually bring intellect into the dream process.

Apparently, there is a disconnect between waking memory and dream experience. You need to recall your dream experiences and think about them as you wake up. Otherwise the memories fade very quickly.

re: cell

I have stumbled over a few issues with pointer types, which fight O2's implicit referencing. So I advise typing your cell variables as cell *, rather than using pointer.

Code: [Select]
double d
quad   q
sys    n=3
'DYNAMIC ARRAY OF 100 CELLS
cell p at getmemory 100 * sizeof cell
'cell *p = getmemory 100 * sizeof cell 'equivalent
q = num_ivalue p[n]
...
free memory @cell

Mike Lobanovsky

  • Guest
Re: [SOLVED] Oxygen's Equivalents to C, Pls?
« Reply #29 on: October 05, 2014, 06:08:28 AM »
Quote
Dream Flying:


Quote
re: cell

Absolutely no problem with that. I'll be keeping on typing pointer everywhere and finally auto-replace all occurences of pointer with cell* throughout the sources. :)