Skip to content

Commit

Permalink
Merge pull request #87 from Sparrow-lang/types_work
Browse files Browse the repository at this point in the history
Remove mut->ptr implicit conversions
  • Loading branch information
lucteo committed Nov 28, 2019
2 parents bfe8421 + 157d946 commit db7d639
Show file tree
Hide file tree
Showing 138 changed files with 2,232 additions and 1,947 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
callgrind.out.*
*-alt.ll
*-alt.opt.ll
.DS_Store
.out
out
tests/Examples/others/nqueens
Expand Down
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ language: cpp
sudo: required
dist: trusty
compiler: clang
git:
depth: false
matrix:
include:
- os: linux
Expand Down
9 changes: 4 additions & 5 deletions SparrowImplicitLib/logic/lRef.spr
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ fun dtor(this: !ScopeTracer)
[convert]
datatype LRef(valueType: Type)
using ValueType = valueType
using ValuePassType = !@valueType
using isLogicalRef = 1

_pptr: SharedPtr(Optional(ValueType))

fun ctor(this: !LRef)
_pptr ctor Optional(this.ValueType)()
_pptr ctor mkShared(Optional(this.ValueType))
[ct] if ( isValid(logicDebug) && isValid(logicRefDebug) )
cout << "ctor: " << this << "\n"

Expand All @@ -64,7 +63,7 @@ fun ctor(this: !LRef)
// cout << "value ctor: " << this << "\n"

[convert] fun ctor(this: !LRef, value: AnyType) if isValid(this.ValueType(value))
_pptr ctor Optional(this.ValueType)(value)
_pptr ctor mkShared(Optional(this.ValueType), value)
[ct] if ( isValid(logicDebug) && isValid(logicRefDebug) )
cout << "value ctor: " << this << "\n"

Expand Down Expand Up @@ -129,8 +128,8 @@ fun mkLRef(x: !LRefType) = x
fun mkLRef(x: !StringRef) = LRef(String)(x)
fun mkLRef(x: !AnyType): LRef(-@typeOf(x)) = x if !LRefType(x)

fun mkValOrRef(x: !ValWrapper) = x
fun mkValOrRef(x: !StringRef) = Optional(String)(x)
fun mkValOrRef(x: ValWrapper) = x
fun mkValOrRef(x: StringRef) = Optional(String)(x)
fun mkValOrRef(x: !AnyType) = Optional(-@typeOf(x))(x) if !ValWrapper(x)

fun /+/ (l, r: !AnyType) = _Impl.mkPlusOp(mkValOrRef(l), mkValOrRef(r))
Expand Down
3 changes: 2 additions & 1 deletion SparrowImplicitLib/logic/predicates.spr
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ fun ()(this: !Predicate): Bool = predObj()

/// Logic equality: equality test + inference
fun eq(l, r: !AnyType) = _mkEq(mkValOrRef(l), mkValOrRef(r))
fun /=/(l, r: @AnyType) = _mkEq(mkValOrRef(l), mkValOrRef(r))
fun /=/(l, r: !AnyType) = _mkEq(mkValOrRef(l), mkValOrRef(r))
fun /=/(l, r: AnyType) = _mkEq(mkValOrRef(l), mkValOrRef(r))

// Assignment: used for native functions instead of unification
fun =/(out: !AnyType, in: AnyType): Bool if -@typeOf(out) == -@typeOf(in)
Expand Down
2 changes: 1 addition & 1 deletion SparrowImplicitLib/os.spr
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ datatype FileRange
_cur: Char

fun ctor(this: !FileRange, file: !File)
this._file := file
this._file := (file mutToPtr)
_isEmpty = true
_cur ctor
if !_file.isEof
Expand Down
4 changes: 2 additions & 2 deletions SparrowImplicitLib/par/locks.spr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ datatype ScopedLock(T: Type) if Lockable(#$T)
_theLock: @LockType

fun ctor(this: !ScopedLock, theLock: !this.LockType)
this._theLock := theLock
this._theLock := (theLock mutToPtr)
_theLock lock

fun dtor(this: !ScopedLock)
Expand All @@ -32,7 +32,7 @@ datatype ScopedTryLock(T: Type) if TryLockable(#$T)
_lockSucceeded: Bool

fun ctor(this: !ScopedTryLock, theLock: !this.LockType)
this._theLock := theLock
this._theLock := (theLock mutToPtr)
this._lockSucceeded = (_theLock tryLock)

fun dtor(this: !ScopedTryLock)
Expand Down
2 changes: 1 addition & 1 deletion SparrowImplicitLib/par/parFor.spr
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ package Impl
// Create a root task and wait until it's executed
var range: part.RangeType = r
using PartType = typeOf(part)
tasks.spawnRootAndWait(Impl.mkParForTask(range, f, (PartType Ptr)(part), 0))
tasks.spawnRootAndWait(Impl.mkParForTask(range, f, (PartType Ptr)(part mutToPtr), 0))

datatype ParForTask(rangeType, funcType, partType: Type)
prefix: tasks.TaskPrefix
Expand Down
2 changes: 1 addition & 1 deletion SparrowImplicitLib/par/tasks.spr
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fun setContinuation(curTask, cont: !TaskType)

// Chain the continuation to the current task
//assert(getPrefix(curTask).cont isNull)
getPrefix(curTask).cont = getPrefix(cont)
getPrefix(curTask).cont reset getPrefix(cont)

fun enqueue(task: !TaskType)
Impl.initAsRoot(task)
Expand Down
6 changes: 3 additions & 3 deletions SparrowImplicitLib/par/tasksImpl/scheduler.spr
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ fun schedSpawnRootAndWait(task: TaskPtr)
var waitingTask: EmptyTask
initAsRoot(waitingTask)
waitingTask.prefix setRefCount 2
task.get().parent = TaskPtr(waitingTask.prefix)
task.get().parent = TaskPtr(waitingTask.prefix mutToPtr)

// This is the task we are waiting for
waitingTask.prefix setWaitingWorker taskWorker

// Execute the root task and wait for it
schedDoWait(taskWorker, task, TaskPtr(waitingTask.prefix))
schedDoWait(taskWorker, task, TaskPtr(waitingTask.prefix mutToPtr))

// Restore the old depth
taskWorker.curDepth = oldDepth
Expand Down Expand Up @@ -191,7 +191,7 @@ fun executeTask(task: !TaskPrefix): TaskPtr
// Execute the task
[ct] if isValidAndTrue(traceCalls)
tracer(taskWorker) << "executing task " << task << "; parent=" << task.parent.get() << endl
task.executeFn(task)
task.executeFn(task mutToPtr)

// Now check if we can execute the parent

Expand Down
10 changes: 5 additions & 5 deletions SparrowImplicitLib/par/tasksImpl/schedulerIf.spr
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import worker = workerImpl(localWorker)
import std.ptr

fun spawn(task: !TaskPrefix)
sched.schedSpawn(TaskPtr(task))
sched.schedSpawn(TaskPtr(task mutToPtr))
fun spawnAndWaitForAll(curTask, child: !TaskPrefix)
sched.schedWaitForAll(TaskPtr(curTask), TaskPtr(child))
sched.schedWaitForAll(TaskPtr(curTask mutToPtr), TaskPtr(child mutToPtr))
fun spawnRootAndWait(root: !TaskPrefix)
sched.schedSpawnRootAndWait(TaskPtr(root))
sched.schedSpawnRootAndWait(TaskPtr(root mutToPtr))
fun waitForAll(curTask: !TaskPrefix)
sched.schedWaitForAll(TaskPtr(curTask), TaskPtr())
sched.schedWaitForAll(TaskPtr(curTask mutToPtr), TaskPtr())
fun enqueue(task: !TaskPrefix)
sched.schedEnqueue(TaskPtr(task))
sched.schedEnqueue(TaskPtr(task mutToPtr))

fun localWorker(): UntypedPtr = UntypedPtr(worker.localWorker().get())
2 changes: 1 addition & 1 deletion SparrowImplicitLib/par/tasksImpl/taskType.spr
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ concept TaskType(x) \
|| isValidAndTrue(typeOf(x.prefix) == TaskPrefix) )

fun getPrefix(t: !TaskType): @TaskPrefix = t prefix if isValid(t prefix)
fun getPrefix(t: !TaskType): @TaskPrefix = t.prefix if isValid(t.prefix) && !isValid(t prefix)
fun getPrefix(t: !TaskType): @TaskPrefix = t.prefix mutToPtr if isValid(t.prefix) && !isValid(t prefix)

4 changes: 2 additions & 2 deletions SparrowImplicitLib/par/tasksImpl/tasksMain.spr
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fun doExecute(T: Type, prefix: @TaskPrefix): Null
obj execute
return Null()

fun getExecuteFn(T: Type): FunctionPtr(Null rt, @TaskPrefix) = \doExecute(T, TaskPrefix())
fun getExecuteFn(T: Type): FunctionPtr(Null rt, @TaskPrefix) = \doExecute(T, cast(@TaskPrefix, null))

//! Initializes the fields required for a task
fun initTaskBasic(obj: !TaskType)
Expand All @@ -32,7 +32,7 @@ fun initAsRoot(task: !TaskType)
//! Initializes a child task
fun initAsChildOf(task, parent: !TaskType)
initTaskBasic(task)
getPrefix(task).parent = getPrefix(parent)
getPrefix(task).parent reset getPrefix(parent)
getPrefix(task).depth = 1+getPrefix(parent).depth
getPrefix(task).worker = getPrefix(parent).worker

Expand Down
8 changes: 4 additions & 4 deletions SparrowImplicitLib/par/tasksImpl/workerImpl.spr
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ var globalTaskSystem: TaskSystem Ptr
fun initTaskSystem(numWorkers: Int = 0)
// Create the task system object
//assert(globalTaskSystem isNull)
globalTaskSystem = new(TaskSystem)
globalTaskSystem reset new(TaskSystem)

// Create the required threads
if numWorkers == 0
Expand All @@ -136,7 +136,7 @@ fun getInitTaskSystem: TaskSystem Ptr

fun createWorker(ts: TaskSystem Ptr): Worker Ptr
let id: Int = ts.get().workers size
let p: Worker Ptr = new(Worker, ts, id, (IdleCounter Ptr)(ts.get().idleCounter))
let p: Worker Ptr = new(Worker, ts, id, (IdleCounter Ptr)(ts.get().idleCounter mutToPtr))
ts.get().workers.pushBack(p)
return p

Expand All @@ -146,7 +146,7 @@ var cnt: Int = 0
fun getRandomTaskQueue(ts: !TaskSystem): @TaskQueue
// TODO: make the selection truly random
let worker: Worker Ptr = ts.workers(++cnt % (ts.workers size))
return worker.get().work
return worker.get().work mutToPtr

fun onTaskAvailable(ts: !TaskSystem)
// Do something only if we have at least one idle worker
Expand All @@ -169,7 +169,7 @@ fun numAvailableWorkers(ts: !TaskSystem): Int = (ts.workers size) - (ts.idleCoun
//! The worker associated with this thread
_worker: Worker Ptr

fun ()(this: !InternalWorkerThread)
fun ()(this: InternalWorkerThread)
setLocalWorker(_worker)
schedDoWait(_worker.get(), TaskPtr(), TaskPtr())

Expand Down
2 changes: 1 addition & 1 deletion SparrowImplicitLib/par/thread.spr
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ datatype Thread

//! Construct the Thread with a functor
//! The functor can have state, not just executing code
fun ctor(this: !Thread, f: @AnyType) if isValid(f())
fun ctor(this: !Thread, f: AnyType) if isValid(f())
_handle = _Impl.startThread(f)

//! Cannot copy construct or assign a Thread object
Expand Down
20 changes: 11 additions & 9 deletions SparrowImplicitLib/sprCore/basicDecls.spr
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,8 @@ datatype UntypedPtr = @Int8
this.data := other.data
fun ctor(this: !UntypedPtr, val: @AnyType)
this.data := reinterpretCast(@Int8, val)
fun ctor(this: !UntypedPtr, val: !AnyType)
this.data := reinterpretCast(@Int8, val)
fun ctor(this: !UntypedPtr, nullptr: Null)
this.data := null
fun asRefOf(this: UntypedPtr, t: Type): @t = reinterpretCast(@t, this.data)
Expand Down Expand Up @@ -678,28 +680,23 @@ package TypeOp
[native("$typeCanBeUsedAtRt")] fun canBeUsedAtRt(t: Type): Bool
[native("$typeNumRef")] fun numRef(t: Type): Int
[native("$typeChangeMode")] fun changeMode(t: Type, mode: Int): Type
[native("$typeChangeRefCount")] fun changeRefCount(t: Type, numRef: Int): Type
[native("$typeAddRef")] fun addRef(t: Type): Type
[native("$typeRemoveRef")] fun removeRef(t: Type): Type
[native("$typeRemoveCat")] fun removeCat(t: Type): Type
[native("$typeIsBitcopiable")] fun isBitcopiable(t: Type): Bool

[ctGeneric]
fun isRef(t: Type) = 0<numRef(t)
fun addRef(t: Type) = changeRefCount(t, numRef(t)+1)
fun removeRef(t: Type) = ife(isRef(t), changeRefCount(t, numRef(t)-1), t)
fun removeAllRef(t: Type) = changeRefCount(t, 0)
fun atLeastOneRef(t: Type): Type = ife(isRef(t), t, addRef(t))

//! Generate a mutable copy of the given value -- only type manipulation, no body
fun copyVal(val: AnyType): !typeOf(val)
fun copyVal(val: !AnyType): !typeOf(val)
fun copyVal(val: @AnyType): !removeRef(typeOf(val))

/// Add reference operator
[ct, native("$typeAddRef")] fun pre_@ (t: Type): Type
//[ctGeneric] fun pre_@ (t: Type) = TypeOp.addRef(t)
/// Remove reference operator
[ctGeneric] fun pre_-@ (t: Type) = TypeOp.removeRef(t)
/// Operator to ensure at least one refenrence
[ctGeneric] fun pre_!@ (t: Type) = TypeOp.atLeastOneRef(t)
[ct, native("$typeRemoveRef")] fun pre_-@ (t: Type): Type

/// Comparison between types
[ct, native("$typeEQ")] fun == (x, y: Type): Bool
Expand All @@ -722,6 +719,11 @@ fun move(x: !AnyType): typeOf(x) tmp = reinterpretCast(typeOf(x) tmp, x)
fun tmpToMut(x: AnyType tmp): !typeOf(x) = reinterpretCast(!typeOf(x), x)
fun tmpToMut(x: !AnyType): !typeOf(x) = x

//! Transform a mutable value to ptr type
fun mutToPtr(val: !AnyType): @typeOf(val) = reinterpretCast(@typeOf(val), val);
fun mutToPtr(val: ! @AnyType): @ @typeOf(val) = reinterpretCast(@ @typeOf(val), val);
fun mutToPtr(val: ! @ @AnyType): @ @ @typeOf(val) = reinterpretCast(@ @ @typeOf(val), val);

/// Concept that is satisfied by any type
concept AnyType(x) // Always true

Expand Down
4 changes: 4 additions & 0 deletions SparrowImplicitLib/sprCore/basicImpl.llvm
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ define i32 @ptrDiff(i8* %p1, i8* %p2) alwaysinline
%4 = trunc i64 %3 to i32
ret i32 %4
}
define i8* @ptrIdentity(i8* %src) alwaysinline
{
ret i8* %src
}

define void @_spr_memcpy(i8* %dest, i8* %src, i64 %size) alwaysinline
{
Expand Down
2 changes: 1 addition & 1 deletion SparrowImplicitLib/sprCore/mainDef.spr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ datatype MainParameters
_begin, _end: _Impl.CStrPtr

[protected]
fun ctor(this: @MainParameters, argc: Int, argv: @ @Char)
fun ctor(this: !MainParameters, argc: Int, argv: @ @Char)
this._begin = (_Impl.fromArgvPtr(argv))
this._end = (this._begin + argc)

Expand Down
5 changes: 1 addition & 4 deletions SparrowImplicitLib/std/array.spr
Original file line number Diff line number Diff line change
Expand Up @@ -111,13 +111,10 @@ datatype Array(valueType: Type)
fun subrange(this: Array, index: Int, num: Int): RangeType
return RangeType(_begin.advance(index), _begin.advance(index + num))

fun =(this: !Array, other: typeOf(this)): @typeOf(this)
fun =(this: !Array, other: typeOf(this))
var tmp = other

tmp.swap(this)

return this

fun ==(this, other: Array): Bool
if this.size != other.size
return false
Expand Down
Loading

0 comments on commit db7d639

Please sign in to comment.